Groovy-nested-if-statement

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

Groovy-ネストされたIfステートメント

場合によっては、複数のifステートメントを相互に埋め込む必要があります。

この声明の一般的な形式は-

if(condition) {
   statement #1
   statement #2
   ...
} else if(condition) {
   statement #3
   statement #4
} else {
   statement #5
   statement #6
}

以下は、ネストされたif/elseステートメントの例です-

class Example {
   static void main(String[] args) {
     //Initializing a local variable
      int a = 12

     //Check for the boolean condition
      if (a>100) {
        //If the condition is true print the following statement
         println("The value is less than 100");
      } else
        //Check if the value of a is greater than 5

      if (a>5) {
        //If the condition is true print the following statement
         println("The value is greater than 5 and greater than 100");
      } else {
        //If the condition is false print the following statement
         println("The value of a is less than 5");
      }
   }
}

上記の例では、最初に変数を値12に初期化しています。 最初の if ステートメントでは、 a の値が100より大きいかどうかを確認しています。 そうでない場合は、2番目のforループに入り、 a の値が5より大きいか5より小さいかを確認します。 上記のコードの出力は次のようになります-

The value is greater than 5 and greater than 100