Groovy - Instruction If imbriquée

Parfois, il est nécessaire d'avoir plusieurs instructions if imbriquées l'une dans l'autre.

La forme générale de cette déclaration est -

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

Voici un exemple d'instruction if / else imbriquée -

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"); 
      }  
   } 
}

Dans l'exemple ci-dessus, nous initialisons d'abord une variable à une valeur de 12. Dans le premier if déclaration, nous voyons si la valeur de a est supérieur à 100. Sinon, nous entrons dans notre deuxième boucle for pour voir si la valeur de a est supérieur à 5 ou inférieur à 5. La sortie du code ci-dessus serait -

The value is greater than 5 and greater than 100