目录
1.选择结构
1.1if语句
1.1.1单行if语句
1.1.2多行格式的if语句
1.1.3多条件if语句
1.1.4嵌套if语句
1.2三目运算符
1.3switch语句
2.循环结构
2.1while循环
2.2do..while语句
2.3for循环语句
2.4嵌套循环
3.跳转语句
3.1.1break
3.1.2continue
3.1.3goto语句

#include
using namespace std;int main()
{//选择结构 单行if语句//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出int score;cout << "请输入一个分数" << endl;cin >> score;cout << "您输入的分数为:" << score << endl;if (score > 600){cout << "恭喜您考上了一本大学" << endl;}system("pause");return 0;}

#include
using namespace std;int main()
{//选择结构 多行if语句//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出//否则打印未考上一本大学int score;cout << "请输入一个分数" << endl;cin >> score;cout << "您输入的分数为:" << score << endl;if (score > 600){cout << "恭喜您考上了一本大学" << endl;}else{cout << "未考上一本大学" << endl;}system("pause");return 0;}

#include
using namespace std;int main()
{//选择结构 多条件if语句//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出//大于500分,视为考上二本大学//大于400分,视为考上三本大学//小于等于400分,视为未考上本科int score;cout << "请输入一个分数" << endl;cin >> score;cout << "您输入的分数为:" << score << endl;if (score > 600){cout << "恭喜您考上了一本大学" << endl;}else if(score>500){cout << "恭喜您考上了二本大学" << endl;}else if (score > 400){cout << "恭喜您考上了三本大学" << endl;}else{cout << "您未考上本科" << endl;}system("pause");return 0;}

#include
using namespace std;int main()
{//选择结构 多条件if语句//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出//大于500分,视为考上二本大学//大于400分,视为考上三本大学//小于等于400分,视为未考上本科int score;cout << "请输入一个分数" << endl;cin >> score;cout << "您输入的分数为:" << score << endl;if (score > 600){if (score > 700){cout << "恭喜您考上了北京大学" << endl;}else if (score > 650){cout << "恭喜您考上了清华大学" << endl;}else{cout << "恭喜您考上了人民大学" << endl;}}else if(score>500){cout << "恭喜您考上了二本大学" << endl;}else if (score > 400){cout << "恭喜您考上了三本大学" << endl;}else{cout << "您未考上本科" << endl;}system("pause");return 0;}
练习,比较三个数的大小
#include
using namespace std;int main()
{int a, b, c;cout << "输入三只小猪的体重" << endl;cin >> a >> b >> c;int ans;if (a > b){if (a > c){cout << "A小猪最重" << endl;}else{cout << "C小猪最重" << endl;}}else{if (b > c){cout << "B小猪最重" << endl;}else{cout << "C小猪最重" << endl;}}system("pause");return 0;}

#include
using namespace std;int main()
{//三目运算符//将a和b作比较,将变量大的值赋值给变量cint a = 10;int b = 20;int c;c = (a > b ? a : b);cout << c << endl;//在C++中三目运算符返回的是变量,可以继续赋值(a > b ? a : b) = 100;cout << "a=" << a << "b=" <

#include
using namespace std;int main()
{//switch语句//给电影打分//10~9 经典//8~7 非常好//6~5 一般//5以下 烂片cout << "请给电影打分" << endl;int score;cin >> score;cout << "您打的分数为:" << score << endl;switch (score){case 10:cout << "经典电影" << endl;break;case 9:cout << "经典电影" << endl;break;case 8:cout << "非常好" << endl;break;case 7:cout << "非常好" << endl;break;case 6:cout << "一般" << endl;break;case 5:cout << "一般" << endl;break;default:cout << "烂片" << endl;break;}//if和switch的区别//switch缺点,判断的时候只能是整型或者字符型,不可以是一个区间//switch优点,结构清晰,执行效率高system("pause");return 0;}


#include
using namespace std;int main()
{//while循环//在屏幕中打印0~9这10个数字int a = 0;while (a < 10){cout << a << endl;a++;}system("pause");return 0;}
猜数字
#include
using namespace std;int main()
{//while循环//添加随机数种子 作用利用当前系统时间生成随机数,防止每次随机数都一样srand((unsigned int)time(NULL));//rand() % 100;//生成0-99的随机数int num=rand() % 100 + 1;//生成1-100之间的随机数int val;while (1){cin >> val;if (val > num){cout << "猜大了" << endl;}else if(val < num){cout << "猜小了" << endl;}else{cout << "猜对了" << endl;break;//利用break,跳出当前的循环}}system("pause");return 0;}

#include
using namespace std;int main()
{//do...while语句//在屏幕中输出0-9int a = 0;do{cout << a << endl;a++;} while (a < 10);system("pause");return 0;}

#include
using namespace std;int main()
{//do...while语句//在屏幕中输出0-9int num = 100;int ge, shi, bai,tmp;do{tmp = num;ge = tmp % 10;tmp = tmp / 10;shi = tmp % 10;bai = tmp / 10;if (num == ge * ge * ge + shi * shi * shi + bai * bai * bai){cout << num << endl;}num++;} while (num < 1000);system("pause");return 0;}



#include
using namespace std;int main()
{//for循环//从数字0打印到数字9for (int i = 1; i <= 10; i++){for (int j = 0; j <= 10; j++){cout << "*";}cout << endl;}system("pause");return 0;}
#include
using namespace std;int main()
{//for循环//打印9 9乘法表for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){cout << j << "*" << i<<"="<

#include
using namespace std;int main()
{//break使用//1.出现在switch语句中cout << "请选择副本难度" << endl;cout << "1:普通 ,2:中等,3:困难" << endl;int select;cin >> select;switch (select){case 1:cout << "普通" << endl;break;case 2:cout << "中等" << endl;break;default:cout << "困难" << endl;break;}//2.出现在循环语句中for (int i = 0; i <= 10; i++){if (i == 5){break;}cout << i << endl;}//3.出现在嵌套循环语句中for (int i = 0; i <10; i++){for (int j = 0; j < 10; j++){if (j == 5){break;//退出内层循环}cout << "*";}cout << endl;}system("pause");return 0;}


#include
using namespace std;int main()
{//continue语句for (int i = 0; i <10; i++){if (i % 2 == 0){continue;//执行到此就不再向下执行,执行下一次循环}cout << i << endl;}system("pause");return 0;}

#include
using namespace std;int main()
{//goto语句cout << "1.XXX" << endl;cout << "2.XXX" << endl;goto FLAG;//标记一般大写cout << "3.XXX" << endl;cout << "4.XXX" << endl;FLAG:cout << "5.XXX" << endl;system("pause");return 0;}
上一篇:量子计算(十):量子计算原理