Sitemap
Farel Alghazali

Farel Alghazali

Undergraduate students that very interested in information technology

Understanding if-else statement in Java

3 min readOct 16, 2025

--

Press enter or click to view image in full size
Photo by Luca Bravo on Unsplash

Java is one of the most popular programming languages ​​in the world. It’s widely studied by students and beginners alike as they begin to learn programming. In this article, we’ll explore one of the language’s fundamental concepts: branching. So what is branching?

What is branching?

Branching is a programming language feature that allows lines of code to be executed if a condition is met. Example: You want to say “Good morning” when it is morning, and if it isn’t morning, you just say “Hello.” That is just a simple example of branching. In Java, we have several ways to do branching, and in this article we will learn how to use if, if-else, & else-if statements.

If statement

If you want to execute a line of code (or an action) when a condition is met, you can use if. Example: if today is rain, i will bring an umbrella. Example code:

boolean rain = true;

if(rain){
System.out.println("Bring an umbrella");
}

System.out.println("Go to school");

In the code above, if rain is true screen will print “Bring an umbrella” and then print “Go to school”. If rain is not true screen just will print “Go to school”. We can conclude that the code inside the if statement is only will be execute when the condition is met, and if not, the code will be skipped and continue to the line below it.

If-Else statement

In the if statement we just can execute one action when the condition is met, but now we will learn to make 1 more action if the condition is not met, with if-else statement. Example: if the room is dark, turn on the lamp; if room isn’t dark, turn off the lamp.

boolean dark = false;

if(dark){
System.out.print("Turn on the lamp");
} else {
System.out.print("Turn off the lamp");}

In the code above, when the condition met, we will print “Turn on the lamp”, but if the condition is not met, we will print “Turn off the lamp”. So, if the condition isn’t met, we will execute the second action, and the code will not be skipped as in if statement.

Else-If statement

If you want to make more than 2 actions & conditions, you can use else-if statement. Example : you want to check the age range of users. Under 12 years old are kids; over 12 and under 20 are teenagers; and over 20 ae adults.

int age = 17;

if(age < 12){
System.out.println("kids");
} else if(age >= 12 && age <= 20){
System.out.println("teenagers");
} else if (age > 20) {
System.out.println("adults");}

In the code above, we will check the conditions line by line. If the first condition is not met, then we check the second condition. If the second condition is not met, we move on to the third condition. And if all conditions are not met, the code will be skipped, therefore we can use else to handle if all conditions are not met.

Get Farel Alghazali’s stories in your inbox

Join Medium for free to get updates from this writer.

So that’s our discussion about branching in Java with if, if-else, & else-if statements. We can conclude that every type of of branching has the same principle: execute when the condition is met. Every type of branching also has different ways of working and uses, so branching according to your needs.

--

--

Farel Alghazali
Farel Alghazali

Written by Farel Alghazali

Undergraduate students that very interested in information technology