Java Else If Statement

The Java Else If Statement is an extension to the If Else Statement, which is very useful when comparing several conditions. We can also use the Nested If statement to perform the same. However, as the number of conditions increases, code complexity will also rise. Let us see the syntax of the Else if.

Java Else If statement Syntax

The syntax of the Else If statement in Java Programming language is as follows:

if(condition1)
          statement1
else if (condition2)
          statement2
      ...........
else if(conditionN)
          statementN
else
    default statements

Java Else If statement handles multiple conditions effectively by performing them sequentially. It will check for the first condition. If the expression is evaluated to TRUE, then it will run the statements present in that block. While the expression is evaluated as FALSE, it will check the Next one (Else If condition) and so on. There will be situations where condition1, condition2 is TRUE, for example:

x = 18, y = 14

Condition1: if(x != y) //TRUE

Condition2: else if(x > y) //TRUE

In these situations, code under Condition1 will execute because ELSE IF conditions will only execute when its previous IF statement fails.

Java Else If Statement Example

This program allows the user to enter his / her grand total (a total of six subject marks). By using the Java Else if statement, we are going to calculate whether he/she is eligible for a scholarship or not.

Please refer to the If Else and Nested If articles in Programming.

package ConditionalStatements;

import java.util.Scanner;

public class ElseIfStatement {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int totalmarks;
		sc = new Scanner(System.in);		
		//Imagine you have 6 subjects and Grand total is 600 
		System.out.println(" Please Enter your Total Marks: ");
		totalmarks = sc.nextInt();
		
		if (totalmarks >= 540) {
			System.out.println("Congratulations"); 
			System.out.println("You are eligible for Full Scholarship");
		}
		else if (totalmarks >= 480)  {
			System.out.println("Congratulations"); 
			System.out.println("You are eligible for 50 Percent Scholarship");
		}
		else if (totalmarks >= 400)  {
			System.out.println("Congratulations"); 
			System.out.println("You are eligible for 10 Percent Scholarship");
		}
		else  {
			System.out.println("You are Not eligible for Scholarship"); 
			System.out.println("We are really Sorry for You");
		}
	}
}

OUTPUT 1: In the below Java Else If statement screenshot, we entered the totalmarks = 580. Here, the first If condition is TRUE. So, the code inside the first If block will be executed.

Java Else If Statement 1

We will enter totalmarks = 510 means the first IF condition is FALSE. It will check the Java else if statement (totalmarks >= 480), which is TRUE. So, it will print the statements inside this block. Although Totalmarks >= 400 expression result is TRUE, it will not check this condition.

Please Enter your Total Marks: 
510
Congratulations
You are eligible for 50 Percent Scholarship

OUTPUT 3: Totalmarks = 450 means the first IF condition, and totalmarks>= 480 is FALSE. So, It will check the totalmarks>= 401, which is TRUE. So, it will print the statements inside this block.

Please Enter your Total Marks: 
450
Congratulations
You are eligible for 10 Percent Scholarship

OUTPUT 4: We entered totalmarks= 380 means all the IF conditions Fail. So, It will print the statements inside the else block.

Please Enter your Total Marks: 
380
You are Not eligible for Scholarship
We are really Sorry for You