目录

直到变种的同时(until variant of while)

CoffeeScript提供的until替代方案与while循环完全相反。 它包含一个布尔表达式和一段代码。 只要给定的布尔表达式为false,就执行until循环的代码块。

语法 (Syntax)

下面给出了CoffeeScript中until循环的语法。

until expression
   statements to be executed if the given condition Is false

例子 (Example)

以下示例演示了CoffeeScript中until循环的用法。 将此代码保存在名为until_loop_example.coffee的文件中

console.log "Starting Loop "
count = 0  
until count > 10
   console.log "Current Count : " + count
   count++;
console.log "Set the variable to different value and then try"

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

c:\> coffee -c until_loop_example.coffee

在编译时,它为您提供以下JavaScript。 在这里,您可以观察到until循环被转换为while not在生成的JavaScript代码中。

// Generated by CoffeeScript 1.10.0
(function() {
  var count;
  console.log("Starting Loop ");
  count = 0;
  while (!(count > 10)) {
    console.log("Current Count : " + count);
    count++;
  }
  console.log("Set the variable to different value and then try");
}).call(this);

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

c:\> coffee until_loop_example.coffee

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

Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Set the variable to different value and then try 
↑回到顶部↑
WIKI教程 @2018