04 程序流程结构: 顺序、选择和循环
1、选择结构
1.1、if 语句(同C)
1) if(条件){ 条件满足执行的语句 }
2) if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 }
3) if(条件1){ 条件1满足执行的语句 }else if(条件2){条件2满足执行的语句}... else{ 都不满足执行的语句}
✔练习案例: 三只小猪称体重
有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重?
 
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int pigA,pigB,pigC; 7 cout<<"please input pigA's weight: "<<endl; 8 cin>>pigA; 9 cout<<"please input pigB's weight: "<<endl; 10 cin>>pigB; 11 cout<<"please input pigC's weight: "<<endl; 12 cin>>pigC; 13 if(pigA>pigB) 14 { 15 if(pigA>pigC) 16 { 17 cout<<"pigA is the fattest"<<endl; 18 } 19 else cout<<"pigC is the fattest"<<endl; 20 } 21 else if(pigB>pigC) 22 { 23 cout<<"pigB is the fattest"<<endl; 24 } 25 else cout<<"pigC is the fattest"<<endl; 26 27 system("pause"); 28 return 0; 29 }
1.2、三目运算符
   语法:表达式1 ? 表达式2 :表达式3
判断表达式1的值。为真,执行表达式2,并返回表达式2的结果;为假,执行表达式3,并返回表达式3的结果。
1.3、switch 语句
 switch(表达式)            //表达式类型只能是整型或者字符型
   {
     case 结果1:执行语句;break;  //case里如果没有break,那么程序会一直向下执行
     case 结果2:执行语句;break;
     ...
     default 执行语句;   break;
   }
总结:与if语句比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间。
2、循环结构
2.1 while 语句
语法:while(循环条件){ 循环语句 } //只要循环条件的结果为真,就执行循环语句
✔练习案例:猜数字
系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,
如果猜对恭喜玩家胜利,并且退出。
 
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a=rand()%100+1,b; 7 cout<<"Guess the random! please input your num(1~100):"<<endl; 8 cin>>b; 9 if(b==a) 10 cout<<"Unbelievable!! you got it in the first round!"<<endl; 11 else 12 { 13 while (b!=a) 14 { 15 if(b>a) 16 { 17 cout<<"it is too big,please input again:"<<endl; 18 cin>>b; 19 } 20 else 21 { 22 cout<<"it is too small,please input again:"<<endl; 23 cin>>b; 24 } 25 } 26 cout<<"congratulation!! you got it!"<<endl; 27 } 28 29 30 system("pause"); 31 return 0; 32 }
2.2 do...while语句
语法:do{循环语句}while(循环条件);//与while区别:do…while先执行一次循环语句,再判断循环条件
✔练习案例:水仙花数
水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
例如:1^3 + 5^3+ 3^3 = 153
请利用do…while语句,求出所有3位数中的水仙花数
 
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int bai,shi,ge,i=100; 7 do 8 { 9 bai=i/100; 10 shi=(i%100)/10; 11 ge=i%10; 12 if(bai*bai*bai+shi*shi*shi+ge*ge*ge==i) 13 cout<<i<<" "<<endl; 14 i++; 15 16 } 17 while(i<1000); 18 system("pause"); 19 return 0; 20 } //运行结果:153 370 371 407
2.3、for循环语句
语法:for(起始表达式;条件表达式;末尾循环体) { 循环语句; }✔练习案例:敲桌子
从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,
我们打印敲桌子,其余数字直接打印输出。
 
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a; 7 for(a=1;a<=100;a++) 8 { 9 if(a%7==0||a%10==7||a/10==7) 10 { 11 cout<<a<<"knock desk "<<endl; 12 } 13 else cout<<a<<endl; 14 } 15 system("pause"); 16 return 0; 17 }
✔练习案例:乘法口诀表
利用嵌套循环,实现九九乘法表
 
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a,b,c,i,j,sum; 7 for(i=1;i<=9;i++) 8 { 9 for(j=1;j<=i;j++) 10 { 11 sum=i*j; 12 cout<<j<<"x"<<i<<"="<<sum<<" "; 13 } 14 cout<<endl; //endl:end a line 即为换行符,作用同\n,自动换行,不加就不换行 15 } 16 17 system("pause"); 18 return 0; 19 } 20 21 22 23
3、跳转语句
3.1、break 语句
作用: 用于跳出选择结构或者循环结构。
使用时机:
出现在switch语句中,作用是终止case并跳出switch
出现在循环语句中,作用是跳出当前的循环语句
出现在嵌套循环中,跳出最近的内层循环语句(跳出内层for语句)
3.2 continue 语句
作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环。
3.3 goto 语句
作用:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置。
语法:goto 标记;
注意:在程序中不建议使用goto语句,以免造成程序流程混乱。
 
 
  
 
  
  
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号