instruction if imbriquée dans java

Il est toujours légal d'imbriquer des instructions if-else, ce qui signifie que vous pouvez utiliser une instruction if ou else if dans une autre instruction if ou else if.

Syntaxe

La syntaxe pour un if ... else imbriqué est la suivante -

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
   if(Boolean_expression 2) {
      // Executes when the Boolean expression 2 is true
   }
}

Vous pouvez nicher else if...elsede la même manière que nous avons imbriqué l' instruction if .

Exemple

public class Test {

   public static void main(String args[]) {
      int x = 30;
      int y = 10;

      if( x == 30 ) {
         if( y == 10 ) {
            System.out.print("X = 30 and Y = 10");
         }
      }
   }
}

Cela produira le résultat suivant -

Production

X = 30 and Y = 10
java_decision_making