美文网首页
C++ 控制结构

C++ 控制结构

作者: 小潤澤 | 来源:发表于2020-03-16 19:52 被阅读0次

    if语句

    1.单行if语句

    #include<iostream>
    using namespace std;
    
    int main(){
    
      ##单行if语句
      //输入分数
      int score = 0;
      cout<<"请输入一个分数: "<<end1;
      cin>>score;
    
      //打印输入的分数
      cout<<"您输入的分数是: "<<score<<end1;
    
      //判断分数是否大于600
      if(score > 600)
     {
         cout<<"恭喜您考上一本大学"<<end1;
     }
      system("pause");
      return 0;
    }
    

    2.多行if语句


    满足条件执行一个语句,不满足条件执行另一个语句

    #include<iostream>
    using namespace std;
    
    int main(){
    
      ##多行if语句
      //输入分数
      int score = 0;
      cout<<"请输入一个分数: "<<end1;
      cin>>score;
    
      //打印输入的分数
      cout<<"您输入的分数是: "<<score<<end1;
    
      //判断分数是否大于600
      if(score > 600)
     {
         cout<<"恭喜您考上一本大学"<<end1;
     }
      else
     {
         cout<<"未考上一本大学"<<end1;
     }
      system("pause");
      return 0;
    }
    

    3.多条件if语句

    #include<iostream>
    using namespace std;
    
    int main(){
    
      ##多行if语句
      //输入分数
      int score = 0;
      cout<<"请输入一个分数: "<<end1;
      cin>>score;
    
      //打印输入的分数
      cout<<"您输入的分数是: "<<score<<end1;
    
      //判断分数是否大于600
      if(score > 600)
     {
         cout<<"恭喜您考上一本大学"<<end1;
     }
      else if(score > 500)
     {
         cout<<"恭喜您考上二本大学"<<end1;
     }
      else if(score > 400)
     {
         cout<<"恭喜您考上三本大学"<<end1;
     }
      else
     {
         cout<<"未考上本科大学"<<end1;
     }
    
      system("pause");
      return 0;
    }
    

    4.嵌套if语句

    顾名思义,就是if语句里面再用一个if语句

    #include<iostream>
    using namespace std;
    
    int main(){
    
      ##多行if语句
      //输入分数
      int score = 0;
      cout<<"请输入一个分数: "<<end1;
      cin>>score;
    
      //打印输入的分数
      cout<<"您输入的分数是: "<<score<<end1;
    
      //判断分数是否大于600
      if(score > 600)
     {
         cout<<"恭喜您考上一本大学"<<end1;
         if(score > 700)
       {
              cout<<"恭喜您考上北京大学"<<end1;
       }
         if(score > 650)
       {
              cout<<"恭喜您考上清华大学"<<end1;
       }
         else
       {
              cout<<"恭喜您考上人民大学"<<end1;
       }
     }
      else if(score > 500)
     {
         cout<<"恭喜您考上二本大学"<<end1;
     }
      else if(score > 400)
     {
         cout<<"恭喜您考上三本大学"<<end1;
     }
      else
     {
         cout<<"未考上本科大学"<<end1;
     }
    
      system("pause");
      return 0;
    }
    

    选择switch语句

    作用是执行多条件语句,不过case要和break连用,break是退出当前分支

    #include<iostream>
    using namespace std;
    
    int main(){
    
      //输入分数
      int score = 0;
      cin>>score;
      cout<<"您的分数: "<<score<<end1;
    
      //多条件执行
      switch(score)
     {
          case 10:
                cout<<"您认为是经典电影"<<end1;
               break;//退出当前分支
           case 9:
                cout<<"您认为是经典电影"<<end1;
                break;//退出当前分支
           case 8:
                cout<<"您认为电影非常好"<<end1;
                break;//退出当前分支
           case 7:
                cout<<"您认为是电影非常好"<<end1;
                break;//退出当前分支
           case 6:
                cout<<"您认为电影一般"<<end1;
                break;//退出当前分支
           case 5:
                cout<<"您认为电影一般"<<end1;
                break;//退出当前分支
            default:
                cout<<"您认为电影很烂"<<end1;
                break;//退出当前分支
    ##这里的default是指其他情况,即除分数为10,9,8,7,6,5的情况
     }
      system("pause");  return 0;
    }
    

    这里的default是指其他情况,即除分数为10,9,8,7,6,5的情况

    循环结构

    1.while


    在循环体中满足条件退出循环

    #include<iostream>
    using namespace std;
    
    int main(){
      int num = 0;
      while (num < 10)
     { 
         cout<<num<<end1;
         num++;#每次加一    
     }
     system("pause");
      return 0;
    }
    

    2.do while语句

    这个循环的特点是满足条件执行循环


    #include<iostream>
    using namespace std;
    
    int main(){
      int num = 0;
    
      do
     {
         cout<<num<<end1;
         num++;
     }
      while (num < 10);
    
      system("pause");
      return 0;
    }
    

    我们可以看到,循环体在do这个结构里面

    3.for循环

    for循环的特点是满足循环条件,执行循环

    #include<iostream>
    using namespace std;
    
    int main(){
      
      for (int i = 0;i < 10; i++)
     {
          cout<< i <<end1;
     }
      system("pause");
      return 0;
    }
    

    for循环将起始值写在第一个,条件写在第二个,最后写循环体
    如果在for循环里面不写初始值,条件也可以,就代码长点

    #include<iostream>
    using namespace std;
    
    int main(){
      
      int i = 0;
      for ( ;  ; )
     {
         if(i >= 10)
      {
           break;
      }
        cout<< i <<end1;
     }
      system("pause");
      return 0;
    }
    

    跳出语句

    1.break

    参见swich语句,这里不再叙述

    2.continue语句

    在循环语句中,跳过本次循环余下未执行的语句,继续执行下一次循环

    #include<iostream>
    using namespace std;
    
    int main(){
      
      for (int i = 0;i < 10; i++)
     {
           if(i % 2 == 0)
      {
            continue;
            //如果i为偶数的时候跳过
      }
        cout<< i <<end1;
     }
      system("pause");
      return 0;
    } 
    

    3.goto语句

    #include<iostream>
    using namespace std;
    
    int main(){
      
      cout << "1.xxx"<<end1;
      goto FLAG:
      cout<<"2.xxx"<<end1;
      FLAG:
      cout<<"3.xxx"<<end1;
    
      system("pause");
      return 0;
    } 
    

    goto语句的用处是跳过第二个cout,直接输出第三cout

    相关文章

      网友评论

          本文标题:C++ 控制结构

          本文链接:https://www.haomeiwen.com/subject/bsccjhtx.html