Skip to main content

Command Palette

Search for a command to run...

Conditional Statements in Java – Complete Guide with Examples

Updated
4 min readView as Markdown

Conditional statements in Java are used to make decisions in a program. They allow the program to execute different blocks of code based on different conditions.

In simple words: Conditional statements control the flow of execution based on conditions.


1. if Statement

The if statement executes a block of code only if the condition is true.

Syntax:

if (condition) {
    // code to execute
}

Example:

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

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

Explanation: If age is greater than or equal to 18, the message will be printed.


2. if-else Statement

The if-else statement executes one block if the condition is true and another block if the condition is false.

Syntax:

if (condition) {
    // if block
} else {
    // else block
}

Example:

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

        if (number % 2 == 0) {
            System.out.println("Even number");
        } else {
            System.out.println("Odd number");
        }
    }
}

3. else-if Ladder

Used when multiple conditions need to be checked.

Syntax:

if (condition1) {
    // code
} else if (condition2) {
    // code
} else if (condition3) {
    // code
} else {
    // default code
}

Example:

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

        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 75) {
            System.out.println("Grade B");
        } else if (marks >= 50) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}

4. Nested if Statement

An if inside another if is called nested if.

Example:

public class NestedIfExample {
    public static void main(String[] args) {
        int age = 25;
        boolean hasLicense = true;

        if (age >= 18) {
            if (hasLicense) {
                System.out.println("You can drive");
            } else {
                System.out.println("You need a license");
            }
        } else {
            System.out.println("You are underage");
        }
    }
}

5. switch Statement

The switch statement is used when we have multiple options based on one variable.

Syntax:

switch (variable) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

Example:

public class SwitchExample {
    public static void main(String[] args) {
        int day = 3;

        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

Note: break is important in switch; otherwise, all cases after the matched case will execute.


6. Ternary Operator (Short if-else)

This is a shortcut for if-else.

Syntax:

condition ? value_if_true : value_if_false;

Example:

public class TernaryExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        int max = (a > b) ? a : b;
        System.out.println("Maximum is: " + max);
    }
}

Comparison Table

Statement Used For
if Check single condition
if-else Two conditions
else-if Multiple conditions
nested if Condition inside condition
switch Multiple values of one variable
ternary Short if-else

Real-Life Example

public class LoginSystem {
    public static void main(String[] args) {
        String username = "admin";
        String password = "1234";

        if (username.equals("admin") && password.equals("1234")) {
            System.out.println("Login successful");
        } else {
            System.out.println("Invalid username or password");
        }
    }
}

Conclusion

Conditional statements are very important in Java because they control the decision-making of a program. Without conditional statements, a program cannot make decisions.

Flow of conditional statements:

if → if-else → else-if → nested if → switch → ternary

To become strong in Java, you must practice problems based on conditional statements like:

  • Even/Odd

  • Largest of 3 numbers

  • Grade system

  • Calculator

  • Login system


More from this blog

Amit singh's blog

235 posts