More Loops

Read: §7.4 of the Textbook, pp 80-1
Read: §3.6 of the C Programming Language, p 56

Review: while loops
Format:

while (stay-in condition)
    statement
The top line is called the loop header. The statement is called the body of the loop. It is often a compound statement.

Semantics: The stay-in condition is checked before the potential execution of the body. Each time the stay-in condition is checked it evaluates to either true or false. If it is true, we get a repetition: the body is executed and then the stay-in condition is checked again. This continues so long as each check returns true. When the check returns false, the body is skipped; execution continues with whatever comes afterwards. If stay-in condition never becomes false, we have what is known as an inifinite loop. If the stay-in condition is false the very first time we get zero executions of the body.

New Loops: The for Loop

The new loops discussed below are not really necessary, but they make the programming a bit easier.

for loops
Format:

for(init; stay-in condition; update)
     statement
Semantics: Once again there is a header and a body. There are 3 parts inside the parentheses following the keyword for. The init code is executed once when the for loop is started. Next the stay-in condition is checked. If it is true, the body is executed, then the update is executed and the stay-in condition is checked again. So long as it is true we repeat this pattern. When it becomes false the execution of the for loop is finished. Note: the init is only executed before the first time the stay-in condition is checked.

Complication: Suppose the for loop is a statement (header and body) in the body of another loop. Then each repetition of the body of the containing loop will result in restarting the for loop. Each time that happens the init statement will be re-executed.

Below you see a while loop that is equivalent to the for loop above.

init
while (stay-in condition) {
   statements from the body
    .
    .
   update
}
An Example: Finding the length of a string.
char str[100];
  .   //some string is put into the array starting at str[0].
  .
int i;
for(i=0; str[i] != 0; i++)
    ;
printf("length=%d\n", i);
The for loop in this example appears to not have a body. It has one! The compiler inserts the null statement right before the semi colon. This statement does nothing. The update part of the header keeps increasing i by one and then the stay-in condition is checked to see whether str[i] is still not equal to zero. When str[i] is equal to zero the loop quits. Variable i is the index of the zero and the length of the string.

NOTE: often a semicolon directly after a while or for header is an ERROR. New programmers put them there because they think the header is a statement. But while and for headers are NOT by themselves statements. The best way to think of them is that they are statement prefixes. They're something that you put in front of a statement to make a new one.

New Loops: The do while Loop

do while loops
Format:

do
  statement
while (condition);
Semantics: The keyword do marks the top of the loop. The while (condition); marks the bottom. Each time a repetition of the statement finishes the condition in the while is checked. It it is true, we get another repetition of the statement and another check of the condition. This continues until the condition becomes false. When that happens execution continues with whatever comes after the loop. NOTE: semicolon after the condition is required. It marks the end of the statement (unit) composed of the do, the statement, the while with its condition. The statement can be compound. Unlike the while loop, this loop guarantees at least one repetition since the condition is not tested until the body has executed.

Example: Problem: Get an integer from the user. Then find the first positive multiple of 10 (10, 20, 30, . . .) that when added on to the user's number makes a sum greater than 100.

printf("Please enter your number\n");
int num;
scanf("%d", &num);
int mul10 = 0;
do 
  mul10 += 10;
 while (num+mul10 <= 100);
printf("The multiple of 10 is %d\n", mul10);
Since we started with mul10=0, we need at least one repetition to get the first positive multiple of 10.

Next:Functions