2、选择结构(if语句、if语句的嵌套、switch语句)

1、单行格式if语句:

#include<iostream>
using namespace std;
int main()
{
   int score;
   cout<<"please input a number:"<<endl;
   cin>>score;

   //单行if语句:if(条件){条件满足执行的语句}
   if (score>100)
   {
       cout<<"the input number is:"<<score<<endl;
   }
}

2、多行格式if语句:

int main()
{
   int score;
   cout<<"please input a number:"<<endl;
   cin>>score;
   
    //多行格式:if(条件){条件满足执行的语句}else{条件不满足执行的语句}
   if (score==100)
   {
       cout<<"满分"<<endl;
   }
   else
   {
       cout<<"不是满分"<<endl;
   }
}

3、多条件

int main()
{
   int score;
   cout<<"please input a number:"<<endl;
   cin>>score;


    //多条件:f(条件1){条件1满足执行的语句}else if(条件2){条件1满足执行的语句}...else{都不满足执行的语句}
   if(score>60 and score<=80)
   {
        cout<<"通过"<<endl;
   }
   else if(score>80 and score<=100)
   {
        cout<<"优秀"<<endl;
    }
    else
    {
        cout<<"不及格"<<endl;
    }
}

4、if 语句的嵌套

int main()
{
   //if 语句的嵌套
   int score;
   cout<<"please input a number:";
   cin>>score;
   if(score<0 || score>100)
   {
       cout<<"请输入合格的分数"<<endl;
   }
   else
   {
       if (score<60)
       {
           cout<<"不及格"<<endl;
       }
       else if(score>60 && score<80)
       {
           cout<<"合格"<<endl;
       }
       else if(score>80 && score<100)
       {
           cout<<"优秀"<<endl;
       }
       else if(score==100)
       {
           cout<<"满分"<<endl;
       }
   }
}

5、switch语句:

int main()
{
    /*
    switch(表达式)
    {
        case 结果1:执行语句;break;
        case 结果2:执行语句;break;
        ...
        default:执行语句;break;         //如果前面情况都没有出现,则执行该处
    }
    */
    int grade;
    cout<<"请输入打分:";
    cin>>grade;
    switch(grade)
    {
        case 10:cout<<"牛逼电影"<<endl;break;
        case 9:cout<<"优秀电影"<<endl;break;
        case 8:cout<<"凑乎能看的电影"<<endl;break;
        default:cout<<"烂片"<<endl;break;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小树苗m

您的打赏,是我的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值