目录

if-then statement

使用if-then语句,我们可以在一行中编写CoffeeScript的if语句。 它由一个布尔表达式后跟then关键字组成,后跟一个或多个语句。 当给定的布尔表达式为true时,将执行这些语句。

语法 (Syntax)

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

if expression <b class="notranslate">then</b> Statement(s) to be executed if expression is true

例子 (Example)

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

name = "Ramu"
score = 60
if score>40 then console.log "Congratulations you have passed the examination"

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

c:\> coffee -c if_then_example.coffee

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

// Generated by CoffeeScript 1.10.0
(function() {
  var name, score;
  name = "Ramu";
  score = 60;
  if (score > 40) {
    console.log("Congratulations you have passed the examination");
  }
}).call(this);

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

c:\> coffee if_then_example.coffee

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

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