Courses / Complete Java Course by codesofrohan / Conditional Statements / If Statement in Java

If Statement in Java

Imagine you are entering a movie theatre. There is a simple rule:

If your ticket is valid, allow the person to enter.

But if the ticket is not valid, the security guard simply does not allow the entry process to continue.

This is the basic idea behind an if statement in Java.

An if statement allows your program to make a decision. Java checks a condition, and if that condition is true, it executes a particular block of code.

What is an if Statement?

The basic syntax is:

if (condition) {
    // code to execute
}

There are three important parts here:

  • if tells Java that we want to make a decision.

  • The condition inside ( ) is what Java checks.

  • The code inside { } executes only when the condition is true.

So you can think of it like this:

Condition
   ↓
Is it true?
   ↓
 Yes ──→ Execute the code
 No  ──→ Skip the code

Let's take a very simple example.

if (4 < 5) {
    System.out.println("Condition is true");
}

Java checks:

4 < 5

Since 4 is less than 5, the condition is true.

Therefore, Java executes the statement inside the curly braces.

Output:

Condition is true

Now let's change the condition:

if (4 < 2) {
    System.out.println("Condition is true");
}

This time:

4 < 2

is false.

So Java simply skips the code inside the if block.

There is no output from that println statement.

That's the most important thing to remember about if:

The code inside an if block executes only when its condition is true.


A Real-World Example

Let's say we are creating a system for an age-restricted service.

The requirement is simple:

If the user is under 18, show a warning message.

We don't need to do anything for adults yet.

We can write:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        System.out.println("Enter your age");

        int age = scan.nextInt();

        if (age < 18) {
            System.out.println("You are underage");
        }
    }
}

Suppose the user enters:

16

Java checks:

16 < 18

The result is true.

Therefore:

You are underage

is displayed.

Now suppose the user enters:

35

Java checks:

35 < 18

The result is false.

Therefore, the if block is skipped.

Nothing is printed from the if block.

This is exactly the situation where if is useful: we want to perform an action only when a particular condition is satisfied.


Code Outside the if Block

There is one more thing worth understanding.

Look at this:

if (age < 18) {
    System.out.println("You are underage");
}

System.out.println("Program ended");

If the user enters 16, we get:

You are underage
Program ended

If the user enters 35, we get:

Program ended

Why?

Because "Program ended" is outside the if block.

The if statement controls only the code between its curly braces.

So:

if (condition) {
    // controlled by if
}

System.out.println("Program ended");

The second statement executes regardless of whether the condition is true or false.


When Should You Use if?

Use an if statement when you have a condition and you want something to happen only when that condition is true.

For example:

If age is below 18
→ Show warning
If temperature is high
→ Show a message
If the user is a VIP
→ Show VIP information

The important idea is simple:

if gives your program the ability to say: "If this condition is true, do this."

Key Takeaway

An if statement does not provide an alternative action when the condition is false. It simply skips its block.

if (condition) {
    // executes only when condition is true
}

That is the foundation of conditional decision-making in Java.

🔖 Bookmark saved successfully!