# if 條件式 ###### tags: `程式設計` `條件式` ```cpp if(條件...) { 執行程式A } else { 執行程式B } ``` 程式執行時,會先判斷條件是否符合,若符合就執行A,不符合就執行B 如果有多個事情要判斷,就可以使用``else if`` ```cpp if(條件...) { 執行程式A } else if(條件...) { 執行程式B } else if(條件...) { 執行程式C } . .. ... ``` :exclamation:在這裡輸入的條件裡面要特別注意一件事: 如果要判斷兩變數是否相等,必須用``==``而非`=` 以下來舉例子示範 --- ## 例題 >[a003: 兩光法師占卜術](https://zerojudge.tw/ShowProblem?problemid=a003 "點選看題目") ```cpp= #include <iostream> using namespace std; main() { int a, b,c; cin>>a>>b; c = (a*2+b)%3; if(c==0) cout<<"普通"; else if (c==1) cout<<"吉"; else if (c==2) cout<<"大吉"; } ``` 這一題是基本的條件判斷題目 注意地 7 行和第 8 行 **第 7 行的``=``是指定** **第 8 行的``==``是數學的等於(=)**
×
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