Loops (for, while, do)

 

Loop: Group of instructions computer executes repeatedly while some condition remains true.

 

for

 

the for statement has the form:

for (initial_value , test_condition , step){
   // code to execute inside loop
};

 
  1. initial_value sets up the initial value of the loop counter.

  2. test_condition this is the condition that is tested to see if the loop is executed again.

  3. step this describes how the counter is changed on each execution of the loop ( increment or decrement ).

Here is an example:

// The following code adds together the numbers 1 through 10
// this variable keeps the running total
int total=0;

// this loop adds the numbers 1 through 10 to the variable total
for (int i=1; i < 11; i++){
   total = total + i;
}

So in the preceding chunk of code we have:

  1. initial_condition is int i=0;

  2. test_condition is i < 11;

  3. step is i++;

So, upon initial execution of the loop, the integer variable i is set to 1. The statement total = total + i; is executed and the value of the variable total becomes 1. The step code is now executed and i is incremented by 1, so its new value is 2.

 

The test_condition is then checked, and since i is less than 11, the loop code is executed and the variable total gets the value 3 (since total was 1, and i was 2. i is then incremented by 1 again.

The loop continues to execute until the condition i<11 fails. At that point total will have the value 1+2+3+4+5+6+7+8+9+10 = 55.

 

Initialization and increment

 

Can be comma-separated lists such as:

for (int i = 0, j = 0;  j + i <= 10; j++, i++)

    printf( "%d\n", j + i );

Arithmetic expressions

 

Initialization, loop-continuation, and increment can contain arithmetic expressions.  If x equals 2 and y equals 10

        for ( j = x; j <= 4 * x * y; j += y / x )

is equivalent to

        for ( j = 2; j <= 80; j += 5 )

 

All three expressions of a for loop are optional

 int i=0;
for(;;)
{
    if(i<10)
       {
         printf("hi");
         i++;
        }
    else
         break;
}

while

 

The while statement has the form:

while ( condition) {
   // code to execute

   };

initial_value

while ( condition) {
   // code to execute

   step
};

As an example, let's say that we wanted to write all the even numbers between 11 and 23 to the screen. The following is a full C++ program that does that.

 
#include <stdio.h>

int main(){
   // this variable holds the present number
   int current_number = 12;   // initial value (1)

   // while loop that prints all even numbers between
   // 11 and 23 to the screen
   while (current_number < 23) // check condition (2) 
            {
              printf("%d", current_number );
              current_number += 2;  // increment part(3) 
             }
  }   
 

The preceding example prints the value of current_number to the screen and then adds 2 to its value. As soon as the value of the variable current_number goes above 23, the while loop exits and the next line is executed.

The output of the preceding program would be:

12
14
16
18
20
22

 

The do/while repetition structure

 

Similar to the while structure

Condition for repetition tested after the body of the loop is performed

All actions are performed at least once

 

Format:

do {
        statement;
} while ( condition );

 

Example: Prints the integers from 1 to 10

counter=1;
do {
       printf( "%d  ", counter );
} while (++counter <= 10);

Notes about the loops: