c++基礎知識02

1.前置與後置區別

#include<iostream>
using namespace std;

int main()
 {
    //前置和後置區別
    //前置遞增或遞減 先讓變數加減1 然後進行表達式運算
    int a1 = 10;
    int b1 = ++a1 * 10;
    cout << "a1=" << a1 << endl;
    cout << "b1=" << b1 << endl;
    //後置遞增或遞減 先進行表達式運算,後讓變數加減1
    int a2 = 10;
    int b2 = a2++ * 10;
    cout << "a2=" << a2 << endl;
    cout << "b2=" << b2 << endl;

    system("pause");

    return 0;
}

2.if選擇結構

#include<iostream>
using namespace std;
int main() {
    //選擇結構 
    int score = 0;
    cout << "請輸入一個分數" << endl;
    cin >>score;
    if (score > 600) //if條件後不要加分號
    {
        cout << "恭喜您考上一本大學" << endl;
    }
    else
    {
        cout << "很遺憾您未能考上一本" << endl;
    };
    //多條件選擇結構
    int grade = 0;
    cout << "請輸入一個分數"  << endl;
    cin >> grade;
    cout << "您輸入的分數為:" << grade << endl;

    if (grade > 600)
    {
        cout << "恭喜您考上一本大學" << endl;
        if (grade>700)
        {
            cout << "您能考入985" << endl;
        }
        else if (grade>650)
        {
            cout << "您能考入211" << endl;
        }
        else
        {
            cout << "您能考入普通一本" << endl;
        }
    }
    else if (grade > 500)
    {
        cout << "恭喜您考上二本大學" << endl;
    }
    else if (grade > 400)
    {
        cout << "恭喜您考上三本大學" << endl;
    }
    else
    {
        cout << "很遺憾您未能考上本科" << endl;
    }

    system("pause");

    return 0;
}

3.while與do..while循環結構

#include<iostream>
using namespace std;

int main()                            
{
    //while循環語句
    //輸出數字0~9
    int num = 0;
    
    while (num < 10) 
    {
        cout << num << endl;
        num++;
        
    }
    
    //do...while循環語句: do{循環語句}while(循環條件);
    // 與while的區別在於會先執行一次循環語句,再判斷循環條件

    do
    {
        cout << num << endl;
        num++;
    } while (num < 10);


    system("pause");

    return 0;