Return to sysprog.net home page

españolfrançaisdeutsch

C and C++

Main page

Defines

Design

Loops

Pointers

Punctuation

Strings

Stuctures

Types


SITE SERVICES
Contact Us
Search
Site Map

Home —» C and C++> —» Loops



C Tutorial: Loops


Defining For-Loops:
C uses two types of loops: for and while. The for-loop is the Swiss army knife of C. It has four distinct sections as shown below: initialization, condition, iteration and body. The first three sections follow the for keyword and are enclosed in parentheses. The sections must be divided by semi-colons, even when one or more of the sections are empty. The body of the loop follows and is enclosed in braces - except when there is only one statement in the body, when the braces are optional.

for (INITIALIZATION; CONDITION; ITERATION)
   {
   BODY
   }

A simple example of a for-loop initializes each member in an array.

int month[12], i;

for (i = 0; i < 12; i++)
   {
   month[i] = 0;
   }

Mainframers who are used to requiring all variables to have 'meaningful' names may look askance at the loop counter 'i' used in this example. It is, however, standard practice in simple loops, because it makes the code more readable, especially when it enables the 'for' statement to all fit on one line.

Initialization Section:
The initialization section is performed exactly once - when the for-loop is first encountered. If there is more than one statement in the initialization section, they should be separated by commas and will be performed in order from left to right.

The initialization section in this example initializes two variables.

int i, emptyseats;

for (i = 0, emptyseats = 250; CONDITION; ITERATION)
   {
   BODY
   }

The initialization can be left empty if there is nothing to initialize.

Condition Section;
The condition section describes what must be true for the loop body to be performed again. When the condition becomes not true, control passes to the statement after the for-loop. The condition section can be left empty, but then there must be code in the body to break out of the loop when some condition is met, or the loop will execute for ever.

The condition section is evaluated according tothe normal boolean laws of C. Evaluation of the condition, going from left to right, will stop as soon as it can be determined that the whole condition is certainly true or false. In the example below, if i is greater than or equal to 10, then the value in table[i] is not checked.

int i, table[10];

for (INITIALIZATION; i < sizeof(table) && table[i] != 0; ITERATION)
   {
   BODY
   }

If the condition evaluates to false the first time it is tested, the body of the loop will not be performed at all and neither will the iteration.

Iteration Section:
The iteration statements are performed each time the body of the loop completes, before the condition is tested again. If there is more than one statement in the iteration section, you should separate them by commas and they will be performed in order from left to right.

In this example, the counter is incremented on each iteration, but only after the current value in the table has been added to the total.

int i, total, table[10];

for (INITIALIZATION; CONDITION; total += table[i], i++)
   {
   BODY
   }

The iteration section can be left empty if there is nothing to do after each loop.

Body:
The statements in the body of the loop must be enclosed in braces if there is more than one statement: otherwise they are optional. If the body is empty, you must code either empty braces or a semi-colon to end the for-statement.

for (INITIALIZATION; CONDITION; ITERATION)
   {
   }

Comparing For-Loops and While-Loops:
For-loops can be rewritten as while-loops by moving the initialization and iteration sections as shown below. Or, to put it another way, a while-loop is like a for-loop with no initialization or iteration sections.

INITIALIZATION
while (CONDITION)
   {
   BODY
   ITERATION
   }


TOP OF PAGE The last good thing written in C++ was the Pachelbel Canon. (Jerry Olson)