選擇 - Asia Universitydns2.asia.edu.tw/~wzyang/slides/programming/B/ch04_AEL0080.pdf · 3/42...

21
1/42 在正常的情況下,電腦程式的執行 在正常的情況下,電腦程式的執行 是以敘述的排列次序逐步處理的。 是以敘述的排列次序逐步處理的。 使用控制架構 使用控制架構 (control structures) (control structures) 可以改變這種既定的先後次序,讓 可以改變這種既定的先後次序,讓 程式得以進行更複雜的運算,或以 程式得以進行更複雜的運算,或以 更簡潔的指令來實現演算法。 更簡潔的指令來實現演算法。 4 2/42 選擇 4.1 演算法的描述方式 4.2 變數的運用範圍 (Scope of variables) 4.3 if-else敘述 4.4 巢狀if-else敘述 (Nested if statements) 4.5 switch敘述 4.6 條件運算子 (Conditional Operator ?: ) 4.7 goto無條件前往敘述

Transcript of 選擇 - Asia Universitydns2.asia.edu.tw/~wzyang/slides/programming/B/ch04_AEL0080.pdf · 3/42...

  • 1/42

    選選 擇擇

    在正常的情況下,電腦程式的執行在正常的情況下,電腦程式的執行是以敘述的排列次序逐步處理的。是以敘述的排列次序逐步處理的。使用控制架構使用控制架構 (control structures) (control structures) 可以改變這種既定的先後次序,讓可以改變這種既定的先後次序,讓程式得以進行更複雜的運算,或以程式得以進行更複雜的運算,或以更簡潔的指令來實現演算法。更簡潔的指令來實現演算法。

    4

    2/42

    選擇

    4.1 演算法的描述方式 4.2 變數的運用範圍 (Scope of variables) 4.3 if-else敘述 4.4 巢狀if-else敘述 (Nested if statements) 4.5 switch敘述 4.6 條件運算子 (Conditional Operator ?: ) 4.7 goto無條件前往敘述

  • 3/42

    演算法的描述方式

    演算法 (algorithms) 是執行工作的詳細步驟。 電腦只能對以演算法式 (algorithmic) 的指令進行處理,無法直接處理直覺式 (intuitive) 的指令。

    所有的演算法都可以用循序 (sequence),選擇 (selection) 以及重複 (repetition) 三種流程控制架構 (control structures) 完整描述。

    4/42

    演算法的三種描述方式

    1. 虛擬程式碼 (pseudocode)

    2. 公式 (formula)

    3. 流程圖 (flowchart)

  • 5/42

    求平均值的流程圖

    計算 Sum

    輸入 x, y

    開始

    結束

    輸出

    6/42

    常用的流程圖符號

  • 7/427/42

    複合敘述 (compound statement) 將數個敘述 (statements) 以大括號 {} 包起來所形成的單元。

    複合敘述又稱為區塊 (block)。複合敘述和單一敘述在語法上佔有同等的地位。以包含兩個敘述的複合敘述為例,其語法為:

    { 敘述_1 敘述_2} 或{敘述_1敘述_2

    }

    8/428/42

    變數的運用範圍(Scope of variables)

    {int x = 2;{

    int x = 0;int y = 5;...// 在這裡x,y都適用,...// 但x為重新定義的整數且其初值為0。

    }... // 這裡y已不適用,x則回到2的預設值。

    }... // 在這裡x和y都為未定義,無法使用。

  • 9/429/42

    C++ 的兩種選擇語法

    if-else敘述 (if-else statement)

    switch敘述 (switch statement)

    10/4210/42

    if-else 敘述

    敘述_2

    條件式

    敘述_1

    false

    trueif (條件式) 敘述_1 else 敘述_2 敘述_2

    條件式

    敘述_1

    false

    trueif (條件式) 敘述_1 else 敘述_2

  • 11/4211/42

    條件式

    所有的 C++ 表達式 (expression) 都可以當做條件式。

    使用關係表達式或邏輯運算式是語意比較直接的寫法:

    x > 0 x == y y 0)

     ? >  �  ? <  ?  ? >=  �

    12/4212/42

    程式 CharInput.cpp 使用了 if-else 敘述與使用者互動:

    // CharInput.cpp#include using namespace std;// ------ 主程式 --------------------int main(){char ch;cout ch;if ( ch == 'Y'||ch == 'y' )

    cout

  • 13/4213/42

    操作過程

    請輸入一個字元 (Y/N):Y您輸入了 yes.

    14/4214/42

    if 敘述

    false

    true條件式

    敘述_1

    if (條件式) 敘述_1

    false

    true條件式

    敘述_1

    if (條件式) 敘述_1

  • 15/4215/42

    巢狀 if-else 敘述(Nested if-else statements)

    可分為下列兩種:

    – 置入的if-else敘述位於if和else之間

    – 置入的if-else敘述位於 else 之後

    16/4216/42

    巢狀 if-else 敘述(Nested if-else statements)-1/2

    敘述_2

    條件_2

    敘述_1

    條件_1

    敘述_3

    false

    true

    true

    false

    if (條件_1) if (條件_2) 敘述_1 else 敘述_2 else 敘述_3

  • 17/4217/42

    巢狀 if-else 敘述(Nested if-else statements)-2/2

    敘述_2

    條件_2

    敘述_1

    條件_1

    敘述_3

    falsetrue

    true

    false

    if (條件_1) 敘述_1 else if (條件_2) 敘述_2 else 敘述_3

    18/4218/42

    if-else 敘述可能的錯誤

    被解釋為

    if (條件_1) if (條件_2)

    敘述_1 else

    敘述_2

    if (條件_1) {

    if (條件_2) 敘述_1

    else 敘述_2

    }

  • 19/4219/42

    計算二次常係數方程式兩個根的程式Root2.cpp

    // Root2.cpp#include using namespace std;// ------ 主程式 --------------------int main(){float a, b, c, D;cout a;cout b;cout c;if ( a == 0.0 )

    if (b == 0.0)cout

  • 21/4221/42

    cout

  • 23/4223/42

    if-else 鏈(if-else chain) 有時又稱為else-if鏈;或else-if架構 (else-if construct)。

    敘述_2

    條件_2

    敘述_1

    條件_1

    敘述_3

    falsetrue

    true

    false

    條件_3

    敘述_4

    falsetrue

    if (條件_1) 敘述_1 else if (條件_2) 敘述_2 else if (條件_3) 敘述_3 else 敘述_4

    24/4224/42

    範例程式 檔案 Tax.cpp // Tax.cpp#include using namespace std;// ------ 主程式 --------------------int main(){float GIncome, Tax;cout

  • 25/4225/42

    else if (GIncome < 890000.0)Tax = GIncome * 0.13 - 23100;

    else if (GIncome < 1780000.0)Tax = GIncome * 0.21 - 94300;

    else if (GIncome < 3340000.0)Tax = GIncome * 0.3 - 254500;

    else Tax = GIncome * 0.4 - 588500;

    cout

  • 27/4227/42

    switch 敘述 完整的 switch 敘述包括switch,case,default,break 四個關鍵字。

    敘述_2敘述_1

    整數表達式

    敘述_3

    值_2值_1

    switch (整數表達式) { case 值_1: 敘述_1 break; case 值_2: 敘述_2 break; default: 敘述_3 }

    28/4228/42

    範例程式 Seasons.cpp 依使用者輸入的月份判斷該月份所屬的季節,程式內應用了switch敘述的特性。

    // Seasons.cpp#include using namespace std;// ------ 主程式 --------------------int main(){

    int Month;cout 12)

    cout

  • 29/4229/42

    else{

    cout

  • 31/4231/42

    操作結果

    狀況一 狀況二

    請輸入一個月份: 55月是春季

    請輸入一個月份: 99月是秋季

    32/4232/42

    範例程式 CharTest.cpp 檢查使用者輸入的字母是子音還是母音

    // CharTest.cpp#include using namespace std;// ------ 主程式 --------------------int main(){

    char C;cout 122)

    cout

  • 33/4233/42

    cout

  • 35/4235/42

    條件運算子

    條件式 ? 表達式_1 : 表達式_2;例如,結合等號成為指派敘述

    (assignment expression),可以寫成c = a > b ? a : b ;

    36/4236/42

    範例程式 Condition.cpp 使用條件運算子以判斷數值大小和奇偶數

    // Condition.cpp#include using namespace std;// ------ 主程式 --------------------int main(){float a, b;int N;// --- (1) -------------------------cout

  • 37/4237/42

    endl;cout a;cout b;cout

  • 39/4239/42

    goto 無條件前往敘述

    40/4240/42

  • 41/4241/42

    範例程式 SeasonsGoTo.cpp 用 goto 敘述代替 if-else 敘述

    // SeasonsGoTo.cpp#include using namespace std;int main() {

    int Month;cout 12){

    cout