☕ Java

Control Flow Statements in Java

Without control flow, every program would just run from top to bottom in a straight line. Control flow lets your program make decisions, repeat actions, and skip code — the difference between a calculator and an actual application.

if-else — Making Decisions

The if-else block is how your program makes choices. It evaluates a condition (true/false) and runs the matching block. Real apps use this constantly — checking login status, validating inputs, routing requests.
Java
int marks = 75;

if (marks >= 90) {
    System.out.println("Grade: A — Outstanding!");
} else if (marks >= 75) {
    System.out.println("Grade: B — Good job");
} else if (marks >= 60) {
    System.out.println("Grade: C — Passing");
} else {
    System.out.println("Grade: F — Try again");
}
// Output: Grade: B — Good job

switch — Cleaner Multi-Branch Logic

When you're checking one variable against many possible values, switch is cleaner than a chain of if-else if. Think of it as a routing table — "if the value is X, do this; if it's Y, do that."
Java
String day = "Monday";

switch (day) {
    case "Monday":
    case "Tuesday":
    case "Wednesday":
    case "Thursday":
    case "Friday":
        System.out.println("Weekday — back to work");
        break;
    case "Saturday":
    case "Sunday":
        System.out.println("Weekend — relax!");
        break;
    default:
        System.out.println("Unknown day");
}

// Modern Switch Expression (Java 14+) — much cleaner
String type = switch (day) {
    case "Saturday", "Sunday" -> "Weekend";
    default -> "Weekday";
};

for Loop — When You Know the Count

Use a for loop when you know exactly how many times you want to repeat something — printing 10 rows, processing 100 records, iterating over an array.
Java
// Classic for loop
for (int i = 1; i <= 5; i++) {
    System.out.println("Attempt: " + i);
}

// Enhanced for loop (for-each) — cleaner when iterating collections
int[] scores = {85, 92, 78, 95, 88};
for (int score : scores) {
    System.out.println("Score: " + score);
}
// You can't modify the original array with for-each though — use classic for that

while and do-while — When You Don't Know the Count

Use while when you want to keep looping as long as some condition is true. Classic examples: reading user input until they type "quit", retrying a network request until it succeeds, polling a sensor until a threshold is hit. do-while is the same, but it always runs at least once — useful when you need to execute before checking the condition (like showing a menu once before asking "run again?").
Java
// while — checks condition BEFORE each iteration
int attempts = 0;
while (attempts < 3) {
    System.out.println("Trying to connect... attempt " + (attempts + 1));
    attempts++;
}

// do-while — runs FIRST, then checks condition
// Good for: "show menu, then ask if they want to continue"
String input;
do {
    System.out.println("Enter a positive number:");
    input = scanner.nextLine();
} while (Integer.parseInt(input) <= 0);