8-【快乐学习c++】流程结构

if语句

#include<iostream>
using namespace std;
//if语句
int main(){
    //1-输入分数
    int score = 0;
    cout << "请输入一个分数" << endl;
    cin >> score;
    //2-打印用户输入分数
    cout << "您的分数为:" << score <<endl;
    //3-判断是否考上大学
    if (score > 90)
    {
        cout << "恭喜您获得A!" << endl;
    }
    else if (score > 80)
    {
        cout << "恭喜您获得B!" << endl;
    }
    else if (score > 60)
    {
        cout << "恭喜您获得C!" << endl;
    }//注意-if后面不能加上分号
    else
    {
        cout << "不及格!" << endl;
    }
}

switch语句

#include<iostream>
using namespace std;
//switch语句
int main(){
    //电影打分
    //10-9  经典
    //8-7   非常好
    //6-5   一般
    //5-0   烂片
    int score = 0;
    cout << "请给电影打分" << endl;
    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;

    }

}

while语句

#include<iostream>
using namespace std;
//while语句
int main(){
    //打印0~9
    int i = 0;
    while (i != 10)
        {
        cout << "i:" << i++ <<endl;
        }
}

do while语句

#include<iostream>
using namespace std;
//do while语句
int main(){
    //先执行一次循环语句,再看是否满足条件
    //打印0~9
    int i = 0;
    do
    {
    cout << "i:" << i++ <<endl;
    }
    while(i != 10);

}

for循环语句

#include<iostream>
using namespace std;
//for循环语句
int main(){
    //语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}
    //打印0~9

    for(int i=0;i!=10;i++)
    {
    cout << "i:" << i <<endl;
    }
}

嵌套循环语句

#include<iostream>
using namespace std;
//嵌套循环
int main(){
    for(int i=0;i!=10;i++)
    {
        for(int i=0;i!=10;i++)
        {
        cout << "* ";
        }
    cout <<endl;
    }
}

goto跳转语句

#include<iostream>
using namespace std;
//跳转语句
int main(){
    //break 场景:switch、循环、嵌套
    //continue
    //goto 无条件跳转 语法:goto 标记;
    cout << "1、*********** "<<endl;
    cout << "2、*********** "<<endl;
    goto FLAG;
    cout << "3、*********** "<<endl;
    cout << "4、*********** "<<endl;
    FLAG:
    cout << "5、*********** "<<endl;

}

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107548382