Programming Loops

<Back>

 

There are 3 questions to ask before deciding which type of loop to use :

 

1)    Do you know how many times you want to repeat code?
(This kind of loop is “top tested” before code is executed)

                                                                                                                        Some examples :

a)    for each of twelve months

b)   for each student in a class

c)    for each question on a test

 

 

If the number of times the code will be executed is not known :

 

2)    Do you want to execute the code at least one time?
(This kind of loop is “bottom tested” after code is executed at least once)

                                                                                                                  Some examples :

a)    To let the user enter something, and possibly quit

i)     Such as used in a “menu” :

(1)  the user will do this at least once,
      or as many times as necessary ?

3)    Is it possible that the code will not execute even once?
(This kind of loop is “top tested” before code is executed even one time)

                                                                                                                        Some examples :

a)    entering a list of data, one at a time, until finished

i)     user may choose to enter many items, or none at all !

b)    Reading data from a file, where there not be any!

i)     There may be many items, or none at all, in the file !

Summary:

There are several ways to repeat instructions in nearly any programming language.

1.    The ‘For’ loop

2.    The ‘Do … Until’

3.    The ‘Do … While’ loop (confusing variation of the ‘Do … Until’ loop)

4.    The ‘While’ Loop

 

 

 

 

 

The ‘For’ loop :

(Executes code a preset number of times)

 

If you know how many times you want to execute the code, the ‘for’ loop is the best choice.

 

Primarily, the ‘for’ loop lets the programmer specify what value to start, and when to stop.

By default, the loop counter increments by 1, but optionally this can be any value even fractions!

 

Loop start from start value to end value, and counts by ‘ones’ (unless specified)

         // Checks <Terminating Condition> to decide whether to it again ?

         <program code> 

End of loop

The “For” loop is “top tested” before code is executed.

 

The body of code in a “For” loop may execute any number of times,

or may not execute at all, because it is tested before the code executes the first time.

 

<Back>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Both of the following types of loops let the user (or data from a file) control how many times the code is executed :

 

The Do … Until or Do … While loop :
(Code may be executed at least once, or more times)

 

This loop starts by executing the code before it gets to the ‘terminating condition’ that checks to see if the code should be done again?

If the terminating condition is false, the code is executed again, otherwise the loop ends.

As you can see, the code is executed before checking to see if it should be done again.

 

The format looks like this :

 

Loop start

        <program code> 

        <Data is entered that may tell loop to quit >

End of loop < Terminating Condition value is checked>

 

 

 

NOTE : Some languages have both a “Do … While” and a “Do … Until” loop structure.

 

 

The difference between the “Do … While” and the “Do … Until” is subtle:

These are both essentially the same thing though.

 

The “Do … While” performs the code “while” the condition is true, then exits when it is false.

During the “Do … While” loop, the condition is true.

 

The body of code in a “Do…While” loop will execute at least once,

because it is tested after the code executes the first time.

 

 

 

The “Do … Until” performs the code “until” the condition is true, then exits when it is true.

During the “Do … Until” loop, the condition is false.

 

The body of code in a “Do…While” loop will execute at least once,

because it is tested after the code executes the first time.

 

(This kind of loop is “bottom tested” after code is executed)

 

<Back>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The While Loop
(Code may be executed 0 or more times)

 

The program lets the user (or data from a file) control how many times the code is executed :

 

Loop start <”While” Terminating Condition is True : value is checked before code is executed>

        <program code> 

        <Data is entered that may tell loop to quit >

End of loop

 

Since the terminating condition is checked before the code is executed, it is possible that the body of code never gets done at all!

 

The body of code in a “While” loop may execute any number of times,

or may not execute at all, because it is tested before the code executes the first time.

 

<Back>