Java If Else Statement with Explanations

Java If Else Statement with Explanations

If-Else statement is also known as a conditional statement, used to perform a task depending on whether a given condition is true or false.

Here task can be a single statement or a group of statements.

Syntax:

if(condition)
    statement1;
else 
    statement2;

The above syntax is an example of an if-else statement in java. It is not necessary to have an else block along with an if block. A program can have only an if block alone without an else block.

There are various types of if statements in Java.

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

1. Java if Statement

If statement is use to check the condition, if condition become true or we can say satisfy, then execute the block inside the if block.

Syntax of if-block

if (condition) {
  // statement inside if 
}

Program to check if age is eligible for vote

public class Main {  
public static void main(String[] args) {  
    int age=25;  
    if(age>18){  
        System.out.print("Eligible for vote");  
    }  
}  
}  

Output

Eligible for vote

2. if-else statement

if-else statement in java is used to test the condition. If condition true then executes the if block and if condition not satisfied then run the else block.

Means for all condition that is not satisfied by if condition, else block will run.

Syntax of if-else statement

if (condition) {
  // statement inside if 
}else{
  // statement inside else
}

Even odd program in java

public class Main {  
public static void main(String[] args) {  
    int num=10;  
    if(num%2==0){  
        System.out.println("Number is Even");  
    }else{  
        System.out.println("Number is Odd");  
    }  
}  
}  

Output

Number is Even

3. if-else-if ladder

if-else-if ladder statement is required when we required mulliple condition to check,

i.e. one if condition is not enough to handle the conditions.

Syntax of if-else-if

if (condition1) {
  // statement inside if 
}else if(condition2){
  // statement inside elseif
}else if(condition3){
  // statement inside elseif
}else if(condition4){
  // statement inside elseif
}else{
  // statement inside else
}

Program to check the grade of student

public class Main {  
public static void main(String[] args) {  
    int marks=85;  
    if(marks<30){  
        System.out.println("You are fail");  
    }  
    else if(marks>=30 && marks<45){  
        System.out.println("C grade");  
    }  
    else if(marks>=45 && marks<60){  
        System.out.println("B grade");  
    }  
    else if(marks>=60 && marks<75){  
        System.out.println("A grade");  
    }else if(marks>=75 && marks<100){  
        System.out.println("A+ grade");  
    }else{  
        System.out.println("Invalid Valid");  
    }  
}  
}  

Output

A+ grade

4. Nested if statement

Nested If statement is used if we need to check conditions inside if means a if block is inside if block.

if(condition1){    
     // statement inside if   
          if(condition2){  
             // statement inside if
    }    
} 

Program to Check game eligibility

public class Main {    
public static void main(String[] args) {    
    int age=22;  
    int weight=70;    
    if(age>=18){    
        if(weight>60){  
            System.out.println("Eligible to Participate in Game");  
        }    
    }    
}
}  

Output

Eligible to Participate in Game