More work with loops and sequence

After completing this lesson you should be able to:

  1. describe and use nested loops
  2. describe and use compound statements
  3. use indentation to show program structure


Return to the index
Go to the next lesson
Return to the previous lesson


Nesting loops - loops within loops

The situations where nested loops might be used are quite common, they are the kind of situation where we need to get a piece of data from a number of pieces and carry out some processing on each piece. For example we might be reading student records from a disk drive and for each student record we might count the number of subjects that the student is enrolled in and calculate what fees the student should pay.

Here are some examples of the structure of nested loops:

REPEAT 
 REPEAT 
  ... 
 UNTIL Inside_Loop_Done 
UNTIL Outside_Loop_Done 


REPEAT 
 REPEAT 
   REPEAT 
      ... 
   UNTIL End_1 
 UNTIL End_2 
UNTIL End_3 
DOWHILE In_Loop_1 
 DOWHILE In_Loop_2 
  ... 
 END DOWHILE 
END DOWHILE 


DOWHILE In_Loop_1 
 DOWHILE In_Loop_2 
   DOWHILE In_Loop_3 
  ... 
  END DOWHILE 
 END DOWHILE 
END DOWHILE 

To demonstrate the use of nested loops I'll borrow the student records example from above. Assume that we have a database of student records and each student record is something like:

There are one hundred students and each student is enrolled in four subjects. I need to know the total fees that will be collected.

Repeat and While nested loops
st_count = 0
total_fee = 0
REPEAT
 INPUT student_name
 subject_count = 0
 REPEAT
   INPUT subject_title
   INPUT subject_cost
   total_fee = total_fee + subject_cost
   subject_count = subject_count + 1
 UNTIL subject_count = 4
 st_count = st_count + 1
UNTIL st_count = 100
st_count = 0 
total_fee = 0 
DOWHILE st_count < 100 
 INPUT student_name 
 subject_count = 0 
 DOWHILE subject_count < 4 
   INPUT subject_title 
   INPUT subject_cost 
   total_fee = total_fee + subject_cost 
   subject_count = subject_count + 1 
  END DOWHILE 
 st_count = st_count + 1 
END DOWHILE

There is no real limit to how deeply loops can be nested but it is not common to find loops nested more than three deep.

The compound statement and indentation

You have probably noticed that I have tended to use indentation within loops. There is a method to my madness. One aim is to make the algorithms easier to read; the other is to stress that processes that are indented to the same level belong together. In the diagram here I have exaggerated and outlined the indentation so that you can see how the statements in the algorithm belong together. In some cosmetic respects it isn't necessary to use indentation but there is a situation where the use of indentation is mandatory, otherwise your program designs are going to be misleading or, worse, wrong.

For instance: Is the piece of pseudocode on the left different from the piece on the right?

x = 22 
y = 1 
IF x = = 22 
 THEN y = x + 7 
z = y + 2 
j = z + 9 
DISPLAY y, z, j
x = 21 
y = 1 
IF x = = 22 
 THEN y = x + 7 
            z = y + 2 
            j = z + 9 
DISPLAY y,z,j

There is no obvious difference except that the code on the right uses indentation and the indentation is meant to show that the statements y = x + 7, z = y + 2 and j = z + 9 will only execute if x = =  22 is true. This kind of arrangement of statements is called a compound statement.

Often when using the IF statement it isn't necessary to use ENDIF but in cases where compound statements are used it is better to make use of ENDIF to avoid any confusion:

x = 22 
y = 1 
IF x = = 22 
 THEN y = x + 7 
ENDIF 
z = y + 2 
j = z + 9 
DISPLAY y, z, j
x = 21 
y = 1 
IF x = = 22 
 THEN y = x + 7 
            z = y + 2 
            j = z + 9 
ENDIF 
DISPLAY y,z,j

Summary

In this lesson you studied the nesting of loops and the use of indentation and compound statements. This was quite a short lesson which built on the material learned in the previous lesson on pseudocode. The next lesson introduces some new topics which basically represent  the end of the material which is relevant to all the methods of designing programs introduced in this course.



Return to the index
Go to the next lesson
Return to the previous lesson


This publication is copyright David Beech and Learning Systems 1997-2002
and may not be reproduced by any means without the written permission of
David Beech.
9 Wyndella Street, Tasmania, Australia


db@codelearn.com