Java Certification, Programming, JavaBean and Object Oriented Reference Books
Java if Statement

This tutorial will take you step by step through the process of understanding and using the if statement. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

Example 1 : simple form of if statement

  
class If1{ 
   public static void main(String args[]){
      int i = 11;
      int j = 20;

      if(i>j) 

// if i > j this statement executes 
        System.out.println(" i > j"); 

// if i <= j controls goes to this statement and 
// continues; this is what happens with the above values of i and j.
      System.out.println(" i <= j ");
    }
}

Try different values of i and j and see the output.

Example 2 : if/else form

If you want several statements to execute after if or/and else, use blocks of statements

  
class If2{ 
   public static void main(String args[]){
      int i = 11;
      int j = 20;


      if(i>j) 
//if i > j this whole block executes
        {
           System.out.println(" i = "+i+" j= "+j); 
           System.out.println(" i > j");
        }


      else 
// if i <= j this block will execute
        {
        System.out.println(" i = "+i+" j= "+j); 
        System.out.println(" i <= j ");
        }
    }
}

Try different values of i and j and see the output.

Example 3 : Nested if

Which else goes with which if ?

  
class If3{ 
   public static void main(String args[]){
      int i = 29;
      int j = 20;

      if(i==29){
        if(j<20) System.out.println(" i = 29 and j < 20");
        if(j>20) System.out.println(" i = 29 and j > 20");

// this else is associated with if(j>20) above
        else System.out.println(" i = 29 and j not greater than 20");
      }

// this else statement which is outside the block
// is associated with if(i==29)
        else System.out.println("i is not equal to 29");

        System.out.println("control goes to this statement and continues");
   }
}

Run this program and try different values of i and j and see the output.
.

Example 4 : if-else-if

if statements are executed downwards and as soon as one of the conditions is true the statement associated with that if is executed and the rest of the if statements is bypassed. If none of the conditions is true the final else is executed.

  
class Test{ 
   public static void main(String args[]){
      int grade = 71;

      if(grade >= 90)System.out.println("grade A");
      else if(grade >= 80) System.out.println("grade B");
      else if(grade >= 70) System.out.println("grade C");
      else if(grade >=60 ) System.out.println("grade D");
      else System.out.println("grade F");
      System.out.println("proram continues here");
   }
}

with grade = 71 the output is grade C and control goes to the statement after the last else "System.out.println("proram continues here")...."

Put different values for the variable grade and examine the output.

Return to : Java Programming Hints and Tips