Overview
Domain 2 is Oracle's single objective on program flow control. It asks you to create and read if/else statements, switch statements and switch expressions, all four loop forms, and break and continue statements, including labeled versions that jump out of nested loops. In Java 21 the switch construct has two personalities: the classic statement with colon labels and fall-through, and the expression form with arrow labels, yield and an exhaustiveness requirement.
The exam rarely asks for definitions. Instead it shows a loop or a switch and asks for the output, the number of iterations, or which line stops the code from compiling. Pattern matching inside switch is part of the object-oriented section, but the basic mechanics of case labels, default and yield are tested here.
How it's tested
Expect compact snippets with nested loops or a switch over an int, String or enum value. You either count iterations, give the final value of a variable, or decide whether the code compiles. Distractors are usually off by one iteration or assume fall-through behaves the opposite way.
-
Count the iterations
A loop with an unusual update expression or condition runs, and you choose how many times the body executes or what a counter holds.
-
Fall-through or not
A switch with colon labels and missing break statements is compared with an arrow-label version, and you pick the printed output.
-
Does this switch compile
A switch expression lacks a default, returns no value on one path or mixes arrow and colon labels, and you identify the error.
A switch statement over an int has case 1: print A, case 2: print B and default: print C, with no break statements. The value is 2. Execution jumps to case 2 and prints B, then falls through into default and prints C, so the output is BC. If the labels used arrows instead, only B would print, because arrow cases never fall through.
Objectives 1
Official objectives from the Oracle outline, in outline order.
2.1 Create program flow control constructs including if/else, switch statements and expressions, loops, and break and continue statements.Conditionals, both switch forms, all loop forms and jump statements.
- What it means
- You must be able to write and trace if/else chains, switch statements, switch expressions, for loops, enhanced for loops, while loops and do/while loops. Break leaves the nearest loop or switch, continue skips to the next iteration, and both accept a label to target an outer statement. Switch expressions return a value, use arrow labels or yield, and must be exhaustive.
- What to understand
- A switch can test int-compatible primitives and their wrappers, String, enum values and, since Java 21, any reference type when patterns are used. Case constants must be compile-time constants and cannot repeat. Colon labels fall through, arrow labels do not, and the two styles cannot be mixed in one switch. A switch expression needs a default unless the cases already cover every value, as with all constants of an enum. Inside a block on an arrow case, yield supplies the value. Loop details matter: the for update runs after each body pass, an enhanced for loop cannot modify the array slot it iterates over, a do/while body runs before its first test, and a statement after an unconditional break is unreachable and fails compilation.
- How it shows up
- Expect nested loops with labeled continue, switch expressions missing a case, and loops whose counter is changed inside the body.
- Common confusion
- Thinking a switch expression with arrow labels falls through like the classic statement, or that break in an inner loop exits the outer one.
High-yield concepts
The terms, rules and formulas this domain turns into questions most often.
-
Fall-through
Colon-label cases continue into the next case without break.
- Remember
- Arrow cases never fall through.
- Tested as
- Output of a switch statement.
- Trap
- Expecting a case to stop by itself.
-
Switch expression
A switch that produces a value.
- Remember
- It must be exhaustive and end with a semicolon when assigned.
- Tested as
- Compile-error and output questions.
- Trap
- Omitting default for an int selector.
-
yield
Returns a value from a block inside a switch expression.
- Remember
- return is not allowed there for that purpose.
- Tested as
- Arrow blocks with several statements.
- Trap
- Writing return instead of yield.
-
Exhaustiveness
Every possible selector value must be handled.
- Remember
- Enum switches can be exhaustive without default.
- Tested as
- Missing case errors.
- Trap
- Assuming default is always required.
-
Labeled break
Exits the statement carrying the label.
- Remember
- Labels go on loops or blocks, not on single expressions.
- Tested as
- Nested-loop output.
- Trap
- Thinking plain break leaves all loops.
-
Labeled continue
Jumps to the next iteration of the labeled loop.
- Remember
- The inner loop restarts from scratch.
- Tested as
- Iteration counting.
- Trap
- Counting skipped inner iterations as executed.
-
do/while
Loop that tests its condition after the body.
- Remember
- The body runs at least once.
- Tested as
- Off-by-one counts.
- Trap
- Treating it like a while loop with a false start.
-
Unreachable code
Statements the compiler proves can never run.
- Remember
- Code after break, continue or an endless loop fails.
- Tested as
- Compile-error identification.
- Trap
- Expecting a runtime warning instead.
-
Enhanced for
Iterates arrays and Iterable objects element by element.
- Remember
- The loop variable is a copy of each element.
- Tested as
- Questions on modifying arrays in loops.
- Trap
- Assigning to the loop variable to change the array.
Side by side
Concepts candidates mix up, compared on the point that decides the answer.
Switch forms
| Concept | Meaning | Key difference | Common trap |
|---|---|---|---|
| Switch statement with colon labels | Classic branching with fall-through | Needs break to stop; no value produced | Missing break prints extra cases |
| Switch statement with arrow labels | Branching without fall-through | Each case runs only its own code | Thinking exhaustiveness is required for constants |
| Switch expression | Branching that yields a value | Must be exhaustive and assign or return a value | Leaving out default for an int or String selector |
Loop forms
| Concept | Meaning | Key difference | Common trap |
|---|---|---|---|
| for | Init, condition and update in one header | Update runs after each body pass | Off-by-one with <= bounds |
| Enhanced for | Element-by-element iteration | No index and no write-back to arrays | Trying to change elements through the loop variable |
| while | Tests before every pass | May run zero times | Forgetting to change the condition variable |
| do/while | Tests after every pass | Always runs at least once | Missing the trailing semicolon |
Exam traps
Each trap is an answer that looks right. Learn the clue that should change your mind.
-
Arrow fall-through
- What candidates pick
- Picks output that includes the following case after an arrow label.
- Why it looks right
- The classic switch falls through, and the habit carries over.
- Why it's wrong
- Arrow labels execute only the matched case; fall-through exists only with colon labels.
Clue to watch for Cases written as case 2 -> with no break statements.
-
Default not needed
- What candidates pick
- Says a switch expression over an int compiles without default.
- Why it looks right
- The listed cases cover the values used in the example.
- Why it's wrong
- The compiler needs every possible int handled, so a non-exhaustive switch expression fails to compile.
Clue to watch for A switch expression assigned to a variable with no default branch.
-
return inside a switch block
- What candidates pick
- Chooses code that uses return to give a switch expression its value.
- Why it looks right
- return hands back values in methods.
- Why it's wrong
- A switch expression block must use yield; return would try to leave the enclosing method and does not compile there.
Clue to watch for A braced block after an arrow inside an assignment.
-
Plain break exits everything
- What candidates pick
- Counts as if the outer loop stopped.
- Why it looks right
- The break looks like it ends the whole search.
- Why it's wrong
- An unlabeled break leaves only the innermost loop or switch, so the outer loop keeps running.
Clue to watch for Nested loops where only the inner one contains break.
-
Mixed label styles
- What candidates pick
- Assumes a switch with one arrow case and one colon case compiles.
- Why it looks right
- Both forms are valid separately.
- Why it's wrong
- Java forbids mixing arrow and colon labels within the same switch block.
Clue to watch for A switch that switches syntax partway down.
Answer framework
Work through flow questions with this sequence before choosing an answer.
- Check that the code compiles first: label styles are not mixed, case constants are unique and constant, and no statement is unreachable.
- For a switch expression, confirm exhaustiveness and that every path produces a value through an arrow expression or yield.
- Find the matching case or condition for the given input and note whether colon labels let execution fall through into later cases.
- Trace loops with a small table of variable values per pass, including the update step and any change to the counter inside the body.
- Apply break and continue precisely, noting which loop each one targets, whether a label is present, and what code is skipped as a result.
Practice question
Want more like this? The 1Z0-830 practice test has questions for every domain.
Checklist
0 of 10
Tick each one you can do without notes. Your ticks stay in this browser.
Study strategy
Program flow is a short section, so aim for accuracy rather than volume. Write a handful of small programs: one switch statement that falls through, the same logic as an arrow switch, and a switch expression with a block that uses yield. Remove the default branch and read the compiler message, then switch over an enum and see why the default is no longer needed.
For loops, practise with paper. Take nested loops with labeled break and continue, build a table of i, j and the counter for every pass, and only then run the code. Repeat with loops that modify the counter inside the body and with do/while loops that start with a false condition.
After two sessions, take a set of timed practice items and note every miss by cause: fall-through, exhaustiveness, label target or off-by-one. Most errors in this section repeat a single habit, so the error log tells you exactly what to drill. Revisit the list the day before the exam.
FAQ
How much of 1Z0-830 is Domain 2, Controlling Program Flow?
Oracle publishes no weight for Controlling Program Flow. It is a single objective, and we estimate about four questions, although loops and switches appear in snippets everywhere.
Is pattern matching for switch tested in this section?
The pattern forms, such as type patterns and record patterns in case labels, sit under the object-oriented objective. This section focuses on the switch mechanics themselves.
Can a switch statement use a String selector?
Yes. switch has accepted String since Java 7. Case labels must be compile-time constant strings, and matching uses equals.
When can I omit default in a switch expression?
When the cases already cover every possible value, for example every constant of an enum or every permitted subtype of a sealed interface.
What is the difference between yield and return?
yield gives a value to the enclosing switch expression. return leaves the whole method, so it cannot be used to produce a switch expression's value.
Do labeled statements come up often?
They appear regularly in nested-loop questions. Practise tracing labeled break and continue until you can count iterations quickly and reliably.
Test your Domain 2 knowledge
You have reviewed if/else, both switch forms, yield, all loop types and labeled jumps. Test whether you can count iterations and spot non-compiling switches under time pressure.
Keep going
- Training Training Built on your Weak areas
- Practice test Questions by domain, with explanations
- Mock exam Full length, timed like the real exam
- Readiness test Find your weakest domain in minutes
- Cheat sheet Key terms and formulas on one page
- Ask the 1Z0-830 AI tutor Get any concept explained your way