E ExamMaster

Object-Oriented Programming through Java · Language Basics

Control Flow

In Object-Oriented Programming through Java because a ticket desk does not run every line in order — it chooses a fare, matches a day, and walks seats, sometimes leaving early.

Without control, Java runs the next statement, then the next. Control statements change that: pick a branch, match a label, or repeat. Meera's desk chooses a student fare, picks a weekday surcharge, and walks a row of seats — sometimes skipping a blocked one, sometimes stopping when a seat is found.

  • Object-Oriented Programming through Java
  • Easy level
  • 6 concepts

1Not every line runs next

Without control, Java runs the next statement, then the next. Control statements change that: skip a block, pick one of several, or repeat.

The machine still only does what you wrote. It does not guess which seats are free.

Figure. The top node is the default: run the next line. The right branch is what if, switch and loops are for — skip, pick, or repeat. Not a full flowchart of a program, only that fork.

How it works

  1. Not every line runs nextDefault is the next line. Control is how you skip, pick, or repeat.

2if picks one block

if (condition) { ... } else { ... } runs one block or the other. The condition must be boolean, not an int.

An else-if chain tests in order and takes the first true. Forgetting braces is legal for one statement and is how people attach the wrong line to the if.

Figure. if (student) runs one block or the other. The condition is boolean. Here student is true, so price is 150; the else sets 200.

Student fare or list price

boolean student = true;
int price;
if (student) {
    price = 150;
} else {
    price = 200;
}

Meera's student flag picks a fare

student is true. if (student) price = 150; else price = 200. What does price hold?

  • studenttrue
  • if (student) istaken
  • price150

Pro tip. The else block does not run. Flip student to false and price is 200.

Coding lab. Choose Meera's fare runs in the app, with checks on your output.

3switch matches a value; break stays put

switch picks a case from a value — a day name, a seat type — instead of a long else-if. default runs when no case matches.

Each case should break, or it falls through into the next case. Use switch when you match one value against known labels, not when you test ranges.

Figure. Sat and Sun share one body: extra is 30, then break. default is 0. Each case should break, or it falls through into the next case.

Weekend surcharge; Sat and Sun share a body

String day = "Sat";
int extra = 0;
switch (day) {
    case "Sat":
    case "Sun":
        extra = 30;
        break;
    default:
        extra = 0;
}

Saturday surcharge

day is Sat. Sat and Sun share extra = 30; default is 0. What is extra after the switch?

  • daySat
  • case Sat: falls into Sun bodyshared weekend case
  • extra = 30; break30

Pro tip. Without break after 30, a later default would overwrite extra. The break is what stays put.

4while may run zero times; do-while runs once

while (condition) { ... } repeats as long as the condition is true — maybe zero times if it starts false.

do { ... } while (condition); runs the body once, then tests — at least once. You must change something the condition reads, or the loop does not stop.

No diagram — the idea is carried by the prose or the Java listing.

Two loops, one extra guarantee
LoopTestsBody runs
whileBefore the bodyZero or more times
do-whileAfter the bodyAt least once

Count down remaining seats

int seatsLeft = 3;
while (seatsLeft > 0) {
    System.out.println(seatsLeft);
    seatsLeft = seatsLeft - 1;
}

5Counted for versus walk each value

for (int i = 0; i < n; i++) is a counted loop: start, test, step. The index i is how you name a position.

The enhanced for (for (String seat : seats)) walks each element. Use the counted for when you need the index; use for-each when you only need each value.

Figure. The counted for names a position: 0 A12, 1 A13, 2 A14. The enhanced for walks each seat value with no index. Use the counted for when you need the index.

Index when you need it; for-each when you do not

String[] seats = {"A12", "A13", "A14"};
for (int i = 0; i < seats.length; i++) {
    System.out.println(i + " " + seats[i]);
}
for (String seat : seats) {
    System.out.println(seat);
}

Walk three seats with an index

seats is A12, A13, A14. A counted for prints i then seats[i]. What are the three lines?

  • i = 00 A12
  • i = 11 A13
  • i = 22 A14

Pro tip. for-each would print A12 then A13 then A14 and would not know i.

6break leaves; continue skips this pass

break leaves the innermost loop or switch immediately. continue skips the rest of this pass and goes to the next test or step.

Neither is a substitute for a clear condition. They are exits from a pass you already started.

No diagram — the idea is carried by the prose or the Java listing.

Leaving a pass
WordWhat happens next
breakLeave the loop or switch
continueSkip the rest of this pass; test again

Skip a blocked mark; take the first real seat

String[] seats = {"A12", "X", "A14"};
for (String seat : seats) {
    if (seat.equals("X")) {
        continue;
    }
    System.out.println(seat);
    break;
}

Notes

  • In Object-Oriented Programming through Java because a ticket desk does not run every line in order — it chooses a fare, matches a day, and walks seats, sometimes leaving early.
  • if needs a boolean. switch matches a value against cases and needs break to stay in that case.
  • while may run zero times. do-while runs at least once. break leaves; continue skips the rest of this pass.

Exam traps & shortcuts

  • A switch case without break falls into the next case. That is usually a bug, not a feature you meant.
  • If a loop never changes the value the condition reads, it does not stop.

Recap

if picks a boolean branch, switch matches a label and needs break, while can run zero times, and continue is not break.

Not every line runs next
Control skips, picks, or repeats. The machine does not guess.
if picks one block
The condition is boolean. Braces keep the right statements.
switch matches a value
A case without break falls into the next case.
break and continue
break leaves. continue skips the rest of this pass.

Practise Control Flow

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.