CodeGym /Courses /JAVA 25 SELF /Conditional statement: if else

Conditional statement: if else

JAVA 25 SELF
Level 3 , Lesson 1
Available

1. The if else statement

Programs wouldn’t be very useful if they did the same thing regardless of changing external circumstances. A program needs to adapt to different situations and do one thing in some cases and another in others.

In Java this is implemented using the conditional statement — the keywords if and else, which allow you to execute different blocks of statements depending on whether a condition is true.

The conditional statement consists of three parts: the condition, statement 1, and statement 2. If the condition is true, statement 1 runs; otherwise, statement 2 runs. The two statements are never executed at the same time. The general form looks like this:


if (condition)
   statement1;
else
   statement2;
Conditional statement if-else

if reads as “if”, and else as “otherwise”. In plain language, it can be written like this:


If condition is true, then
   execute statement1;
otherwise
   execute statement2;
Conditional statement if-else in plain language

Examples:

// The following will be printed: You are still a child

int age = 17;
if (age < 18)
   System.out.println("You are still a child");
else
   System.out.println("You are already an adult");
// The following will be printed: Warm

int temperature = 5;
if (temperature < 0)
   System.out.println("It is freezing outside");
else
   System.out.println("Warm");
// The following will be printed: You can take the driving test

int age = 18;
if (age == 18)
   System.out.println("You can take the driving test");
else
   System.out.println("You can save up for a car");

2. Block of statements

If, when a condition is met (or not met), you want your program to execute several statements, you can combine them into a block of statements.

To combine statements into a block, you need to wrap them in curly braces. Here’s what it looks like in general:


{
   statement1;
   statement2;
   statement3;
}

A block can contain any number of statements — even zero.

Examples combining the if-else statement and a block of statements:

// The following will be printed: You are still a child. Don’t argue with adults

int age = 17;
if (age < 18)
{
   System.out.println("You are still a child");
   System.out.println("Don’t argue with adults");
}
else
{
   System.out.println("You are already an adult");
   System.out.println("Kids these days");
}
// The following will be printed: Warm

int temperature = 5;
if (temperature < 0)
{
   System.out.println("It is freezing outside");
   System.out.println("Put on your hat");
}
else
   System.out.println("Warm");
// An empty block of statements will be executed. Nothing will be printed. 

int age = 21;
if (age == 18)
   System.out.println("You can get your driver's license");
else
{
}

3. Short form of the if statement

If the code in the else block is missing, you can omit it. Then the if statement looks like this:


if (condition)
{
    statement1;
}
Conditional statement if

Example:

int age = 19;
if (age >= 18)
{
    System.out.println("You are of legal age!");
}

How does this work?

If the condition is true, the block of code executes. If it’s false, nothing happens — the program just continues.

The else block exists in the program, but it’s empty (there are no statements between the braces). You can simply remove it — the program won’t change.

4. Combining conditions: else if

Sometimes you need to do more than a simple yes/no check — you need to choose from several options. This is where the else if chain helps:

if (condition1)
{
    // Executes if condition1 is true
}
else if (condition2)
{
    // Executes if condition2 is true and condition1 is false
}
else
{
    // Executes if none of the above conditions matched
}

Example:

int hour = 13;
if (hour < 12)
{
    System.out.println("Good morning!");
}
else if (hour < 18)
{
    System.out.println("Good afternoon!");
}
else
{
    System.out.println("Good evening!");
}

What’s happening here?

  • If the time is before 12 — it’s morning.
  • If it’s from 12 to 18 — it’s afternoon.
  • If it’s later — it’s evening.
  • The program chooses only ONE option! As soon as a condition matches, the rest are not even checked.

5. Schematic illustration: branching flowchart

Here’s what it looks like as a flowchart:

And if there are several conditions:

This is very much like branches in tabletop role-playing games: go left — one thing happens, right — another, straight — something else!

6. What kinds of conditions can you test?

Inside the parentheses after if there must be a BOOLEAN expression — whose result is true or false.

Basic comparison operators:

Operator Meaning Example
==
Equal to
x == 5
!=
Not equal to
x != 10
>
Greater than
x > 0
<
Less than
age < 18
>=
Greater than or equal to
score >= 100
<=
Less than or equal to
count <= 50

Examples of conditions:

if (number == 10)
if (age != 18)
if (temperature > 36)
if (balance <= 0)

Note: = (a single sign) is assignment. == (two signs) is comparison. This is one of the most common mistakes among beginners! If you accidentally write:

if (x = 5) // ERROR!

— the compiler will complain — and rightly so.

7. Working with strings in conditions

Comparison using == in Java works correctly for integers. Floating-point numbers are best not compared using ==, and strings must not be compared using == — for strings, use the equals() method, which compares the contents of strings (not references).

We’ll discuss the subtleties of string comparison later. Here’s how to compare strings correctly — see the example below:

String password = console.nextLine();
if (password.equals("qwerty"))            // call equals() and pass the second string to it
{
    System.out.println("You are logged in!");
}

8. Particulars and nuances:

Curly braces: necessary or not?

If there is only one statement after if or else, the curly braces can be omitted:

if (age > 18)
    System.out.println("You are over 18!");

// But it's safer to ALWAYS write them:
if (age > 18)
{
    System.out.println("You are over 18!");
}

Tip: always use braces. This will save you from a bunch of errors, especially as the code grows.

Logical errors: be careful with your conditions!

If you write this:

if (age > 18)
    System.out.println("You are over 18!");
    System.out.println("Welcome!");

The compiler will interpret the code like this:

if (age > 18)
{
    System.out.println("You are over 18!");
}

System.out.println("Welcome!");

The second line will always run — you need braces so both lines are inside the “branch”.

1
Task
JAVA 25 SELF, level 3, lesson 1
Locked
Buying a Movie Ticket 🎬
Buying a Movie Ticket 🎬
1
Task
JAVA 25 SELF, level 3, lesson 1
Locked
Check the weather outside ❄️
Check the weather outside ❄️
1
Task
JAVA 25 SELF, level 3, lesson 1
Locked
Account Unlock 🔒
Account Unlock 🔒
1
Task
JAVA 25 SELF, level 3, lesson 1
Locked
Greeting by Time of Day ☀️
Greeting by Time of Day ☀️
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Muksud Alam Level 4, Bangladesh, Bangladesh
8 June 2026
REALLY REAL WORLD LIKE SUPERNATURAL