Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

else

The else Statement

Use an else statement to define a block of code that will be executed if the condition is false.

Syntax

if (condition) {

// block of code to be executed if the condition is true

} else {  

// block of code to be executed if the condition is false

}

Example

int time = 20;
if (time < 18) {

    System.out.println(“Good day.”);

} else {

  System.out.println(“Good evening.”);
}
// Outputs “Good evening.”

Example explained

In the example above, the time (20) is greater than 18, making the condition false. As a result, we proceed to the else block and print “Good evening” to the screen. If the time were less than 18, the program would print “Good day”.