Lecture 11: Repetition and Loop Statements (Chapter 5)

 

Loops provide a means of repeating a series of executable statements (the loop body) as long as the condition controlling the loop usually found in the loop header) evaluates to TRUE.

  

The loop control condition is a logical expression that evaluates to either TRUE or FALSE.  Often, this expression involves a single variable, called a loop control variable.

 

The loop body is a block of code that contains the series of executable statements associated with the loop condition. The loop body will be repeated as long as the loop condition evaluates to TRUE, when the loop condition evaluates to FALSE the loop will stop. Each time the loop body is repeated the loop is said to have gone through iteration.

 

There are 3 types of repetition statements:

1. WHILE loops

2. FOR loops

3. DO-WHILE loops

 

WHILE loops

 

Basic form of the while loop

 

 

while (condition)

{

statement1;

statement2;

}

 

 

 

 

Standard way of using while loop

 

  // Before the loop construct, initialize any loop control

  //   variable(s) (the variables appearing in the condition

  //   in the loop header)

 

  initialize; // initialize loop control variable(s);

 

  while (condition) /* Loop header, test the loop condition */

  {

      // Loop body -- the dependent statements

      //    Dependent statements that are executed while (as

      //    long as) the condition evaluates to TRUE.  If the

      //    condition is false the first time, the loop will

      //    not execute at all.                               

 

      statement1;

      statement2;

          ...

 

      // The loop body must contain statements that update

      //    the loop control variable(s) These statements are

      //    found at the bottom of the loop body.

 

      update; // update loop control variable(s);

   }

 

   // First executable statement following the while construct

   // Control is transferred here when loop condition becomes false.

 

 

Note: The dependent statement or statements contained within the curly braces { } are executed each time the condition [contained within the parenthesis ( ) in the loop header] evaluates to TRUE.  When the condition contained within the parenthesis ( ) evaluates to FALSE, the loop stops and the dependent statement or statements contained within the curly braces { } are NOT executed.  Program execution continues with the first executable statement after the end of the "while" loop construct.

 

Example 1:  A while loop to display seven stars.

 

int countstars, n;

n = 7;

countstars = 0;            /* initialize loop control variable */

while (countstars < n)     /* countstars is loop control variable */

{

   printf(‘*’);            /* loop body starts here */

   countstars++;           /* or countstars = countstars + 1;  (update) */

}

 

Note the pattern used here.  We initialize the loop control variable, or countstars, then test it (in the loop control expression).  Finally, at the bottom of the loop, we “update” the countstars.

 

FOR loops

 

Basic form of the for loop

 

 

for (initialize; condition; update)

{

statement1;

statement2;

}

 

 

 

Example 2: A for loop to display seven stars.

 

int countstars, n;

n = 7;

for (int countstars = 0; countstars < n; countstars++)

{

   printf(‘*’);

}

 

Note: The for loop does exactly the same thing as the standard way of using while loop in the example above.

 

Note:  The for loop construct is used exclusively as a counting loop unlike the while and do-while loop constructs which can be used for either counting or more complex loops.

 

Note:  The loop "header" in a for loop construct contains the initialization of the condition variable(s), the condition test (involving these variables(s)), and the update of the condition variable.  As a result, the variable that is used in the loop condition is initialized and updated in the loop header as opposed to the loop "body".

     

Note H: 

-   The initialization of the variable(s) used in the loop condition occurs ONCE at the start of the loop execution. 

-   The condition testing occurs BEFORE each loop iteration and continues with each loop iteration until the condition evaluates to FALSE and the loop stops. 

-   The update of the variable(s) used in the loop condition occurs AFTER each loop iteration has completed but BEFORE the loop condition is evaluated and continues with each loop iteration until the condition evaluates to FALSE and the loop stops.

 

 Note: The dependent statement or statements contained within the curly  braces { } are executed each time the condition evaluates to TRUE. When the condition evaluates to FALSE, the loop stops and the dependent statement or statements contained within the curly braces { } are NOT executed.  Program execution continues with the first executable statement after the end of the for loop construct.

 

DO-WHILE loops

 


Basic form of the do-while loop

 

 

do

{

statement1;

statement2;

} while (condition);

 

 

 

Standard way of using while loop

 

// Before the loop construct, initialize any loop control

//   variable(s) (the variables appearing in the condition

//   in the loop header)

 


initialize; // initialize loop control variable(s);

 

do

{

// Loop body -- the dependent statements.

//    Dependent statements that are executed at least

//    once and then repeated as long as the loop condition

//    (below) evaluates to TRUE                               

 

statement1;

statement2;

          ...

 

// The loop body must contain statements that update

//    the loop control variable(s) These statements are

//    found at the bottom of the loop body.

 

update; // update loop control variable(s);

} while (condition);

 

// First executable statement following the do-while construct

// Control is transferred here when loop condition becomes false.

 

Example 3:  A do-while loop to print 7 stars.

 

int countstars, n;

n = 6;               /* observe that n is 6, not 7 here! */

countstars = 0;      /* initialize loop control variable */

do                   /* countstars is loop control variable */

{

   printf(‘*’);      /* loop body starts here */

   countstars++;     /* update */

} while (countstars < n)

 

Note:  The semi-colon ; is required after the closing parenthesis at the end of the do-while loop construct!

 

Note: The variable or variables used in the loop condition normally should be initialized BEFORE the loop is entered, and then updated (in the loop body) before the loop condition is tested at the end of each repetition of the loop body.

 

Note: The dependent statement or statements contained within the curly braces { } are executed at least once.  They are then repeated each time the condition [contained within the parenthesis  ( ) in the last line of the loop] evaluates to TRUE.  When the condition contained within the parenthesis ( ) evaluates to FALSE, the loop stops and the dependent statement or statements contained within the curly braces { } are NOT repeated.  Program execution continues with the first executable statement after the end of the do-while loop construct.