流程控制
If語句的語法形式
- 單一if語句:
- 例:if (x > y) cout << x;
 
- 例:
- if-else語句:
- 例:if (x > y) cout << x; else cout << y;
 
- 例:
- 多重if-else語句:
- 例:if (x > y) cout << x; else if (y > x) cout << y; else cout << "x = y";
 
- 例:
例2_3:輸入兩個整數,比較兩個數的大小
//2_3.cpp
#include<iostream>
using namespace std;
int main() {
    int x, y;
    cout << "Enter x and y: ";
    cin >> x >> y;
    if (x != y) {
        if (x > y)
            cout << "x > y" << endl;
        else
            cout << "x < y" << endl;
    } else {
        cout << "x = y" << endl;
    }
    return 0;
}
嵌套的if結構
- 語法形式:
- if( 表達式1 ) if( 表達式2 ) 语句1 else 语句2 else if( 表達式3 ) 语句3 else 语句4
 
- 注意事項:
- 語句1、2、3、4可以是複合語句,每層的if與else應配對,或用{}來確定層次關係。
 
- 語句1、2、3、4可以是複合語句,每層的if與else應配對,或用
例2_3:輸入兩個整數,比較兩個數的大小
//2_4.cpp
#include <iostream>
using namespace std;
int main() {
    int day;
    cin >> day;
    switch (day) {
        case 0: cout << "星期日" << endl; break;
        case 1: cout << "星期一" << endl; break;
        case 2: cout << "星期二" << endl; break;
        case 3: cout << "星期三" << endl; break;
        case 4: cout << "星期四" << endl; break;
        case 5: cout << "星期五" << endl; break;
        case 6: cout << "星期六" << endl; break;
        default: cout << "超出範圍,請輸入0到6之間的數字" << endl; break;
    }
    return 0;
}
switch語句的語法
- 一般形式:
- switch( 表達式 ) { case 常量表達式1: 语句1 case 常量表達式2: 语句2 ... default: 语句n+1 }
 
- 執行順序:
- 以case中的常量表達式值為入口標號,由此開始順序執行。因此,每個case分支最後應該加break語句。
 
- 以
- 分支:
- case分支可包含多個語句,且不用- {}。
- 表達式、判斷值都是int型或char型。
- 若干分支執行內容相同可共用一組語句。
 
例2_5:求自然數1~10之和
//2_5.cpp
#include <iostream>
using namespace std;
int main() {
    int i = 1, sum = 0;
    while (i <= 10) {
        sum += i; //相當於sum = sum + i;
        i++;
    }
    cout << "sum = " << sum << endl;
    return 0;
}
執行結果:
sum = 55
while語句的語法
- 形式:
- while( 表達式 ) 语句
- 可以是複合語句,其中必須含有改變條件表達式值的語句。
 
- 執行順序:
- 先判斷表達式的值,若為true,則執行語句。
 
- 先判斷表達式的值,若為
例2_6:輸入一個整數,將各位數字翻轉後輸出
//2_6.cpp
#include <iostream>
using namespace std;
int main() {
    int n, right_digit;
    cout << "Enter the number: ";
    cin >> n;
    cout << "The number in reverse order is ";
    do {
        right_digit = n % 10;
        cout << right_digit;
        n /= 10; //相當於n=n/10
    } while (n != 0);
    cout << endl;
    return 0;
}
執行結果:
Enter the number: 365
The number in reverse order is 563
do-while語句的語法形式
- 一般形式:
- do 语句 while ( 表達式 )
- 可以是複合語句,其中必須含有改變條件表達式值的語句。
 
- 執行順序:
- 先執行循環體語句,後判斷條件。表達式為true時,繼續執行循環體。
 
- 先執行循環體語句,後判斷條件。表達式為
例2_7:用do-while語句,求自然數1~10之和
//2_7.cpp
#include <iostream>
using namespace std;
int main() {
    int i = 1, sum = 0;
    do {
        sum += i;
        i++;
    } while (i <= 10);
    cout << "sum = " << sum << endl;
    return 0;
}
對比下面的程式
#include <iostream>
using namespace std;
int main() {
    int i, sum = 0;
    cin >> i;
    while (i <= 10) {
        sum += i;
        i++;
    }
    cout << "sum = " << sum << endl;
    return 0;
}
#include <iostream>
using namespace std;
int main() {
    int i, sum = 0;
    cin >> i;
    do {
        sum += i;
        i++;
    } while (i <= 10);
    cout << "sum = " << sum << endl;
    return 0;
}
例2_8:輸入一個整數,求出它的所有因數
//2_8.cpp
#include <iostream>
using namespace std;
int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;
    cout << "Number " << n << " Factors ";
    for (int k = 1; k <= n; k++)
        if (n % k == 0)
            cout << k << " ";
    cout << endl;
    return 0;
}
執行結果1:
Enter a positive integer: 36
Number 36 Factors 1 2 3 4 6 9 12 18 36
執行結果2:
Enter a positive integer: 7
Number 7 Factors 1 7
for語句
- 語法形式:
- for (初始語句; 表達式1; 表達式2) 语句
- 循環前先求解初始語句,當表達式1為true時執行循環體,之後求解表達式2。
 
- 範圍for語句:
- 另一種更加簡潔的寫法,主要用於遍歷容器中的序列。
 
循環結構與選擇結構的嵌套
- 例2_10:輸入一系列整數,統計正整數的個數和負整數的個數,讀入0則結束。
- 分析:
- 需要讀入一系列整數,但數量不定,需在每次讀入後進行判斷,使用while循環最為合適。循環控制條件應為n != 0。由於要判斷數字的正負並分別進行統計,因此需要在循環內嵌入選擇結構。
 
- 需要讀入一系列整數,但數量不定,需在每次讀入後進行判斷,使用
例2-10
- 思考:是否有冗餘的判斷?如何修改?
//2_10.cpp
#include <iostream>
using namespace std;
int main() {
    int i = 0, j = 0, n;
    cout << "Enter some integers (enter 0 to quit):" << endl;
    cin >> n;
    while (n != 0) {
        if (n > 0) i += 1;
        if (n < 0) j += 1;
        cin >> n;
    }
    cout << "Count of positive integers: " << i << endl;
    cout << "Count of negative integers: " << j << endl;
    return 0;
}
