Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Java Control Flow Statements

Conditional Statements in Java

In programming, decision-making statements help the computer choose what to do based on conditions. It’s like making choices in real life.

For example, if it’s raining, you take an umbrella. Otherwise, you don’t.

In Java, we use the following decision-making statements:

if Statement

It checks a condition. If true, it runs the code inside.

Example:


public class MasterBackend {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18) {
            System.out.println("You can vote.");
        }
    }
}

If age is 18 or more, it prints "You can vote."

If-Else Statement

The if-else statement is used to check a condition and execute different blocks of code based on whether the condition is true or false.

Syntax:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

Example:

public class MasterBackend {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18) {
            System.out.println("You can vote.");
        } else {
            System.out.println("You cannot vote.");
        }
    }
}

Explanation:

  1. The program checks if the value of age is greater than or equal to 18.

  2. If the condition is true, it prints "You can vote."

  3. If the condition is false, it prints "You cannot vote."

If-Else If-Else Statement

The if-else if-else statement is used when multiple conditions need to be checked sequentially.

Syntax:

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if none of the conditions are true
}

Example:

public class MasterBackend {
    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("Grade: F");
        }
    }
}

Explanation:

  1. The program checks multiple conditions for marks.

  2. If marks is 90 or above, it prints "Grade: A".

  3. If marks is between 75 and 89, it prints "Grade: B".

  4. If marks is between 50 and 74, it prints "Grade: C".

  5. If none of the conditions match, it prints "Grade: F".

If else is the one of the control flow statement. here we can regulate the flow of the statement which will be executed. where ever we need to take decision we go for else.

Common Mistakes in If-Else Statements

A common mistake in if statements is using the assignment operator = instead of the comparison operator ==. This results in a compile-time error.

Example of a Mistake:

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

        if (a = b) { // Compilation Error
            System.out.println("Equal");
        } else {
            System.out.println("Unequal");
        }
    }
}

Explanation:

  1. The statement if (a = b) results in a compile-time error because = is an assignment operator, not a comparison operator.

  2. The correct way to write this condition is if (a == b), which correctly compares the two values.

  3. Always use == for comparisons, otherwise, it will lead to compilation errors.

If-Else Without Braces

When there is only a single statement inside an if or else block, the braces {} can be omitted.

Example:

public class MasterBackend {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18)
            System.out.println("You can vote.");
        else
            System.out.println("You cannot vote.");
    }
}

Explanation:

  1. Since there is only one statement inside both the if and else blocks, we do not need to use braces {}.

  2. However, using braces is recommended for better readability and to avoid potential errors when adding more statements in the future.

Switch-Case Statement

The switch-case statement is used when a variable is compared against multiple possible values. It provides a cleaner alternative to multiple if-else if statements.

Syntax:

switch (variable) {
    case value1:
        // Code to execute if variable == value1
        break;
    case value2:
        // Code to execute if variable == value2
        break;
    default:
        // Code to execute if no case matches
}

Example:

public class MasterBackend {
    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;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Invalid day");
        }
    }
}

Explanation:

  1. The variable day is checked against multiple cases.

  2. If day matches a case value, the corresponding block executes.

  3. The break statement prevents fall-through to the next case.

  4. The default block executes if no case matches.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board