4.1 程式的基本架構
Sequence structure (循序式結構) , Selection structure (選擇式結構) , Repetition structure (重複式結構)
4.1.2 Selection structure
if, if-else, if-elseif
4.1.3 Repetition structure
do-while loop, while loop, for loop
4.2 條件選擇
4.2.1 關係運算符號
>, <, >=, <=, ==, !=
運算符號(operators)
|
優先順序(priority)
|
( )
|
1
|
*, /, %
|
2
|
+, -
|
3
|
==, <, >, <=, >=, !=
|
4
|
int a = 5, b = 6; bool x = (a == b); // x = 0 bool y = (a >= b); // y = 0 bool z = (a != b); // z = 1 cout << (a < b); // 輸出 1
4.2.2 邏輯運算符號
!, &&, ||
- AND運算: 有一個運算式為 0(false)則 A && B 為 0(false)
- OR運算: 有一個運算式為 1(true)則 A || B 為 1(true)
在混和運算式中盡量使用小括號來表示運算的順序
4.2.3 if 敘述
#include <cstdlib> //system #include <iostream> //cout, cin using namespace std; int main() { int input; cout << "請輸入一個整數 : "; cin >> input; if (input >= 0) { cout << input << "是正數。 \n"; } system("PAUSE"); return 0; }
4.2.4 if-else 敘述
#include <cstdlib> //system #include <iostream> //cout, cin using namespace std; int main() { int number; cout << "請輸入一個整數 : "; cin >> number; if (number >= 0) { cout << number << " 是正整數。\n"; } else { cout << number << " 是負整數。\n"; } system("PAUSE"); return 0; }
4.2.5 if-else if 敘述
#include <cstdlib> //system #include <iostream> //cout, cin using namespace std; int main() { int number; cout << "請輸入一個整數 : "; cin >> number; //將鍵盤輸入的數字存入number中 if(number > 0) //判斷number中的數字是否大於0 { cout << number << " 大於 0\n"; } else if(number < 0) //判斷number中的數字是否小於0 { cout << number << " 小於 0\n"; } else { cout << number << " 等於0\n"; } system("PAUSE"); return 0; }
4.2.6 巢狀 if 敘述
#include <cstdlib> //system #include <iostream> //cout, cin using namespace std; int main() { int year; cout << "請輸入西元年 : "; cin >> year; //讀入西元年 if (year % 4 != 0) //判斷是否為4的倍數,若是4的倍數則回傳0 { cout << year << "不是閏年\n"; //不是4的倍數 ,就不是閏年 } else if (year % 100 == 0) //判斷是否為100的倍數,若不是1000的倍數則回傳0 { if (year % 400 == 0) cout << year << "是閏年\n"; //是4、100與400的倍數的是閏年 else cout << year << "不是閏年\n"; //是4與100的倍數但不是400的倍數的不是閏年 } else cout << year << "是閏年\n"; //是4的倍數但不是100的倍數就是閏年 system("PAUSE"); return 0; }
4.2.7 switch 敘述
- 條件運算式 的值必須是整數 int, short, long, char
- 執行完任何 case 敘述後,會繼續向下執行,所以要用 break 來中斷 switch 敘述
switch (條件運算式) { case 數值1; 敘述區1; break; case 數值2; 敘述區; break; default; 敘述區n; }
判斷輸入
#include <cstdlib> //system #include <iostream> //cout, cin using namespace std; int main() { cout << "確定 Y/N ?"; char letter; cin >> letter; switch (letter) { case 'Y': //若輸入Y從此執行 case 'y': //若輸入y從此執行 cout << "Yes"; //輸出 break; //結束switch case 'N': //若輸入N從此執行 case 'n': //若輸入n從此執行 cout << "No"; //輸出 break; //結束switch default: //輸入字不在範圍內時,所進行的處理 cout << "輸入錯誤"; //輸出 } system("PAUSE"); return 0; }
4.2.8 條件運算符號
條件運算式 ? 敘述1 : 敘述2
- 當條件運算式成立執行敘述1,不成立執行敘述2
判斷正負數
#include <cstdlib> //system #include <iostream> //cout, cin using namespace std; int main() { int num; cout << "請輸入整數 : "; cin >> num; cout << ((num >=0) ? "是整數\n" : "是負數\n"); system("PAUSE"); return 0; }程式輸出
=================================================================================
習題:
選擇題:
- <、>、>=、<=、==、!= 等符號稱為 關係運算符號
- 關係運算式的值為真,則該運算式的傳回值為 1
- == 是關係運算式中,比較兩運算式是否相等的運算符號
- != 是關係運算式中,比較兩運算式是否不相等的運算符號
- &&、||、! 是邏輯運算符號
- 在if敘述中,判斷變數letter的值是否等於大寫'A'或小寫'a'的條件運算式是 letter = 'A' || letter = 'a'
- 在if敘述中,判斷變數number的值是否介於-10到10的條件運算式是 number >= -10 && number <= 10
- 關係運算式 x != y 的值等於關係運算式 x>y || x<y 的值
- 關係運算式 x >= y 的值等於關係運算式 x>y || x==y 的值
- 假設x = 5, y = 3 則運算式 x == 5 || y > 3 的值是 1
實作題:
沒有留言:
張貼留言