levo

Program Control Structures

Breaking out of loops and avoiding infinite loops

  • 3 things to know
  • AS Unit 1

Overview

How a flag-controlled search loop works, infinite loop and loop guard.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Describe how a flag-controlled search loop works.

Key term

What does break do inside a loop?

Key term

What does continue do inside a loop?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What does break do inside a loop?

    Model answer

    A break abandons the loop immediately, so no further passes occur and execution continues at the first statement after the loop.

  2. 2

    What does continue do inside a loop?

    Model answer

    A continue abandons the rest of the current pass only, so execution jumps to the loop update and condition and the loop carries on.

  3. 3

    for (int i = 1; i <= 5; i++) { if (i == 3) { break; } System.out.print(i + " "); } — what does this fragment print?

    Model answer

    The loop prints 1 and then 2, and on the third pass the break leaves the loop before the printing statement, so the output is 1 2.

  4. 4

    for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.print(i + " "); } — what does this fragment print?

    Model answer

    Only the pass with the counter at 3 is cut short, and the remaining passes print as normal, so the output is 1 2 4 5.

  5. 5

    Why do some programmers avoid break, and what is the structured alternative?

    Model answer

    A break gives the loop a second exit point, which makes the flow harder to follow; the structured alternative is a boolean flag set inside the loop and tested in the loop condition.

  6. 6

    Describe how a flag-controlled search loop works.

    Model answer

    A boolean flag starts false and the loop continues while the flag is false and items remain; when the wanted item is met the flag is set to true, which ends the loop at the next test without a break.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Program Control Structures, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.