Java Control Flow

Control the flow of your program with if-else, switch, loops, and break/continue statements.

Control Flow statements decide the direction in which your Java program executes. They help you make decisions and repeat actions.

1. Conditional Statements

A. if Statement

Executes a block only when the condition is true.

Try example:

public class IfDemo {
    public static void main(String[] args) {

        int age = 20;

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

Executes one block when the condition is true, otherwise another block executes.

Try example:

public class IfElseDemo {
    public static void main(String[] args) {

        int time = 20;

        if (time < 18) {
            System.out.println("Good day");
        } else {
            System.out.println("Good evening");
        }
    }
}
C. if-else-if Ladder

Used when multiple conditions must be checked in sequence.

Try example:

public class ElseIfDemo {
    public static void main(String[] args) {

        int marks = 72;

        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 75) {
            System.out.println("Grade B");
        } else if (marks >= 60) {
            System.out.println("Grade C");
        } else {
            System.out.println("Grade D");
        }
    }
}
D. switch Statement

Useful when comparing one variable against multiple values.

Try example:

public class SwitchDemo {
    public static void main(String[] args) {

        int day = 4;

        switch (day) {
            case 1 -> System.out.println("Monday");
            case 2 -> System.out.println("Tuesday");
            case 3 -> System.out.println("Wednesday");
            case 4 -> System.out.println("Thursday");
            default -> System.out.println("Weekend");
        }
    }
}

2. Looping Statements

A. for Loop

Best when the number of repetitions is known.

Try example:

public class ForLoopDemo {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {
            System.out.println("Count: " + i);
        }
    }
}
B. while Loop

Runs as long as the condition remains true.

Try example:

public class WhileDemo {
    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {
            System.out.println("i = " + i);
            i++;
        }
    }
}
C. do-while Loop

Executes at least once even if the condition is false initially.

Try example:

public class DoWhileDemo {
    public static void main(String[] args) {

        int i = 1;

        do {
            System.out.println("i = " + i);
            i++;
        } while (i <= 5);
    }
}

3. Jump Statements

A. break Statement

Immediately exits a loop or switch.

Try example:

public class BreakDemo {
    public static void main(String[] args) {

        for (int i = 1; i <= 10; i++) {
            if (i == 5) break;
            System.out.println(i);
        }
    }
}
B. continue Statement

Skips the current iteration and continues with the next one.

Try example:

public class ContinueDemo {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {
            if (i == 3) continue;
            System.out.println(i);
        }
    }
}
Sidebar Ad Space
Advertisement Space