Part 7: Loops
Very often within a program you will want to repeat the same process over and over, for either a defined amount of times or until a certain event occurs. It would be possible to display the same line of text many times by entering that program statement each time yo uwanted to print that line, but that is very inefficient. Loops allow you to define the circumstances of repetition, and allow you to execute the same statement repeatedly instead of typing the same instruction over and over.
There are two primary types of loops in any programming language, I'll call them "pre-conditional" and "post-conditional". There are as many different ways to implement these loops as there are languages to implement them in, but the overall concepts don't change. The C programming language provides two types of pre-conditional loops and one post-conditional.
The pre-conditional loops execute a conditional expression first, and if and only if that statement evaluates to true, the contents of the loop are executed. After each execution of the loop, the conditional expression is re-evaluated and until it is false, the contents of the loop will continue to execute.
Always ensure that the conditional will eventually evaluate as false, otherwise you will be stuck within an infinite loop which will never stop executing.
The first type of pre-conditional loop is the "while" loop. The keyword while is used to define the condition, then the loop contents follow, contained within brackets similar to if statements.
/* while.c 1.1 (03/16/98) */
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 1;
while(x <= 5)
{
printf("%i. This is some text.\n",x++);
}
return 0;
}
First the x integer is initialized to 1. If we don't initialize it, it could contain anything, so always initialize your variables. Next our while statement identifies the conditional expression we are using to determine if we should execute the loop. Since x <= 5 evaluates to true, the loop will execute. Since the the conditional expression is evaluated before the loop, depending on your conditions it is possible that a whileloop will never execute.
The braces after the while statement enclose the contents of our loop. Within the printf statement, the variable x is followed by the post-increment operator: ++. What this does is increment the value of x by 1 after it has been used in the statement.
After the text is displayed and x is incremented, our while condition is evaluated again. This time x is now equal to 2, and x <= 5 still evaluates to true, so the loop is executed again. The loop will continue to execute until x is 6, and at that point execution will continue outside the loop.
The output of this program is:
1. This is some text. 2. This is some text. 3. This is some text. 4. This is some text. 5. This is some text.
The next type of pre-conditional loop in C is the for loop. The for loop is useful when you have a defined number of iterations you want your loop to perform, and you have the counter built into the loop itself.
In a for loop, the initialization, conditional comparison, and counter incrementation all take place within the for statement, each part seperated by a semi-colon.
/* Forloop.c 1.1 (03/16/98) */
#include <stdio.h>
int main(int argc, char *argv[])
{
int x;
for (x = 1; x <= 5; x++)
{
printf("%i. This is some text.\n",x);
}
return 0;
}
The output of this loop will be exactly the same as the previous loop.
The C programming language also provides a post-conditional loop. The primary difference of the post-conditional loop is that the conditional comparison occurs after the loop executes, so the loop contents will always execute at least once.
This type of loop is created in C using the do keyword, with the while statement and condition following the loop body.
/* doloop.c 1.1 (03/16/98) */
#include <stdio.h>
int main(int argc, char *argv[])
{
int x=1;
do
{
printf("%i. This is some text.\n",x);
} while ( x <=5 );
return 0;
Again, the output of this loop will be exactly the same as the previous loops.
Two additional keywords used with loops are continue and break. They breaks keyword breaks out of the loop and continues execution after the loop. The continue keyword breaks out of the loop and continues running the loop, returning to the loop's conditional comparison.
Here's an example of these keywords in use:
/* BCloop.c 1.1 (03/16/98) */
#include <stdio.h>
int main(int argc, char *argv[])
{
int x ;
for (x = 1; x <= 10; x++ )
{
if (x == 3)
{
continue;
}
if (x == 6)
{
break;
}
printf("%i. This is some text.\n",x);
}
return 0;
}
When the value of x reaches 3, the continue keyword will force a break out of the loop and the loop will continue from the increment and comparison. When x is 6 the break keyword will break out of the loop and continue after the loop.
The output of this program is:
1. This is some text. 2. This is some text. 4. This is some text. 5. This is some text.
Observe the number 3 is not displayed, nor are any values after 5, even though the loop is designed to count up to 10 if left to its own conditional expression.
There can never be enough emphasis on the fact that the double equal sign means to compare the two values and a single equal sign means to assign the values. This is one of the most common errors to make in C programming. Imagine if the second if statement had been written like this:
if( x = 5 ) break;
In this instance, the program would have compiled fine, but when it was excuting and reached this point, x would have been assigned the value of 5, and since that is not 0 the condition would evaluate as true and break out of the loop on the first iteration, never displaying a single line.
Loops are great time savers, allowing a group of statements to repeat as many times as you desire without requiring you to type them in for each repetition. Loops also conserve computer memory, since the program executes the same section of code over and over instead of executing many copies of the statements. You will probably use loops often within your programs so be sure to practice their usage to understand them fully. Also practice using break and continue so you can be sure to use the correct keyword when you need to add it in your program.

