Groovy-nested-switch-statement
提供:Dev Guides
Groovy-入れ子になったSwitchステートメント
*switch* ステートメントのネストされたセットを持つことも可能です。 ステートメントの一般的な形式は以下に示されています-
switch(expression) {
case expression #1:
statement #1
...
case expression #2:
statement #2
...
case expression #N:
statement #N
...
default:
statement #Default
...
}
以下は、ネストされたswitch文の例です-
class Example {
static void main(String[] args) {
//Initializing 2 variables i and j
int i = 0;
int j = 1;
//First evaluating the value of variable i
switch(i) {
case 0:
//Next evaluating the value of variable j
switch(j) {
case 0:
println("i is 0, j is 0");
break;
case 1:
println("i is 0, j is 1");
break;
//The default condition for the inner switch statement
default:
println("nested default case!!");
}
break;
//The default condition for the outer switch statement
default:
println("No matching case found!!");
}
}
}
上記の例では、最初に変数をaの値2に初期化しています。 次に、変数 a の値を評価する switch ステートメントがあります。 変数の値に基づいて、ステートメントの関連するケースセットを実行します。 上記のコードの出力は次のようになります-
i is 0, j is 1