开始使用免费开始使用

如果这样,否则如果那样,否则就那件事

为了增加更多逻辑,您可以按照 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!"
    • 否则如果大于等于 55micr 小于 75,打印 "Do nothing!"
    • 否则打印 "Sell!"

交互式实操练习

通过完成这段示例代码来试试这个练习。

micr <- 105.67

# Fill in the blanks
if( micr < 55 ) {
    print("Buy!")
} else if( ___ ){
    print("Do nothing!")
} ___ { 
    ___
}
编辑并运行代码