C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Tuesday, January 7, 2014

Working of for loop statement with examples

What is a For Loop Statement?


for Loop is a looping or iteration statement. It is used to execute a set of one or more statements for a specified number of times. It is a  counter-controlled loop.

Syntax of for Loop

for(initialization; condition; increment_decrement_expression)

{

   statement number 1;

   statement number 2;

   statement number 3;

   .

   .

   .

  statement number N;

}

Explanation of syntax of for loop

1. Initialization: 
It is used to assign a starting value to loop control variable also called a loop counter variable. Here we can initialize more than one variables separated by commas.
Example:  i = 1;
Example: int counter = 1;
Example: int i = 1, j = 10, k = 20;
2. condition
This condition is normally a relational expression. This expression will either produce a TRUE or FALSE. The statements in loop body will be executed only if the given condition is TRUE. If condition is FALSE then the loop body will not be executed.
Example: i < 11;
Example: counter <=10;
Example: j > 0;

3. Increment_Decrement_expression
It is used to increase or decrease the value of the counter variable. It is used to change the value of counter value in such a way that at some time it will make the given condition FALSE, so that the loop may be terminated. If this expression fails to produce a value that makes the given condition false, then for loop will never stop. Such for loop is called an infinite for loop the will never stop. It will continue executing loop body infinitely.
Example: i ++;
Example: counter --;
Example: j += 2;
Example: i = i + 1;
Example: k = k - 1;
4. Loop Body ( Statements 1 to N)
The statements in braces (curly brackets) are also called loop body. These are the statements to be executed again and again as long as the given condition remains TRUE. Note that if there is a single statement then the the use of curly brackets is optional. However, more than one statements make a block that must be placed within curly brackets.

How for loop works

 We will use the following example to explain the working of for loop in C++:
Example:
for( int i = 1; i <= 3; i ++)
     cout<<i<<"\t";
will produce the following output:

1        2        3  

working-of-for-loop-with-examples
Explanation
1. First of all counter variable i is initialized to 1.
2. Now condition i <= 3; is checked for the value of i = 1. So that 1 <= 3 is TRUE.
3. Since condition is TRUE, so loop body   cout<<i<<"\t"; will be executed and will print 1
4. Now increment expression  i ++  is executed. So that the value of i becomes 2.
5. Now condition i <= 3; is checked for the value of i = 2. So that 2 <= 3 is TRUE.
6. Since condition is TRUE, so loop body   cout<<i<<"\t"; will be executed and will print 2
7. Now increment expression  i ++  is executed. So that the value of i becomes 3.
8. Now condition i <= 3; is checked for the value of i = 3. So that 3 <= 3 is TRUE.
9. Since condition is TRUE, so loop body   cout<<i<<"\t"; will be executed and will print 3
10. Now increment expression  i ++  is executed. So that the value of i becomes 4.
11. Now condition i <= 3; is checked for the value of i = 4. So that 4 <= 3 is FALSE.
12. Since condition is False, so loop body   cout<<i<<"\t"; will not be executed and the for loop is terminated.

Example:

 for(int i = 1; i <=3; i ++)
     cout<<"Good ";
     cout<<"Luck";


will produce the following output:

Good Good Good Luck 


Example:
 for(int i = 1; i <=3; i ++)
{    

cout<<" Good ";
cout<<"Luck";

}
will produce the following output:
  Good Luck Good Luck Good Luck
Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels