<style> .mkd { background-color: rgba(0, 150, 250, .2); } </style> # 選擇性敘述 if ## infro if 顧名思義就是對條件進行判斷,如果符合判斷就執行 ## 先備知識 當我們要進行判斷條件時主要是使用到關係運算子,也就是<、>、==等 當我們判斷一個條件的時候,如果<span class="mkd">條件符合電腦就會回傳true</span>,也就是1 而<span class="mkd">不符合是就會回傳false</span>,也就是0 ```cpp= #include<iostream> using namespace std; int main(){ cout << (2 > 1) << endl;//輸出1 cout << (1 > 2) << endl;//輸出0 return 0; } ``` ## if語法 ```cpp= if(判斷條件A) { 程式碼//條件A成立時執行 } ``` 而if判斷條件就是1和0 如果if後的括號內是1,就執行if內的程式碼 如果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; } ``` ## else if語法 ```cpp= if(判斷條件A) { 程式碼//條件A成立時執行 } else if(判斷條件B) { 程式碼//條件A不成立且條件B成立時執行 } else if(判斷條件B) { 程式碼//條件A、B不成立且條件C成立時執行 } ``` else if的判斷方法和if大致相同 else if必須接在if/else if後 else if可以有兩個以上 else if是在<span class="mkd">前面的判斷都不成立的情況下才會進行判斷</span> 如果前面條件成立,後面的else if 就不會判斷 ```cpp= #include<iostream> using namespace std; int main(){ if(2 > 1){ cout << "2 > 1" << '\n';//輸出2 > 1 } else if(3 > 1){ cout << "3 > 1" << '\n';//不輸出 } 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(判斷條件B) { 程式碼//條件A、B不成立且條件C成立時執行 } else { 程式碼//條件A、B、C都不成立時執行 } ``` else只能接在if或else if後 且只有當前面的條件全都不符合時才會執行 ```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; } ``` ## 小技巧 ### 大括號 如果要執行的程式碼只有一行的化 可以不用加大括號{} 直接寫在if/else if/else後就可以 ```cpp= #include<iostream> using namespace std; int main(){ if(2 > 1) cout << "2 > 1" << '\n';//輸出2 > 1 return 0; } ``` ### 0/1 if的判斷是由0(false)/1(true)決定 所以其實if的判斷不用限制於關係運算子(>、<、==) 只要可以決定0/1就好 所以我們其實也可以搭配邏輯運算子 ```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; } ``` 或是一個布林變數(bool) 0為false 1為true ```cpp= #include<iostream> using namespace std; int main(){ bool flag = 0; int a,b; cin >> a >> b; if(a >= b) flag = 1; if(flag) cout << "a >= b" << '\n'; if(!flag)//複習:!是表示"否",也就是當flag等於0時if內會是1 cout << "a < b" << '\n'; return 0; } ``` 或是一個整數變數(int) 只有變數為0時為false 其他所有數字都是true ```cpp= #include<iostream> using namespace std; int main(){ if(-1) cout << "true" << '\n'; if(3) cout << "true" << '\n'; if(0) cout << "false" << '\n'; return 0; } ``` 甚至不需要是判斷式或變數 而是一個執行式 當一個<span class="mkd">程式成功執行電腦會回傳true</span> 這個寫法雖然在if幾乎等於沒用 但在使用while迴圈的時候常常用到 ```cpp= #include<iostream> using namespace std; int main(){ int n; if(cin >> n)//執行完cin >> n電腦回傳true cout << n << '\n'; return 0; } ``` 如果有寫python的可以注意一下下面這種 在python中不是執行完就回傳true(1) 他的是判斷<span class="mkd">改完的n是不是0</span>(false) ```cpp= #include<iostream> using namespace std; int main(){ int n; cin >> n; if(n *= 2)//執行完n *= 2電腦回傳true cout << n << '\n'; return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up