If, If Else, Else If Ladder Syntaxes and Examples in Java
The if statement in Java allows you to control the flow of program execution on the basis of the outcome of an expression or condition. You can either have a single statement or a block of code within if statement body. The if statement is applicable if you want to execute single block of statement on a condition.
Note: that the conditional expression must be a Boolean expression.
Syntax:
if(b)
{
// statements
// if body
}
b must be boolean expression
Example 1:
int i = 10;
if(i)
{
}
Output:-
compile time exception, incompatible types.
Example 2:
int i = 10;
if(i=20)
{
}
Output:-
compile time exception, incompatible types.
Example 3:
int i = 10;
if(i==10)
{
System.out.println("Value of i is "+i);
}
Output:-
Value of i is 10
Example 4:
boolean b = false;
if(b=true)
{
System.out.println("Value of b is true");
}
Output:-
Value of b is true
Example 5:
boolean b = false;
if(b==false)
{
System.out.println("Value of b is false");
}
Output:-
Value of b is false
Note: The curly braces { & } are optional for if statement but a statement after if must not be a declarative statement.
Examples:
if(true);
if(true) System.out.println("valid if");
if(true) int i=10;
if(true){ int i=10;}
If Else Statement in Java
The if else statement is an extension of if statement. If you have two blocks of statements and one of them must be executed then you need if else statement. If the expression in if evaluate to true then the body of if statement will get executed otherwise body of else will get executed.
Syntax:
if(b)
{
//Statements execution if b is true
}
else{
// Statements execution if b is false
}
Example: Find even and odd numbers
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System. in);
System.out.println("Enter a Number");
int num = sc.nextInt();
if(num%2==0)
{
System.out.println("Number is Even");
}else
{
System.out.println("Number is Odd");
}
}
}
Else If Ladder in Java
The if statement and if else statements are used to check the single condition but if you have multiple conditions dependent on each other means only one condition can be true at a time then you should use else if ladder in Java.
Syntax:
if(b1)
{
//Statements execution if b1 is true
}
else if(b2){
// Statements execution if b2 is true
}
else if(b3){
// Statements execution if b3 is true
}
else{
// Statements execute if all of the above conditions b1, b2, b3 becomes false
}
Example: Find the grade by percentage
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a percentage");
float per = sc.nextFloat();
if(per>=70)
{
System.out.println("Distinction");
}else if(per>=60)
{
System.out.println("First Class");
}else if(per>=50)
{
System.out.println("Second Class");
}else if(per>=40)
{
System.out.println("Pass");
}
else
{
System.out.println("Failed");
}
}
}
Nested If Else in Java
The nested if else statement means using if else statement inside if body or else body.
Syntax:
if(b1)
{
//Statements execution if b1 is true
if(nb){
// Statements execution if nb is true
}
else{
// Statements execute if all of the above conditions b1, b2, b3 becomes false
}
}
else{
// Statements execute if all of the above conditions b1, b2, b3 becomes false
}