Java SE 17 (1Z0-829) study materials
Java SE 17 (1Z0-829) study resources

Java SE 17 (1Z0-829): Controlling program flow

Java SE 17 (1Z0-829) · Work through your exam, one topic at a time.

All guide sections

Controlling program flow

Statements and expressions

if/else, while, do-while, for, and for-each share Java's boolean-only condition rule. switch works on integral types, String, enum, and (as a preview in 17) patterns; case labels must be compile-time constants, and duplicate labels are a compile error. Arrow switch expressions do not fall through; colon-style switch statements do.

switch expressions

A switch expression must be exhaustive. Use yield to return a value from a block arm. int day = 3; String kind = switch (day) { case 1,2,3,4,5 -> "weekday"; case 6,7 -> "weekend"; default -> throw new IllegalArgumentException("bad day"); }; The default is required here because int is not exhaustive. For an enum covering all constants, no default is needed.

Loops and labels

break exits the nearest loop or switch; continue skips to the next iteration. Labeled break exits the named outer loop: outer: for (int i=0;i<3;i++) for (int j=0;j<3;j++) if (j==1) break outer; This ends both loops.

Traps

Missing break in colon-style switch causes fall-through. A case label cannot be a variable. A switch expression arm that is a block must yield or throw. Unreachable statements after break/return are compile errors. In for-each, modifying the underlying collection can throw ConcurrentModificationException.