Command Palette

Search for a command to run...

Phase 2Beginner3 of 17 in Core Java

Control Flow

Make Java programs decide and repeat: if/else, the ternary operator, switch statements and expressions, every loop, break, continue and labels.

Until now every program ran top to bottom, one line after another, exactly once. Real programs need two more powers: choosing (do this or that, depending on a value) and repeating (do this ten times, or until something happens). Together these are called control flow, because they control the order in which statements run.

This phase covers every control-flow tool in the language: if/else, the ? : operator, the classic switch statement, the modern switch expression with arrow labels (Java 14), while, do-while, the classic for loop, the enhanced for loop, and break/continue with labels. For each one you'll see what the compiler checks, what the bytecode looks like, and the bugs people really write.

The last two topics put it all together: nested loops that draw patterns, and a set of classic problems (FizzBuzz, primes, digit tricks) solved step by step. These are the exact building blocks the DSA course (/dsa) assumes you can write without thinking.

0/10 · 0%
10 topics ~5 h 43 code blocks & diagrams
Start with the first topic
1
2.1

Making Decisions with if and else

An if statement runs a block of code only when a boolean condition is true; else and else if add the other paths. Java checks the conditions top to bottom, runs the first branch that matches, and refuses any condition that isn't a real boolean.

33 min 5 code practice

2
2.2

The Conditional (Ternary) Operator

condition ? a : b is an expression that produces a when the condition is true and b otherwise. It's a compact if/else for choosing a value, with its own typing rules that can surprise you (numeric promotion and unboxing).

29 min 3 code practice

3
2.3

The switch Statement

A switch statement compares one value against a list of constant case labels and jumps straight to the matching one. In the classic colon form, execution falls through into the following cases until a break, which is both its trick and its most common bug.

30 min 5 code practice

4
2.4

Switch Expressions and Arrow Labels

Since Java 14, switch can be an expression that produces a value, and case X -> arrow labels never fall through. The compiler checks that a switch expression covers every possible input, and Java 21 adds pattern matching, guards and case null on top.

31 min 5 code practice

5
2.5

while and do-while Loops

A while loop repeats a block as long as a condition stays true, checking the condition before each pass, so it may run zero times. A do-while checks after each pass, so its body always runs at least once.

29 min 5 code practice

6
2.6

The for Loop

The classic for (init; condition; update) loop puts a loop's start, test and step on one line. It's the standard way to repeat a known number of times or walk through array indexes, and most loop bugs are off-by-one errors in its three parts.

30 min 4 code practice

7
2.7

The Enhanced for Loop

for (Type item : collection) visits every element of an array or any Iterable (like a List) in order, with no index to get wrong. The loop variable is a copy of each element, and you must not add to or remove from a collection while walking it this way.

30 min 4 code practice

8
2.8

break, continue and Labels

break leaves the innermost loop or switch at once; continue skips the rest of the current iteration and moves to the next. A label lets either one target an outer loop, which is the clean way to stop nested loops.

28 min 4 code practice

9
2.9

Nested Loops and Patterns

A loop inside another loop runs its whole inner loop once for every iteration of the outer one. That's how you walk grids, compare every pair, and print patterns, and it's why the work grows as rows × columns (often n²).

29 min 4 code practice

10
2.10

Putting It Together: Control-Flow Problems

Classic small problems (FizzBuzz, primes, GCD, Fibonacci, digit tricks, binary conversion) solved with only the tools from this phase. Each one shows a reusable pattern: test the special case first, peel digits, stop early, and keep a running result.

37 min 4 code practice