levo

Program Control Structures

Nested loops and tracing iterations

  • 3 things to know
  • AS Unit 1

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.

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.