Translate

If else if ladder statement in Java.

If else if ladder statement in Java

 
  • It is used to test the condition.
  • If executes only one condition at a time.
  • The condition which is true first from the top will execute.
import java.util.Scanner;
class Easy
{
 public static void main(String[] args) 
 {
  Scanner in=new Scanner(System.in);
  int no=5;
  if(no>2)//condition true
  {
   System.out.println("Hello1");   
  }
  else if(no<2)//condition false
  {
   System.out.println("Hello2");   
  }
  else if(no==5)//condition true
  {
   System.out.println("Hello3");
  }
  else
  {
   System.out.println("Hello4");
  }
 }
}
/*
### Output ###
Hello1
because it executes only one condition 
which becomes true first from top.
*/
import java.util.Scanner;
class Easy
{
 public static void main(String[] args) 
 {
  Scanner in=new Scanner(System.in);
  float p;//declaring variable for percent
  System.out.println("Enter your percent");
  p=in.nextFloat();
  if(p>=60)
   System.out.println("First div");    
  else if(p>=45)
   System.out.println("Second div"); 
  else if(p>=33)
   System.out.println("Third div");
  else
   System.out.println("Sorry!! you are fail!!"); 
 }
}
/*
### Output ###
Enter your percent
50
Second div
*/
Next Post Previous Post
No Comment
Add Comment
comment url