Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java Else If Statement

by suresh

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 Flow Chart

The following picture will show the flow chart behind this Else If in Java

Flow Chart for Java Else If Statement

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.

Java Else If Statement 2

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.

Java Else If Statement 3

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

Java Else If Statement 4

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy