CCEA A-Level Software Systems Development CL4
Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures
Exception Handling and Managing Input and Output
Testing an Object Oriented Application
Graphical User Interface Objects and Events
Multiple Forms and Linking to Files
Designing, Testing and Evaluating an Event Driven Application
Reasons for Systems Development and Systems Methodologies
Managing Projects
Testing Strategies
Database Concepts and Entity Relationship Modelling
Normalisation and Structured Query Language
Project Management and the Systems Approach in Practice
Defining Requirements and Documenting the Design
Building a Desktop Solution with a Database
Testing and Evaluating the Solution
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.
An outer loop runs three times and an inner loop four times. How many times does the inner body execute in total?
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
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
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
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
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
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
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.
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.