程序流程结构
一.顺序结构
二.选择结构
1.if语句
单行
多行
多条件
int main()
{
//if(条件)
// { 条件满足执行的语句;
//}
int score = 0;
cout << "请输入一个分数" << endl;
cin >> score;
if (score > 600)
{
if (score > 700)
{
printf("yes yes yes");
}
else
{
printf("yes yes");
}
}
else if(score > 500)
{
printf("yes");
}
else
{
printf("not");
}
return 0;
}
2.三目运算符
表达式1?表达式2:表达式3;
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
printf("%d\n", c);
//三目运算符返回值在c++是变量,可继续赋值
(a > b ? a : b) = 100;
printf("%d\n", b);
return 0;
}
3只小猪
#include<iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "3只小猪体重" << endl;
cin >> a;
cin >> b;
cin >> c;
cout << ((a > b) ? (b > c ? a : c) : (a > c ? a : c) )<< endl;
}
3.switch语句
switch(表达式)
{
case 结果一:
语句1;
break;
case 结果二:
语句2;
break;
case 结果三:
语句3;
break;
.........
default:
语句n;
break;
}
//滑梯效应
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "给电影打分" << endl;
cin >> score;
switch (score)
{
case 10:
case 9://滑梯
cout << "经典" << endl;
break;
case 8:
cout << "非常好" << endl;
default:
cout << "一般" << endl;
break;
}
}
//switch 语句表达式只能整型或字符型;
case后没有break,会一直执行下去
三,循环语句
while(循环条件)多次循环
{
循环语句;
}
if(循环条件)一次条件
{
循环语句;
}
#include<iostream>
using namespace std;
int main()
{
int num = 0;
while (num < 10)
{
cout << "num=" << num << endl;
num++;
}
return 0;
int a = 0;
}
@@@@@@@
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int num = rand() % 100+1;
int input = 0;
while (1)//条件任意
{
cin >> input;
if (input > num)
{
cout << "大了" << endl;
}
else if(input<num)
{
cout << "小了" << endl;
}
else
{
cout << "恭喜你,过关" << endl;
break;//跳出循环
}
}
return 0;
}
do
{
循环语句;
}while(循环条件);//先做后判定
#include<iostream>
using namespace std;
int main()
{
int num = 0;
do {
cout << num << endl;
num++;
} while (num < 10);
return 0;
}
for循环语句
for(起始表达式;条件表达式;末尾表达式)
{
循环语句;
}
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
return 0;
}
嵌套循环
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j< 10; j++)
{
cout << "*" << " ";
}
cout << endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
for (int num = 0; num < 100; num++)
{
if ( (num % 10 ==7 )|| (num / 10 == 7 )|| num%7==0 )
{
cout << "拍桌子" << endl;
}
else {
cout << num << endl;
}
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << i << "*" << j<< "=" << j*i << " ";
}
cout << endl;
}
return 0;
}
break语句
在switch 终止case跳出switch
在循环语句 跳出当前循环
在嵌套语句 跳出内层循环语句
#include<iostream>
using namespace std;
int main()
{
cout << "请选择副本难度" << endl;
cout << "1.普通" << endl;
cout << "2.中等" << endl;
cout << "3.困难" << endl;
int num = 0;
cin >> num;
switch (num)
{
case 1:
cout << "你选择的是普通难度" << endl;
break;
case 2:
cout << "你选择的是中等难度" << endl;
break;
case 3:
cout << "你选择的是困难难度" << endl;
break;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
return 0;
}
continue语句
跳出本次循环,继续下次循环;
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
continue;
}
cout << i << endl;
}
return 0;
}
goto无条件跳跃语句