NESTED LOOPS
A for loop can occur within another, so that the inner loop (which contains a block of statements) is repeated by the outer loop.
RULES RELATED TO NESTED FOR LOOPS
1. Each loop must use a seperate variable
2. The inner loop must begin and end entirely within the outer loop.
CLASS EXERCISE
Determine the output of the following program,
program NESTED_FOR_LOOPS (output);
var line, column : integer;
begin
writeln('LINE');
for line := 1 to 6 do
begin
write( line:2 );
for column := 1 to 4 do
begin
write('COLUMN':10); write(column:2)
end;
writeln
end
end.
Class exercise .. output of program NESTED_FOR_LOOPS is,
LINE
1 COLUMN 1 COLUMN 2 COLUMN 3 COLUMN 4
2 COLUMN 1 COLUMN 2 COLUMN 3 COLUMN 4
3 COLUMN 1 COLUMN 2 COLUMN 3 COLUMN 4
4 COLUMN 1 COLUMN 2 COLUMN 3 COLUMN 4
5 COLUMN 1 COLUMN 2 COLUMN 3 COLUMN 4
6 COLUMN 1 COLUMN 2 COLUMN 3 COLUMN 4