C++ 流程控制(分支、循环、跳转)

#include

using namespace std;

int main()
{
    // 单分支和双分支
    cout << "please enter your age:" << endl;

    int age;
    cin >> age;
    if(age >= 18)
    {
        cout << "welcome! adult." << endl;
    }
    else
    {
        cout << "only adult can pass." << endl;
    }
    

    age >= 18 ? cout << "welcome, adult!" << endl : cout << "only adult can pass" << endl;

    cout << (age >= 18 ? "welcome, adult!" : "only adult can pass!") << endl;
    
    
    //多分支
    cout << "please enter your age:" << endl;
    int age;
    cin >> age;
    if (age < 12)
    {
        cout << "child" << endl;
    }
    else if (age <= 18)
    {
        cout << "teenager" << endl;
    }
    else if (age <= 35)
    {
        cout << "keep going" << endl;
    }
    else
    {
        cout << "enjoy your life" << endl;
    }

    // switch
    cout << "please enter your score level:" << endl;
    char score;
    cin >> score;
    switch (score)
    {
    case 'A':
        cout << "score >= 90 && score <= 100" << endl;
        break;
    case 'B':
        cout << "score >= 80 && score < 90" << endl;
        break;
    case 'C':
        cout << "score < 80" << endl;
        break;

    default:
        cout << "wrong score" << endl; 
        break;
    }

    // while
    cout << "loop strat...\n" << endl;

    int i = 1;
    while(i <= 10)
    {
        cout << "hello world" << i++ << endl;
    }

    do while
    int i = 10;
    do
    {
        cout << i-- << "次循环" << endl;
    } while (i > 0);
    

    //for循环
    for (int i = 0; i < 10; i++)
    {
        cout << i << "次循环" <<endl;
    }
    
    //范围for循环
    for (int num : {1, 3, 5, 7, 9})
    {
        cout << "序列中输出的数据是" << num << endl;
    }
    
    cin.get();
    cin.get();
}
// continue
// 逢7过
    for (int num = 0; num < 101; num++)
    {
        cout << "\t";
        if (num % 7 == 0 || num % 10 == 7 || num / 10 == 7)
        {
            continue;
        }
        cout << num;
    }
// goto
int x = 0;
    cout << "循环开始。。。" << endl;
begin:
    do
    {
        cout << x++ << "次循环" << endl;
    } while (x <= 10);

    if (x < 15)
    {
        cout << "回到原点" << endl;
        
        goto begin;
    }

你可能感兴趣的:(C++学习记录,c++,算法,开发语言)