Coffeescript-while-untill-variant

提供:Dev Guides
移動先:案内検索

CoffeeScript-whileのuntilバリアント

CoffeeScriptによって提供される until の代替は、 while ループとは正反対です。 ブール式とコードブロックが含まれています。 until ループのコードブロックは、指定されたブール式がfalseである限り実行されます。

構文

以下に、CoffeeScriptのuntilループの構文を示します。

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

次の例は、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"
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c until_loop_example.coffee

コンパイル時に、次のJavaScriptが提供されます。 ここで、結果のJavaScriptコードで until ループが while not に変換されることがわかります。

//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);

次に、*コマンドプロンプト*を再度開き、以下に示すように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