Nested If in java

A nested if statement is an if-else statement with another if statement as the if body or the else body. Here conditions are evaluated from top to bottom . If  1st condition is true then the fist if statement will execute and if 1st condition is false then else if condition will check and rest of the process will move on one by one . If none of the above condition is match then else part will execute . 

Syntax :

          If(1st condition)
          {
                  // execute code
          }
          else if (2nd condition)
          {
              // execute code
          }
          .
          .
          .
        else if (nth condition)
        {
          // execute code
        }
       else
        {
            //  this part will be executed if none of the conditions in 1st condition,   

           // 2nd condition,…nth condition are true.
        }


 Example : 

 public class DemoNestedIf
  {
     public static void main(String args[])
     {
        
         int i=0;

         if(i==1)
         {
           System.out.println("Anupam");
         }
         else if(i==2)
         {
             System.out.println("Singh");
          }
         else
         {
             System.out.println("Nothing happen");
         }
     }
}

       
Output :         Nothing happen


Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        YouTube : Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Constructor in Java