if else if ladder statement in Java.
In this section let's see about if-else ladder in Java.
Syntax;
- This is used to test the condition.
- If executes only one condition at a time.
- The condition which will be true first from the top that will be executed.
Example 1:
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.
Example 2:
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 /
For Videos Join Our Youtube Channel: Join Now
Thank you!