teach-ict.com logo

THE education site for computer science and ICT

7. ITERATION - Part 2

The FOR ... NEXT loop and the DO .... WHILE loop forms are widely used, however, the code within the block will run at least once which is sometimes not desired.

Another construct for looping that avoids this is the WHILE ..... END loop

Example

  WHILE pointer IS NOT 'End of File'
      Read (data from file)
      Increment pointer

  END

With this form of WHILE loop, the test is carried out before the code is run.

In the example above, some kind of pointer is indicating the current position within a file but before any attempt is made to read data from the file the WHILE test makes sure that the end of the file has not been reached.

 

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: Use of iteration