levo

Program Control Structures

Breaking out of loops and avoiding infinite loops

  • 3 things to know
  • AS Unit 1
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A null is the of one.
absencepresencetype

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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