Configuration

Night mode

The else if Statements

Introduction To Java Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions

This is the EXLskills free and open-source Introduction To Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

Basics

The else if Statements

The else if statement takes the if statement functionality one step further. Instead of having two outcomes, the programmer can create as many outcomes as they like, each one with its own expression. Take a look at the use of the else if statement below.

Each else if statement has its own expression that returns true or false. Only the FIRST expression that evaluates to "true" is executed. As we see above, if the expression exampleVariableOne &lt; 10 returns a value of "true", only the statements within the curly brackets of the if statement are executed, and all the statements that follow are ignored. If none of the expressions evaluate to "true", the statement inside the curly braces of the else statement are executed. Remember these vital rules when working with if statements:

The if statement may contain zero or one else statement that must come after any else if expressions.

The if statement may have zero or more else if expressions that must come before the else statement.

Once the expression in an else if statement evaluates to true, all the remaining else if and else expressions are ignored.