Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java if Keyword
Usage and Examples



if Keyword in Java

The if keyword in Java is your first step into controlling the flow of your program. It allows your code to make decisions — a fundamental aspect of programming logic. Whenever you want something to happen only when a specific condition is true, if is what you use.

Syntax of if Statement

if (condition) {
    // code to execute if condition is true
}

The condition inside the parentheses must evaluate to a boolean value — true or false. If the result is true, the code block runs. If it's false, the block is skipped.

Example 1: Basic if Statement

public class IfExample1 {
    public static void main(String[] args) {
        int age = 20;

        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        }

        System.out.println("Check completed.");
    }
}
You are eligible to vote.
Check completed.

Explanation:

Here, the condition age >= 18 is true because age is 20. Therefore, the message inside the if block is printed. The program then continues to the next line after the block.

Example 2: Condition Fails

public class IfExample2 {
    public static void main(String[] args) {
        int age = 16;

        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        }

        System.out.println("Check completed.");
    }
}
Check completed.

Explanation:

This time, age is 16. Since age >= 18 is false, the body of the if statement is skipped. The program continues to print Check completed.

Example 3: Using Boolean Variables

public class IfExample3 {
    public static void main(String[] args) {
        boolean isLoggedIn = true;

        if (isLoggedIn) {
            System.out.println("Welcome back, user!");
        }
    }
}
Welcome back, user!

Notice how we used a boolean variable directly in the if condition. If it's true, the block runs. This pattern is common when checking flags or system states.

Curly Braces: Optional for Single Statements

public class IfExample4 {
    public static void main(String[] args) {
        int number = 10;

        if (number > 5)
            System.out.println("Number is greater than 5");

        System.out.println("End");
    }
}
Number is greater than 5
End

When there's only one statement to execute under an if condition, Java lets you skip the curly braces. But be cautious — for readability and maintainability, it's usually better to keep them.

Example 5: Nested if Statements

public class IfExample5 {
    public static void main(String[] args) {
        int marks = 85;

        if (marks >= 50) {
            if (marks >= 80) {
                System.out.println("Excellent score!");
            }
        }
    }
}
Excellent score!

Nested if statements are useful when you want to validate one condition only after another one is true. In this case, the program first checks if the score is at least 50, then checks if it’s 80 or more.

Common Mistake: Semicolon After if

if (x > 10);
    System.out.println("This will always run!");

A semicolon directly after if ends the statement prematurely. The following block will run regardless of the condition — a classic beginner mistake.

Best Practices for Using if in Java



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M