🔀 Control Flow — If/Else, Switch, Loops
Control Flow — If/Else, Switch, Loops: Control flow (if/else, switch, loops) is the mechanism by which a program decides which code runs, when, and how many times based on a cond
Control flow (if/else, switch, loops) is the mechanism by which a program decides which code runs, when, and how many times based on a condition — like a railway switchman who looks at the incoming train and turns the track one way: the same rail system routes the train to a different line depending on the condition, and the decision is set up once but works automatically for every train. But would writing all lines in sequence be enough — why do we need structures like if/else and loops? Because real programs must behave differently depending on the situation and must summarize repetition with a loop instead of writing it by hand hundreds of times; unconditional straight-line code can neither decide nor scale with data. In Java `switch` now approaches Python's `match` and TypeScript's `switch` with pattern matching, but Java's type safety catches wrong-type comparisons in the arms at compile time. For a QA engineer control flow is the heart of test logic: if a loop that asserts each element of a list, or a branch that picks a different validation path based on test data (`if (env == "prod") ... else ...`), is built wrong, the test silently PASSes without checking anything — the most dangerous error type, the false green.
Micro Lab: Code practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Step by Step: Code practice
Identify goal and input
Complete the critical line
Check output or behavior
Read the error message as evidence
Order the code reading and verification flow.
Run the if/else Ladder Live
See the Scanner + if examples from a QA perspective: the incoming score value moves through conditions, and the first true branch produces output.
for and for-each Loops
for Loop Step by Step
`sum` is initialized to 0 before the loop.