目录

unless...else statement

就像if else语句一样,我们在CoffeeScript中也有一个unless else语句。 它包含一个布尔表达式,一个unless块和一个else块。 如果给定的表达式为false ,则执行unless块,如果为true,则执行else块。

语法 (Syntax)

下面给出了CoffeeScript中unless else语句的语法。

unless expression
   Statement(s) to be executed if the expression is false
else
   Statement(s) to be executed if the expression is true

流程图 (Flow Diagram)

除非另有声明

例子 (Example)

以下示例演示了CoffeeScript中except unless-else语句的用法。 将此代码保存在名为unless_else_example.coffee的文件中

name = "Ramu"
score = 60
unless score>=40
  console.log "Sorry try again"
else
  console.log "Congratulations you have passed the exam"

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

c:\> coffee -c unless_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 you have passed the exam");
  }
}).call(this);

现在,再次打开command prompt并运行CoffeeScript文件,如下所示。

c:\> coffee unless_else_example.coffee

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

Congratulations you have passed the exam
↑回到顶部↑
WIKI教程 @2018