for Statement

Let’s see what we do in a loop. In a loop, we initialize variable(s) at first. Then we set a condition for the continuation/termination of the loop. To meet the condition to terminate the loop, we affect the condition in the body of the loop. If there is a variable in the condition, the value of that variable is changed within the body of the loop. If the value of the variable is not changed, then the condition of termination of the loop will not meet and loop will become an infinite one. So there are three things in a loop structure i.e. (i) initialization, (ii) a continuation/termination condition and (iii) changing the value of the condition variable, usually the increment of the variable value.

To implement these things, C provides a loop structure known as for loop. This is the most often used structure to perform repetition tasks for a known number of repetitions. The syntax of for loop is given below.


We see that a 'for statement' consists of three parts. In initialization condition, we initialize some variable while in continuation condition, we set a condition for the continuation of the loop. In third part, we increment the value of the variable for which the termination condition is set.

Let's suppose, we have a variable counter of type int. We write for loop in our program as


This 'for loop' will print on the screen 0, 1, 2 …. 9 on separate lines (as we use endl in our cout statement). In for loop, at first, we initialize the variable counter to 0. And in the termination condition, we write counter < 10. This means that the loop will continue till value of counter is less than 10. In other words, the loop will terminate when the value of counter is equal to or greater than 10. In the third part of for statement, we write counter = counter + 1 this means that we add 1 to the existing value of counter. We call it incrementing the variable.

Now let's see how this loop executes. When the control goes to for statement first time, it sets the value of variable counter to 0, tests the condition (i.e. counter < 10). If it is true, then executes the body of the loop. In this case, it displays the value of counter which is 0 for the first execution. Then it runs the incrementing statement (i.e. counter = counter + 1 ). Thus the value of counter becomes 1. Now, the control goes to for statement and tests the condition of continuation. If it is true, then the body of the loop is again executed which displays 1 on the screen. The increment statement is again executed and control goes to for statement. The same tasks are repeated. When the value of counter becomes 10, the condition counter < 10 becomes false. Then the loop is terminated and control goes out of for loop.

The point to be noted is that, the increment statement (third part of for statement) is executed after executing the body of the loop. Thus for structure is equivalent to a while structure, in which, we write explicit statement to change (increment/decrement) the value of the condition variable after the last statement of the body. The for loop does this itself according to the increment statement in the for structure. There may be a situation where the body of for loop, like while loop, may not be executed even a single time. This may happen if the initialization value of the variable makes the condition false. The statement in the following for loop will not be executed even a single time as during first checking, the condition becomes false. So the loop terminates without executing the body of the loop.


0 comments:

Post a Comment

 

introduction to programming Copyright © 2011-2012 | Powered by Blogger