# 簡介 判斷式的功能是對<font color="#EB6E41">其條件進行判斷</font>,<font color="#EB6E41">符合條件便會執行</font>,不符合則略過。 # if ```cpp= if(判斷條件A) { 程式碼 //條件A成立時執行。 } ``` `if`判斷條件就是`1`和`0`,如果`if`後的括號內是`1`,就執行`if`內的程式碼,反之為`0`就不執行。 ```cpp= #include<iostream> using namespace std; int main(){ if(2 > 1){ cout << "2 > 1" << '\n'; //輸出2 > 1。 } if(1 > 2){ cout << "1 > 2" << '\n'; //不輸出。 } return 0; } ``` :::info ::: spoiler `true` or `false`。 如果條件符合電腦會回傳`true`,也就是`1`,反之則會回傳`false`,也就是0。 ```cpp= #include<iostream> using namespace std; int main(){ cout << (2 > 1) << endl; //輸出1。 cout << (1 > 2) << endl; //輸出0。 return 0; } ``` ::: :::info :::spoiler 布林值。 `if`的判斷是布林值,所以可以完成以下操作。 ```cpp= if(true){ //此程式碼必執行。 } ``` ::: # else if ```cpp= if(判斷條件A) { 程式碼 //條件A成立時執行。 } else if(判斷條件B) { 程式碼 //條件A不成立且條件B成立時執行。 } else if(判斷條件C) { 程式碼 //條件A、B不成立且條件C成立時執行。 } ``` `else if`必須接在`if/else if`後,可有兩個以上,在前面的判斷都不成立的情況下才會進行判斷,<font color="#EB6E41">如果前面條件成立,後面的else if 就不會判斷。</font> ```cpp= #include<iostream> using namespace std; int main(){ //例子1 if(2 > 1){ cout << "2 > 1" << '\n'; //輸出2 > 1。 } else if(3 > 1){ cout << "3 > 1" << '\n'; //不輸出。 } //例子2 if(1 > 2){ cout << "1 > 2" << '\n'; //不輸出。 } else if(3 > 1){ cout << "3 > 1" << '\n'; //輸出3 > 1。 } else if(4 > 1){ cout << "4 > 1" << '\n'; //不輸出。 } return 0; } ``` # else ```cpp= if(判斷條件A) { 程式碼 //條件A成立時執行。 } else if(判斷條件B) { 程式碼 //條件A不成立且條件B成立時執行。 } else if(判斷條件C) { 程式碼 //條件A、B不成立且條件C成立時執行。 } else { 程式碼 //條件A、B、C都不成立時執行。 } ``` <font color="#EB6E41">`else`只能接在`if`或`else if`後,且當前面的條件全都不符合時才會執行。</font> ```cpp= #include<iostream> using namespace std; int main(){ if(1 > 2){ cout << "1 > 2" << '\n'; //不輸出。 } else if(3 > 4){ cout << "3 > 4" << '\n'; //不輸出。 } else { cout << "else" << '\n'; //輸出else。 } return 0; } ``` :::info :::spoiler 大括號的刪減。 如果要執行的程式碼只有一行的話,可以不用加大括號`{ }`,直接寫在if/else if/else後就可以。 ```cpp= #include<iostream> using namespace std; int main(){ if(2 > 1) cout << "2 > 1" << '\n'; //輸出2 > 1。 return 0; } ``` 但助教覺得不好看。 ::: # 其他用法 ## 搭配邏輯運算子 ```cpp= #include<iostream> using namespace std; int main(){ int n; cin >> n; if(n > 10 && n % 3 == 0){ //n大於10 且 n除3餘0。 cout << "三的倍數且大於10" << '\n'; } if(n > 10 || n % 3 == 0){ //n大於10 或是 n除3餘0。 cout << "三的倍數或大於10" << '\n'; } return 0; } ``` :::info :::spoiler 用集合表示。 這就是交集和聯集,在此類型的邏輯推演常用到的圖表稱為文氏圖。 ![image](https://hackmd.io/_uploads/ByqgFsrTC.png) ###### 圖很陽春,不要介意。 ::: ## 搭配布林數 ```cpp= #include<iostream> using namespace std; int main(){ bool flag = 0; int a,b; cin >> a >> b; if(a >= b){ flag = 1; } if(flag){ //當flag為1時執行。 cout << "a >= b" << '\n'; } if(!flag){ //!是表示"否",也就是當flag等於0時if內會是1。 cout << "a < b" << '\n'; } return 0; } ``` ## 搭配整數 只有變數為`0`時為`false`,其他所有數字都是`true`。 ```cpp= #include<iostream> using namespace std; int main(){ if(-1){ //非0,執行。 cout << "true" << '\n'; } if(3){ //非0,執行。 cout << "true" << '\n'; } if(0){ //為0,不執行。 cout << "false" << '\n'; } return 0; } ``` ## 搭配執行式 當程式成功執行,電腦會回傳`true`。 ```cpp= #include<iostream> using namespace std; int main(){ int n ; if(cin >> n){ //執行完 cin >> n 電腦回傳true 。 cout << n << '\n' ; } return 0 ; } ``` # 例題 ###### 有什麼題目沒提到或有新增的煩請留言告知。 [a020: ㄑㄧˊ 數?](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a020) [a021: 三人行必有我師](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a021) [a022: 上學去吧!](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a022) [a023: 文文的求婚--續集 (1 行版)](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a023) [a031: 山六九之旅](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a031) [a034: 我愛偶數](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a034) [a043: 棄保效應](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a043) [a044: 閏年或平年](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a044) [a063: 指考的學長加油!!-指考學長求女友](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a063) [a125: 閏年發大財](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a125) [a126: 愛吃包子的維尼](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a126) [a165: 0927初階班作業](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a165) [a268: Nikkiyaya's math questions](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a268) [a417: 理想氣體好好玩](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a417) [a455: pF Scheduled Routine](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a455) [a724: B. 小青蛙吃蒼蠅](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a724) ###### 都看到這了難道不按個愛心支持一下嗎?