Paramclasses Academy - Blog

Tutorials Details

Java If-Else and Switch Statements with Practical Examples

Java If-Else and Switch Statements with Practical Examples

October 21, 2026

Introduction

Decision-making is an important part of programming. A Java program often needs to perform different actions depending on a condition.

For example, a program may check whether a student has passed an examination, whether someone is eligible to vote, or which menu option a user has selected.

Java provides conditional statements such as if, if-else, else-if and switch to handle these situations.

In this tutorial, you will learn how these statements work, understand their syntax, and practise them through simple Java programs.

What Are Conditional Statements in Java?

Conditional statements allow a program to choose which block of code to execute based on a condition.

For example:

int age = 20;

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

Output:

You are eligible to vote.

The condition age >= 18 is true, so Java executes the statement inside the if block.

If the condition is false, the block is skipped.

Types of Conditional Statements in Java

The main decision-making statements covered in this tutorial are:

  1. if statement

  2. if-else statement

  3. else-if ladder

  4. Nested if statement

  5. switch statement

Let's understand each one with examples.

1. Java if Statement

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

Syntax:

if (condition) {
    // Code executes when the condition is true
}

Example

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

        if (marks >= 33) {
            System.out.println("You passed the exam.");
        }
    }
}

Output:

You passed the exam.

How It Works

  • Java checks whether marks >= 33.

  • The condition is true because the marks are 75.

  • The program executes the code inside the if block.

If marks were 25, the condition would be false and no message would be printed.

2. Java if-else Statement

The if-else statement allows a program to choose between two alternatives.

If the condition is true, Java executes the if block. Otherwise, it executes the else block.

Syntax:

if (condition) {
    // Code when true
} else {
    // Code when false
}

Example: Pass or Fail

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

        if (marks >= 33) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }
    }
}

Output:

Fail

Since 28 is less than 33, Java executes the else block.

Real-Life Example: Voting Eligibility

int age = 19;

if (age >= 18) {
    System.out.println("Eligible to vote");
} else {
    System.out.println("Not eligible to vote");
}

Output:

Eligible to vote

This program checks the age and prints the appropriate message.

3. Java else-if Ladder

An else-if ladder is useful when a program needs to check multiple conditions.

Java evaluates the conditions from top to bottom. The first true condition's block is executed, and the remaining branches are skipped.

Syntax:

if (condition1) {
    // First condition
} else if (condition2) {
    // Second condition
} else if (condition3) {
    // Third condition
} else {
    // If no condition is true
}

Example: Grade Calculator

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

        if (marks >= 90) {
            System.out.println("Grade A+");
        } else if (marks >= 80) {
            System.out.println("Grade A");
        } else if (marks >= 70) {
            System.out.println("Grade B");
        } else if (marks >= 33) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}

Output:

Grade A

How It Works

  • Java checks whether marks are at least 90. This is false.

  • It checks whether marks are at least 80. This is true.

  • Java prints Grade A and skips the remaining branches.

Important: Put conditions in the correct order. For example, if you check marks >= 33 first, students with 85 marks will match that condition, and the later grade conditions will not be reached.

4. Nested if Statement in Java

A nested if statement means placing one if statement inside another if statement.

It is useful when a second condition should be checked only after a first condition is satisfied.

Example: Login Check

public class Main {
    public static void main(String[] args) {
        boolean usernameCorrect = true;
        boolean passwordCorrect = true;

        if (usernameCorrect) {
            if (passwordCorrect) {
                System.out.println("Login successful");
            } else {
                System.out.println("Incorrect password");
            }
        } else {
            System.out.println("Incorrect username");
        }
    }
}

Output:

Login successful

The program checks the username first. If it is correct, the inner if checks the password.

This example demonstrates the logic only; it is not a secure authentication implementation.

5. Java switch Statement

The switch statement selects a code branch based on the value of an expression. It is especially useful when checking one value against several possible options.

Syntax:

switch (expression) {
    case value1:
        // Code for value1
        break;

    case value2:
        // Code for value2
        break;

    default:
        // Code when no case matches
}

The traditional switch statement supports values such as byte, short, char, int, their corresponding wrapper types, String and enum values.

Example: Day of the Week

public class Main {
    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;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

Output:

Wednesday

Understanding break and default

break: In a traditional switch statement, break exits the switch block. Without it, execution can continue into the following case labels.

default: This branch runs when no case matches. It is optional.

Example Without break

int number = 1;

switch (number) {
    case 1:
        System.out.println("One");
    case 2:
        System.out.println("Two");
        break;
    default:
        System.out.println("Other");
}

Output:

One
Two

Because there is no break after case 1, execution continues into case 2. This behavior is called fall-through.

6. Modern Switch Syntax in Java

Modern Java also supports switch expressions and arrow labels. Arrow-style cases do not fall through into the next case.

Example

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

        String result = switch (day) {
            case 1 -> "Monday";
            case 2 -> "Tuesday";
            case 3 -> "Wednesday";
            default -> "Other day";
        };

        System.out.println(result);
    }
}

Output:

Tuesday

This syntax is available in modern Java versions, including Java 21. It can return a value directly and avoids the traditional fall-through behavior.

Difference Between if-else and switch

if-else switch
Checks Boolean conditions Compares a selector against cases
Supports ranges and complex conditions Best for distinct matching values
Useful for conditions such as marks >= 80 Useful for options such as menu choices
Can evaluate multiple different expressions Traditional switch uses one selector expression
Often suitable for ranges and eligibility checks Often clearer for fixed options

When Should You Use Each?

Use if-else when checking ranges, combining conditions or evaluating expressions such as age >= 18.

Use switch when selecting among a set of distinct values, such as menu options, day numbers or status codes.

Complete Practical Java Program

The following example uses if-else to determine a student's result and a switch statement to display a selected subject.

public class Main {
    public static void main(String[] args) {
        int marks = 76;
        int subjectChoice = 2;

        if (marks >= 90) {
            System.out.println("Grade A+");
        } else if (marks >= 80) {
            System.out.println("Grade A");
        } else if (marks >= 33) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }

        switch (subjectChoice) {
            case 1:
                System.out.println("Java");
                break;
            case 2:
                System.out.println("Cloud Computing");
                break;
            case 3:
                System.out.println("Artificial Intelligence");
                break;
            default:
                System.out.println("Invalid subject");
        }
    }
}

Output:

Pass
Cloud Computing

The if-else ladder evaluates the student's marks, while switch selects the subject corresponding to the chosen number.

Common Mistakes Beginners Should Avoid

  1. Using = instead of a comparison: Use == when comparing primitive values for equality.

  2. Incorrect condition order: In an else-if ladder, an earlier broad condition can prevent later branches from running.

  3. Forgetting break: In a traditional switch statement, this can cause fall-through.

  4. Ignoring the default branch: Consider handling unexpected values when appropriate.

  5. Using non-Boolean conditions: An if condition must evaluate to a Boolean value in Java.

  6. Adding unnecessary nested conditions: Prefer a clear else-if ladder when it makes the logic easier to follow.

Frequently Asked Questions (FAQs)

1. What is an if statement in Java?

An if statement executes a block of code only when its condition is true.

2. What is the difference between if and if-else?

An if statement has a block that runs when the condition is true. An if-else statement provides an alternative block when the condition is false.

3. What is an else-if ladder?

An else-if ladder checks multiple conditions in sequence and executes the first matching branch.

4. What is a switch statement in Java?

A switch statement selects a branch based on a selector value and matching case labels.

5. Why is break used in a switch statement?

In traditional switch syntax, break exits the switch statement and prevents execution from falling through to subsequent cases.

6. What is the default case?

The default branch handles values that do not match any case. It is optional.

7. Can switch work with String values?

Yes. Java supports String values in switch statements.

8. Which is better: if-else or switch?

Neither is always better. Use if-else for ranges and complex conditions, and switch for clear selection among distinct values.

Practice Exercises

Try these exercises to improve your understanding:

  1. Write an if statement that prints a message if a number is positive.

  2. Use if-else to check whether a number is even or odd.

  3. Create an else-if ladder to assign grades based on marks.

  4. Use nested if statements to check two Boolean conditions.

  5. Write a traditional switch program that displays a day based on a number from 1 to 7.

  6. Modify a switch program to handle invalid choices using default.

  7. Rewrite a simple switch statement using modern arrow syntax.

Challenge: Create a menu-based calculator that selects addition, subtraction, multiplication or division using a switch statement. Use if-else to handle division by zero.

Conclusion

Java conditional statements help programs make decisions based on values and conditions. The if, if-else, else-if and nested if statements are useful for evaluating conditions, while switch provides a clear way to select among multiple alternatives.

Practise the examples and exercises to build a strong foundation before moving on to Java loops.