Coffeescript-switch-statement-in-coffeescript

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

CoffeeScript-switchステートメント

*switch* ステートメントを使用すると、変数を値のリストと等しいかどうかをテストできます。 各値は「*ケース*」と呼ばれ、オンになっている変数はスイッチケースごとにチェックされます。 JavaScriptの *switch* の構文は次のとおりです。
switch (expression){
   case condition 1: statement(s)
   break;

   case condition 2: statement(s)
   break;

   case condition n: statement(s)
   break;

   default: statement(s)
}

JavaScriptでは、各スイッチケースの後に、 break ステートメントを使用する必要があります。 誤って break ステートメントを忘れると、あるスイッチケースから別のスイッチケースに落ちる可能性があります。

CoffeeScriptのSwitchステートメント

CoffeeScriptは、 switch-when-else 句の組み合わせを使用してこの問題を解決します。 ここに、caseステートメントが続くオプションのswitch式があります。

各caseステートメントには、 whenthen の2つの句があります。 when の後に条件が続き、 then の後に、その特定の条件が満たされた場合に実行されるステートメントのセットが続きます。 最後に、デフォルト条件のアクションを保持するオプションの else 句があります。

構文

以下に、CoffeeScriptの switch ステートメントの構文を示します。 括弧なしで式を指定し、適切なインデントを維持することでcaseステートメントを分離します。

switch expression
   when condition1 then statements
   when condition2 then statements
   when condition3 then statements
   else statements

流れ図

CoffeeScript switchステートメント

次の例は、CoffeeScriptでの switch ステートメントの使用法を示しています。 このコードを switch_example.coffee という名前のファイルに保存します

name="Ramu"
score=75
message = switch
   when score>=75 then "Congrats your grade is A"
   when score>=60 then "Your grade is B"
   when score>=50 then "Your grade is C"
   when score>=35 then "Your grade is D"
   else "Your grade is F and you are failed in the exam"
console.log message
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c switch_exmple.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var message, name, score;

  name = "Ramu";

  score = 75;

  message = (function() {
    switch (false) {
      case !(score >= 75):
        return "Congrats your grade is A";
      case !(score >= 60):
        return "Your grade is B";
      case !(score >= 50):
        return "Your grade is C";
      case !(score >= 35):
        return "Your grade is D";
      default:
        return "Your grade is F and you are failed in the exam";
    }
  })();

  console.log(message);

}).call(this);

ここで、*コマンドプロンプト*を再度開き、CoffeeScriptファイルを次のように実行します-

c:\> coffee switch_exmple.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

Congrats your grade is A

when句の複数の値

また、スイッチケースでコンマ()を使用して区切ることにより、単一のwhen句に複数の値を指定することもできます。

次の例は、 when 句に複数の値を指定してCoffeeScript switchステートメントを記述する方法を示しています。 このコードを switch_multiple_example.coffee という名前のファイルに保存します

name="Ramu"
score=75
message = switch name
   when "Ramu","Mohammed" then "You have passed the examination with grade A"
   when "John","Julia" then "You have passed the examination with grade is B"
   when "Rajan" then "Sorry you failed in the examination"
   else "No result"
console.log message
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c switch_multiple_example.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var message, name, score;

  name = "Ramu";

  score = 75;

  message = (function() {
    switch (name) {
      case "Ramu":
      case "Mohammed":
        return "You have passed the examination with grade A";
      case "John":
      case "Julia":
        return "You have passed the examination with grade is B";
      case "Rajan":
        return "Sorry you failed in the examination";
      default:
        return "No result";
    }
  })();

  console.log(message);

}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee switch_multiple_example.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

You have passed the examination with grade A