Java Nested-if-else-statement .

In this session i have discussed nested if-else statement in Java .


NESTED-IF-ELSE-Statement :

Java allows the nesting of if-else statement means you can use the if-else with in if statement It is also decision making and branching statement .

If-Else-statement-syntax :

CSharp Nested if else image

Example : Write a java program that check the userid or password using nested if else statement .

class Hello{ 
         
      public static void main(String args[]){  
          int uid = 10, pwd = 20;
          if (uid == 10)
          {
               if (pwd == 20){
   
                    System.out.println("Login successfull");
               else{

                   System.out.println("Invalid password");
               }
          }
          else{

               System.out.println("Invalid UserID");
          } 
      }  
}  
    OUTPUT : Login successfull
    


Example : Write a java program that check the maximum number among three number using nested if-else statement .

 class Hello{ 
         
      public static void main(String args[]){  
            int x=20 ,y=10,z=50;
            if(x>y){
                
                if(x>z){
              
                    System.out.println(x);
                }
                else{
  
                    System.out.println(z);
                }
                
            }
            else{

                if(y>z){
                
                    System.out.println(y);
                }
                else{
       
                    System.out.println(z);
                }
                
            }
      }
}
 
    
    OUTPUT : 50
    


Welcome to AndroidATPoint.

back image                     next image