If 這個、Else If 那個、Else 其他情況
若要加入更多邏輯,可以依照 if, else if, else 的模式撰寫。你可以視控制邏輯需要,加入任意多個 else if。
if(condition1) {
code if condition1 is true
} else if(condition2) {
code if condition2 is true
} else {
code if both are false
}
本練習屬於課程
R 金融中級
練習說明
- 延伸上一個範例,填入空格以完成以下邏輯:
- 若
micr小於55,印出"Buy!" - 否則若大於等於
55且micr小於75,印出"Do nothing!" - 否則印出
"Sell!"
- 若
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
micr <- 105.67
# Fill in the blanks
if( micr < 55 ) {
print("Buy!")
} else if( ___ ){
print("Do nothing!")
} ___ {
___
}