C++ Today 04
4.程序流程结构
C/C++支持最基本的三种程序运行结构:顺序结构,选择结构,循环结构
顺序结构:程序按顺序执行,不发生跳转
选择结构:依据条件是否满足,有选择的执行相应功能
循环结构:依据条件是否满足,循环多次执行某段代码
4.1选择结构
if语句
作用:执行满足条件的语句
if语句的三种行式
-
单行格式if语句 if(条件){条件满足执行的语句}
-
多行格式if语句 if(条件){条件满足执行的语句} else{条件不执行的语句}
-
多条件if语句
- if(条件){条件1满足执行的语句}else if(条件2){条件2执行的语句}·····else{都不满足的语句}
#include<iostream> using namespace std; int main21() { int score = 0; cout << "请输入一个分数:" <<endl; cin >> score; cout << "您输入的分数为:" << score <<endl; if(score>400) { cout << "恭喜您专升本考试成功上岸。" <<endl; if(score>=450) { cout << "恭喜您考上江西财经大学" <<endl; }else if(score>=420) { cout << "恭喜您考上公办院校" <<endl; } }else if(score>350) { cout << "恭喜考上民办本科" <<endl; }else { cout << "没有上岸。" <<endl; } return 0; }
案例
三只小猪找体重
#include<iostream> using namespace std; int main() { int a,b,c; cout << "请输入三只小猪的体重:" <<endl; cin >> a >> b >> c; if(a>b)//a比b重 { if(a>c)//a比c重 { cout << "a小猪的体重最重" <<endl; }else if(c>a)//c比a重 { cout << "c小猪的体重最重" <<endl;A } }else if(b>a)//b比啊、重 { if(b>c)//b比c重 { cout << "b小猪的体重最重" <<endl; }else if(c>b) { cout << "c小猪的体重最重" <<endl; } } }
4.2循环结构
4.2.1 while循环语句
作用:满足循环条件,执行循环语句
语法:while(循环条件){循环语句}
// 只要循环条件的结果为真,就执行循环语句
#include<iostream> using namespace std; int main31() { int num = 0; while(num < 10) { cout << num <<endl; num++; } return 0; }
案例:猜数字
int num = rand() % 100 + 1; //rand()%100 生成0·99随机数 伪随机
srand((unsigned int)time(NULL)); 真随机
#include<iostream>
#include<stdlib.h>
#include<ctime>//time系统时间头文件包含 using namespace std; int main() { int c = 1; //系统生成随机数 cout << "请输入猜测的数" <<endl; //添加随机数种子 作用利用当前 time用系统时间头文件 srand((unsigned int)time(NULL)); int num = rand() % 100 + 1; //rand()%100 生成0·99随机数 //cout << num <<endl; 伪随机 int val = 0; while(1) { cin >> val; if(val>num) { cout << "猜测过大" <<endl; }else if(val<num){ cout << "猜测过小" <<endl; }else{ cout << "猜对了" <<endl;break; } c++; if(c>5) break; } system("pause"); return 0; }
4.2.2do-while循环语句
作用:满足循环条件,执行循环语句
语法:do{循环语句}while(循环条件);
注意:与while的区别在于do-while会先执行一次循环语句,在判断循环条件
#include<iostream> using namespace std; int main() { /*int a = 0; while(a<10) { cout << a <<endl; a++; }*/ int num = 0; //do-while会先执行一次 do { cout << num <<endl; num++; }while(num<10); }
案例:水仙花数
水仙花数是一个三位数,它的每个位上的数字的3次幂之和等于它本身
eg:1的5次幂+5的3次幂+3的3次幂=153
请利用do-while语句是,求出所以3位数中的水仙花数
题解
/* 将所以的三位数进行输出(100-999) 在所以的三位数中找到水仙花数 水仙花数:获取个位 %10,获取十位 /10%10,获取百位 /100 判断 个位的3次幂 + 十位的3次幂 + 百位的3次幂 = 本身 */
#include<iostream> using namespace std; int main() { /* 将所以的三位数进行输出(100-999) 在所以的三位数中找到水仙花数 水仙花数:获取个位 %10,获取十位 /10%10,获取百位 /100 判断 个位的3次幂 + 十位的3次幂 + 百位的3次幂 = 本身 */ int num = 100; do { int a = 0,b = 0,c = 0;//个 十 百 a=num%10; b=num/10%10; c=num/100; if(a*a*a + b*b*b + c*c*c == num) { cout << num <<endl; } num++; }while(num < 1000); return 0; }
4.2.3for循环语句
作用:满足循环语句是,执行循环语句
语法:for(起始表达式;条件表达式;某位循环体){循环语句;}
#include<iostream> using namespace std; int main() { for(int i = 0;i < 10;i++) { cout << i <<endl; } system("pause"); return 0; }
案例:敲桌子
从一数到100,如果数字各位含7,或者数字十位含7,或者数字是7的倍数,我们打印敲桌子,其余数字直接打印输出
#include<iostream> using namespace std; int main() { /*for(int i = 0;i < 10;i++) { cout << i <<endl; }*/ //从一数到100,如果数字各位含7,或者数字十位含7,或者数字是7的倍数,我们打印敲桌子,其余数字直接打印输出 for(int i = 1;i <= 100;i++) { if(i % 7 == 0||i % 10 == 7||i / 10 == 7) { cout << "敲桌子" <<endl; }else { cout << i <<endl; } } system("pause"); return 0; }
4.2.4嵌套循环
作用:在循环体中在嵌套一层循环,解决一些实际问题
#include<iostream> using namespace std; int main() { //外层一次,内层执行一周 for(int i = 0;i < 10;i++)//外层循环 { for(int j = 0;j < 10;j++)//内层循环 { cout <<" * "; } cout << endl; } system("pause"); return 0; }
案例:乘法口诀表
#include<iostream> using namespace std; int main() { for(int i = 1;i < 10;i++) { for(int j = 1;j < i;j++) { cout << j << "*" << i << "=" << i*j <<" ";//列数 * 行数 = 结果 } cout << endl; } }
4.3跳转语句
4.3.1 break语句
作用:用于跳出选择语句或者循环语句
使用时机:
-
出现在switch条件语句中,作用是终止case并跳出switch
-
出现在循环语句中,作用是跳出当前的循环语句
-
出现在嵌套语句中,跳出最近的内层循环语句
#include<iostream> using namespace std; int main() { /*1.出现在switch语句中 cout << "请选择副本难度" <<endl; cout << "1.普通" <<endl; cout << "2.中等" <<endl; cout << "3.困难" <<endl; int select = 0;//创建选择结果的变量 cin >> select ; //等待用户的输入 switch(select) { case 1: cout << "您选择的是普通难度" <<endl;break; case 2: cout << "您选择的是中等难度" <<endl;break; case 3: cout << "您选择的是困难难度" <<endl;break; default: 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 == 2) { break;//退出内层循环 } cout << "*"; } cout <<endl; } }
4.3.2continue
作用:在循环语句中,跳出本次循环中余下尚未执行的语句,继续执行下一次循环
continue:执行到本次,就不在执行后面的代码了,而执行下一次循环
#include<iostream> using namespace std; int main() { //continue语句 for(int i = 0;i < 100;i++) { if(i % 2 == 0) { continue;//输出奇数,偶数不输出 } cout << i <<endl; } system("pause"); return 0; }
4.3.3 goto语句
作用:可以无条件跳转语句
语法:goto 标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
//不建议使用,容易造成代码混乱
#include<iostream> using namespace std; int main() { //goto语句 cout <<"1.china" <<endl; cout << "us" <<endl; goto flag; cout << "rb" <<endl; cout << "odly" <<endl; flag: cout << "xny" <<endl; }
#include<iostream>
using namespace std;
int main()
{ for(int i = 1;i < 10;i++) { for(int j = 1;j < i;j++) { cout << j << "*" << i << "=" << i*j <<" ";//列数 * 行数 = 结果 } cout << endl; }}

浙公网安备 33010602011771号