Skip to Content

If Else-If statement Example

  1. /*
  2.   If Else-If statement Example
  3.   This Java Example shows how to use if else-if statement in Java program.
  4. */
  5.  
  6. public class IfElseIfElseExample {
  7.  
  8.   public static void main(String[] args) {
  9.    
  10.     /*
  11.      * If Else-if statement is used to execute multiple of actions based upon
  12.      * multiple conditions.
  13.      * Sysntax of If Else-If statement is
  14.      *
  15.      * if(<condition1>)
  16.      *   statement1
  17.      * else if(<condition2>)
  18.      *   statement2
  19.      * ..
  20.      * else
  21.      *   statement3
  22.      *
  23.      * If <condition1> is true, statement1 will be executed, else if <condition2>
  24.      * is true statement2 is executed and so on. If no condition is true, then else
  25.      * statement will be executed.
  26.      */
  27.      
  28.      int i = 10;
  29.      
  30.      if(i > 100)
  31.        System.out.println("i is grater than 100");
  32.      else if(i > 50)
  33.        System.out.println("i is grater than 50");
  34.      else
  35.        System.out.println("i is less than 50");
  36.   }
  37. }
  38.  
  39. /*
  40. Output would be
  41. i is less than 50
  42. */