CoffeeScript - if...else 语句
如果指定的布尔表达式为真,则 if 语句执行给定的代码块。 如果布尔表达式为假怎么办?
'if...else' 语句是下一种形式的控制语句,它允许 CoffeeScript 以更受控的方式执行语句。 它将有一个 else 块,当布尔表达式为 false 时执行。
语法
下面给出了 CoffeeScript 中 if-else 语句的语法。 如果给定表达式为真,则执行 if 块中的语句,如果为假,则执行 else 块中的语句。
if expression Statement(s) to be executed if the expression is true else Statement(s) to be executed if the expression is false
流程图

示例
以下示例演示了如何在 CoffeeScript 中使用 if-else 语句。 将此代码保存在名为 if_else_example.coffee 的文件中
name = "Ramu" score = 30 if score>=40 console.log "Congratulations have passed the examination" else console.log "Sorry try again"
打开命令提示符并编译.coffee 文件,如下所示。
c:\> coffee -c if_else_example.coffee
在编译时,它会提供以下 JavaScript。
// Generated by CoffeeScript 1.10.0 (function() { var name, score; name = "Ramu"; score = 30; if (score >= 40) { console.log("Congratulations have passed the examination"); } else { console.log("Sorry try again"); } }).call(this);
现在,再次打开命令提示符并运行 CoffeeScript 文件 −
c:\> coffee if_else_example.coffee
执行时,CoffeeScript 文件产生以下输出。
Sorry try again