Module 1 of 5 · Java Fundamentals Part 2 · Beginner

Control Flow: Making Decisions

Duration: 5 min

What is Control Flow?

Control flow determines the order in which statements are executed in your program. By default, Java executes statements sequentially from top to bottom. However, using control flow statements, you can make your program execute different code based on conditions or repeat code multiple times.

The if Statement

The if statement executes a block of code only if a condition is true. This is the most fundamental decision-making statement in programming.

public class IfStatement {
    public static void main(String[] args) {
        int age = 20;
        
        if (age >= 18) {
            System.out.println("You are an adult");
        }
        
        if (age < 13) {
            System.out.println("You are a child");
        }
    }
}
You are an adult

The if-else Statement

The if-else statement allows you to execute one block if a condition is true, and a different block if it's false. This gives you two paths your program can take.

public class IfElseStatement {
    public static void main(String[] args) {
        int score = 75;
        
        if (score >= 80) {
            System.out.println("Grade: A");
        } else {
            System.out.println("Grade: B or lower");
        }
    }
}
Grade: B or lower

The if-else if-else Statement

When you have multiple conditions to check, use if-else if-else. This allows you to test multiple conditions in sequence.

public class IfElseIfStatement {
    public static void main(String[] args) {
        int score = 75;
        
        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 80) {
            System.out.println("Grade: B");
        } else if (score >= 70) {
            System.out.println("Grade: C");
        } else if (score >= 60) {
            System.out.println("Grade: D");
        } else {
            System.out.println("Grade: F");
        }
    }
}
Grade: C

The switch Statement

The switch statement is useful when you have many conditions based on a single value. It's cleaner than multiple if-else if statements when checking one variable against many values.

public class SwitchStatement {
    public static void main(String[] args) {
        int day = 3;
        String dayName;
        
        switch (day) {
            case 1:
                dayName = "Monday";
                break;
            case 2:
                dayName = "Tuesday";
                break;
            case 3:
                dayName = "Wednesday";
                break;
            case 4:
                dayName = "Thursday";
                break;
            case 5:
                dayName = "Friday";
                break;
            default:
                dayName = "Weekend";
        }
        
        System.out.println("Day: " + dayName);
    }
}
Day: Wednesday

💡 Tip: Always include a break statement after each case to prevent fall-through to the next case. The default case is optional but recommended as a safety net.

Learn more: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

❓ What happens if you forget the break statement in a switch case?

❓ What is a best practice when working with Control Flow?

💡 Tip: Pro Tip: Master Control Flow thoroughly. This foundation is crucial for writing professional Java code.

Continue interactively → Next →

Related Courses