目录

unless...then else statement

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

语法 (Syntax)

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

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

例子 (Example)

下面给出了CoffeeScript的unless-then else语句的示例。 将以下示例保存在名为unless_then_else_example.coffee的文件中

name = "Ramu"
score = 60
unless score>=40 then console.log "Sorry try again" else console.log "congratulations."

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

c:\> coffee -c unless_then_else_example.coffee

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

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

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

c:\> coffee unless_then_else_example.coffee

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

congratulations.
↑回到顶部↑
WIKI教程 @2018