目录

if-then...else statement

if-then语句后跟一个可选的else语句,该语句在布尔表达式为false时执行。 使用if-then ... else语句,我们可以在一行中编写if ... else语句。

语法 (Syntax)

以下是CoffeeScript中if-then...else语句的语法。

if expression <b class="notranslate">then</b> Statements (for true condition) else Statements (for false condition)

例子 (Example)

下面给出了CoffeeScript的if-then...else语句的示例。 将此代码保存在名为if_then_else_example.coffee的文件中

name = "Ramu"
score = 30
if score>=40 then console.log "Congratulations" else console.log "Sorry try again"

打开command prompt并编译.coffee文件,如下所示。

c:\> coffee -c if_then_else_example.coffee

在编译时,它为您提供以下JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var name, score;
  name = "Ramu";
  score = 30;
  if (score >= 40) {
    console.log("Congratulations");
  } else {
    console.log("Sorry try again");
  }
}).call(this);

现在,再次打开command prompt并运行CoffeeScript文件 -

c:\> coffee if_then_else_example.coffee

执行时,CoffeeScript文件生成以下输出。

Sorry try again
↑回到顶部↑
WIKI教程 @2018