Looping Constructs

Looping Constructs

By Sasikumar M, CDAC Mumbai. All rights reserved. You are free to use this for your personal purposes, and for any course work with acknowledgement.

Among the most powerful instructions - from a programmer's perspective, particularly - is the looping construct. There are hardly any useful programs you can write without loops. Essentially, these constructs allow us to repeat an action multiple number of times with parameterised changes, many times. A simple example is printing n number of '*'s, printing odd numbers from 10 to 100, incrementing all elements in a sequence, taking the sum of a sequence, and so on. Sum computations like finding the nth power, factorial of n, etc also needs a series of similar computations, though the specification does not seem to suggest a repetition.

Consider computing 2^8, which is 1 x 2 x 2 x 2 x 2 x … x 2 (8 times). We can write this as:

a = 1
a = a * 2
a = a * 2
a = a * 2
a = a * 2
a = a * 2
a = a * 2
a = a * 2
a = a * 2

At the end of this, the value of a will be 2^8. What if we wanted 2^100. Writing like this 100 times is painful and error prone (what if we missed one or two!), and also not extendable. For example, what we if we knew the power only at run time — you ask the user the value of n, and computer 2^n. So, we need another way to express these. Loop constructs provide this.

A loop specification consists of four components:

1. An initialisation component. When you are computing a quantity as in the example above, you would need to set an initial value. 'a = 1' is an example of this. We may also initialise other temporary variables here. For example, in order to do 'a = a * 2' n times, we need a counter to keep track of how many times this has been done so far. So, we need to initialise this counter to zero, before we start.

2. The work to be repeated. Here it is 'a = a * 2'. We will see more complex examples, later on.

3. A way to represent progress. We need to know how much we have done to know whether it is time to stop. In this case, we need a counter, which we set to 0 to start, increment it everytime we do item (2), and check if it is exceeding 'n'. So the progress mechanism is a counter increment here: counter = counter + 1

4. Are we done? A way to check if it is time to stop. We continue, in this case, as long as 'counter < n'; and stop otherwise.

All looping constructs can be characterised with these four components. There are many variants we will see, in any programming languages. Commonly there are three: a while loop, a repeat-until loop and a for loop. We will do these here, and leave the rest to specialised sections later on.

A while loop has the following strucure using the four components mentioned above:

(1)
while (4) {
(2)
(3)
}

In PHP, this may appear as:

(1)
do while (4)
(2)
(3)

First (1) is executed. Then (4) is checked, if it is false, the loop is over. Else (2) is done, then (3) is done, and then go back to checking (4). repeat.

Our exponentiation program looks like this:

a = 1
counter = 0
while (counter < n)
a = a * 2
counter = counter + 1

For different values of n, hand simulate it and see that 'a' has the right value at the end.

Here is the factorial program using the same structure. Recall fact(n) = 1 x 2 x 3 x 4 x … x n

counter = 1
ans = 1
while (counter < n)
ans = ans * counter
counter = counter + 1

Note that the (2) here is parameterised. We do not multiply with the same value every time. As we progress, we choose different multiplier, because we observe that the multiplier is the same as the counter value, as we count up to n.

Here is another program to print the odd numbers from 10 to 100. Study the program carefully and ensure you understand what is happening.

a = 11
while (a < 100)
print a
a = a + 2

Incrementing by 2, ensures that a takes only odd numbers.

Write similar programs to print numbers from 100 to 10 (in reverse), even numbers from 10 to 100, multiples of 3 from 10 to 100, etc.

…..

page revision: 1, last edited: 17 Oct 2009 07:12