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!")
} ___ {
___
}