Java Else If Statement

The Java Else If Statement is an extension to Java If Else Statement, and it is very useful when we have to compare 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 in Java programming:

Java Else If Statement Syntax

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

if (condition 1)
          statement 1
else if (condition 2)
          statement 2
else if (condition 3)
          statement 3
      ...........
else if (condition N)
          statement N
else
    default statements

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

x = 18, y = 14

Condition 1: if (x != y) //TRUE

Condition 2: else if (x > y) //TRUE

In these situations, statements under the Condition 1 will execute because ELSE IF conditions will only execute if it’s previous IF or ELSE IF statement fails.

Java Else If Statement example

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

Please refer to Java If Else Statement and Nested If statement articles in Java Programming

// Example for Java Else If Statement

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: From the below screenshot, you can observe that we entered the totalmarks = 580. Here, the first If condition is TRUE. So, the statements inside the first If block will be executed.

Java Else If Statement 1

We are going to enter totalmarks = 510 means the first IF condition is FALSE. It will check the Java else if (totalmarks >= 480), which is TRUE. So, it will print the statements inside this block. Although else if (Totalmarks >= 400) condition is TRUE, but 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 first IF condition, else if (totalmarks >= 480) is FALSE. So, It will check the else if (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