Lab 4 Specifications        Executable copy of the instructor's solution

Specialized Loop for Counting (Chapter 15)

Loops that execute a fixed number of times occur so frequently that C has a structure built explicitly for that:  the for loop, with a strange syntax.
for ( <expression> ; <expression> ; <expression>) followed by a single statement or a compound ("{" ) statement.  Let's break that apart into three lines to see what each means:

for ( <expression>  ;   // Generally an assignment statement, initializing the loop variable — the initialization
      <expression>  ;   // A logical expression, testing whether the loop is to be executed  the execution test
      <expression>  )   // Generally a statement modifying the loop variable — the increment
{ <the body of the loop> } 

As you might expect, flow of control is also complicated.  On entry into a for loop, the first, the initialization expression is executed.  If none is needed, there is no statement here but just the semicolon.  The semicolons separate the three different parts of the for loop control.  Then the second expression, the execution test, is executed, and if it is true, control passes into the loop, otherwise the body of the for loop is skipped and control passes to the program segment following the for loop.  When the control passes to the bottom of the loop (i.e, at the "}"), the third, the increment expression is executed.  Again, this can be empty.  At that point, the second expression, the continuation test, is evaluated.  If it is true, enter the body of the loop; otherwise go to the program segment following the for loop.  There is a bizarre twist here:  an empty continuation test is considered to be a logic true.  In other words, for(;;) is an infinite loop.

for loop
  for (k = 0; k < lim; k++)  
     printf("Line %d, index %d\n", k+1, k);  
Matching while loop
  k = 0;             // Initialization  
  while ( k < lim )  // Loop execution test  
     printf("Line %d, index %d\n", k+1, k);  
     k++;            // increment  

Example of nested for loop:  print column headers on the output.  Example:

         1         2         3         4         5         6         7

Specimen:   Header.txt   Header.c   Header.exe

Further example:  the counting loop from 22 January done with a for loop.  This also shows an example with no initialization in the second for loop.
CountingLoop.txt   CountingLoop.c   CountingLoop.exe

We will see lots of  for loops when we get into using arrays.

Enrichment material:
Example:  benchmarking code.  Because of the speed of computers, it is necessary to do millions of passes through a program segment for it to take noticeable time unless it is extremely complex.

<time.h> provides access to clock(), a function that returns the number of "clock ticks" that have occurred since the program began.  By capturing this before the loop segment begins, and then when the loop segment ends, we can get the number of elapsed ticks.  These become seconds when divided by CLOCKS_PER_SEC, an integer constant defined in <time.h>.  You just have to insure that the division is a floating point division.  This specimen program tries to see if there is any significant advantage to using conditional statements versus the equivalent if / else if .... structure.
timer.txt   timer.c   timer.exe