Control Loops

Objectives Conditional, or Logical, Expressions as used in program control
the do while loop
the while loop
the for loop

The while and do while Loops

We can repeat any statement using either the while loop:

while(condition) compound statement;
or the do while loop:
do compound statement while(condition);

Each line of a C program up to the semicolon is called a statement. The semicolon is the statement's terminator. The braces { and } which have appeared at the beginning and end of our program unit can also be used to group together related declarations and statements into a compound statement or a block.

In the case of the "while loop" before the compound statement is carried out the condition is checked, and if it is true the statement is obeyed one more time. If the condition turns out to be false, the looping isn't obeyed and the program moves on to the next statement.

In the case of the "do while loop" it will always execute the code within the loop at least once, since the condition controlling the loop is tested at the bottom of the loop. The do while loop repeats the instruction while the condition is true. If the condition turns out to be false, the looping isn't obeyed and the program moves on to the next statement.

Conditions or Logical Expressions

The only detail we need to clear up is what the condition (or Logical Expression) can be. How, for example, do we display 100 or 10,000 "Hello World!" messages? The condition can be any test of one value against another.

For example:
a>0
is true if a contains a value greater than zero;
b<0
is true if b contains a value less than zero.

The only complication is that the test for 'something equals something else' uses the character sequence == and not =. That's right: a test for equality uses two equal-signs, as in a==0, while an assignment, as in a=0, uses one.

Using while loop: #include
main()
{
while (1 == 1) printf("Hello World!\n");
}
Using the do while loop: #include
main()
{
do
printf("Hello World!\n");
while (1 == 1)
}

C is a language where anything that's used often can be said concisely, so it lets you say "add one to a variable" using the shorter notation:

++a;

The double plus is read "increment a by one". Make sure you know that ++a; and a=a+1; are the same thing because you will often see both in typical C programs.

The increment operator ++ and the equivalent decrement operator --, can be used as either prefix (before the variable) or postfix (after the variable). Note: ++a increments a before using its value; whereas a++ which means use the value in a then increment the value stored in 'a'.

Now it is easy to print "Hello World!" 100 times using the while loop:

#include
main()
{
int count;
count=0;
while (count < 100)
{
++count;
printf("Hello World!\n");
}
}
do while loop: #include
main()
{
int count;
count=0;
do
{
++count;
printf("Hello, World!\n");
}
while (count < 100)
}

Note: the use of the { and } to form a compound statement; all statements between the braces will be executed before the loop check is made.

The integer variable count is declared and then set to zero, ready to count the number of times we have gone round the loop. Each time round the loop the value of count is checked against 100. As long as it is less, the loop carries on. Each time the loop carries on, count is incremented and "Hello World!" is printed - so whenever count does reach 100 and the loop stops. Looping the Loop

We have seen that any list of statements enclosed in curly brackets is treated as a single statement, a compound statement. So to repeat a list of statements all you have to do is put them inside a pair of curly brackets as in:

while (condition)
{
statementl;
statement2;
statement3;
}

which repeats the list while the condition is true. Notice that the statements within the curly brackets have to be terminated by semicolons as usual. Notice also that as the while statement is a complete statement it too has to be terminated by a semi-colon - except for the influence of one other punctuation rule.

The for Loop

The while, and do-while, loop is a completely general way of repeating a section of program over and over again - and you don't really need anything else but... The while loop repeats a list of instructions while some condition or other is true and often you want to repeat something a given number of times. The traditional solution to this problem is to introduce a variable that is used to count the number of times that a loop has been repeated and use its value in the condition to end the loop.

For example:
i=l;
while (i<10)
{
printf("%d \n",i);
++i;
}

repeats while i is less than 10. As the ++ operator is used to add one to 'i' each time through the loop you can see that 'i' is a loop counter and eventually it will get bigger than 10, i.e. the loop will end. In C this special type of loop can be implemented as a for loop.

for ( counter=start_value; counter <= finish_value; ++counter )
compound statement
which is entirely equivalent to:
counter=start;
while (couner <= finish)
{
statements;
++counter;
}

The condition operator <= should be interpreted as less than or equal too.

For example to print the numbers 1 to 100 you could use:

for ( i=l; i <= 100; ++i ) printf("%d \n",i);

In the meantime consider the following program, it does a temperature conversion, but it also introduces one or two new concepts:

#include
main()
{
int fahr;
for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20)
printf("%4d %6.1f\n" , fahr , (5.0/9.0)*(fahr-32));
}

Another example:

#include
main()
{
int lower , upper , step;
float fahr , celsius;
lower = 0 ;
upper = 300;
step = 20 ;
fahr = lower;
while ( fahr <= upper ) {
celsius = (5.0 / 9.0) * (fahr - 32.0);
printf("%4.0f %6.1f\n" , fahr , celsius);
fahr = fahr + step;
}
}