levo

Program Control Structures

Nested loops and tracing iterations

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

Overview

A loop inside a loop and how many times the inner body runs, tracing the output of a nested pair, triangular patterns where the inner limit depends on the outer counter, and breaking out early.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a nested loop?

Key term

An outer loop runs three times and an inner loop four times. How many times does the inner body execute in total?

Key term

Why is using the same variable name for both counters of a nested loop a mistake?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a nested loop?

    Model answer

    A nested loop is a loop written inside the body of another loop, so the whole of the inner loop is carried out on every pass of the outer one.

  2. 2

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

    Model answer

    The outer counter takes 1, 2 and 3, and for each of those the inner counter takes 1 and 2, so the output is 11 12 21 22 31 32.

  3. 3

    An outer loop runs three times and an inner loop four times. How many times does the inner body execute in total?

    Model answer

    The inner body executes twelve times in total, because the four inner passes are repeated on each of the three outer passes.

  4. 4

    int c = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { c++; } } — what value does c hold at the end?

    Model answer

    The inner loop runs five times for each of the five outer passes, so the counter is increased twenty-five times and c finishes at 25.

  5. 5

    int c = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { c++; } } — what value does c hold at the end?

    Model answer

    The inner loop runs once, then twice, then three and then four times as the outer counter grows, so c finishes at 10.

  6. 6

    Why must an inner loop counter be reset on each pass of the outer loop, and how does declaring it in the header achieve that?

    Model answer

    The inner loop must start again from the beginning every time, and declaring the counter in the inner header creates it afresh with its initial value on each entry to the loop.

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.