C++ for loop


The for loop


The for allows us to specify three things about a loop in a single line:

(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
(c)Increasing the value of loop counter each time the program segment within the loop has
been executed.

First of all the value is initialized, then in the loop the condition is checked and after processing
the body of the loop, it will increment or decrement the loop according to requirement.

In this loop structure, more than one variable can be initilized.

Syntax:

for(initialisation; test-condition; incre/decre)
{
statements;
}

In above syntax, the given three expressions are seperated by ';' (Semicolon)

Features :

  • More concise
  • Easy to use
  • Highly flexible
  • More than one variable can be initilized.
  • More than one increments can be applied.
  • More than two conditions can be used.

Simple Example :


#include <iostream.h>
#include <conio.h>

void main()
{
int a;
clrscr();
for(i=0; i<5; i++)
{
cout << "\nHi"; // 5 times
}
getch();
}

Output :

Hi
Hi
Hi
Hi
Hi


Description :

First the variable i is initialized to value 0 and then the condition is checked which is true since 0<5, then the control goes inside the loop and it print 'Hi', and then the value of i is incremented to 2. This process continues till the value of i becomes greater than 5 that is 5 times 'Hi' is displayed.

Note :

It is important to note that the initialization, testing and incrementation part of a for loop can be replaced by any valid expression. Thus the following for loops are perfectly ok.

for ( i = 10 ; i ; i -- ) cout << i ;

for ( i < 4 ; j = 5 ; j = 0 ) cout << i ;

for ( i = 1; i <=10 ; cout << i++ )
;

for ( cin >> i ; i <= 10 ; i++ )
cout << i ;


Nested for loops :


The way if statements can be nested, similarly for can also be nested. To understand how nested loops work, look at the program given below:

/* Demonstration of nested loops */

main( )
{
int r, c, sum ;
for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */
{
for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */
{
sum = r + c ;
cout << "r = " << r;
cout << "c = " << c;
cout << "sum = " << sum;
}
}
}

When you run this program you will get the following output:
r = 1 c = 1 sum = 2
r = 1 c = 2 sum = 3
r = 2 c = 1 sum = 3
r = 2 c = 2 sum = 4
r = 3 c = 1 sum = 4
r = 3 c = 2 sum = 5

Here, for each value of r the inner loop is cycled through twice, with the variable c taking values from 1 to 2. The inner loop terminates when the value of c exceeds 2, and the outer loop
terminates when the value of r exceeds 3.

Multiple Initialisations in the for Loop

The initialisation expression of the for loop can contain more than one statement separated by a comma. For example,

for ( i = 1, j = 2 ; j <= 10 ; j++ )

Multiple statements can also be used in the incrementation expression of for loop; i.e., you can increment (or decrement) two or more variables at the same time. However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators.

Use of multiple statements in the initialisation expression also demonstrates why semicolons are used to separate the three expressions in the for loop. If commas had been used, they could
not also have been used to separate multiple statements in the initialisation expression, without confusing the compiler.

Note :

We will discuss more for loops programs in our C++ programs section

1 comments:

Nikola Anđelić said...

What are loops and how we can use them?

In general a loop is a sequence of statements which is specified once but which may be carried out several times in succession. The function of the loop is to repeat the calculation a given number of times until it reaches a certain value. The code inside the loop is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely. We can divide loops into:
1) Count-controlled loops,
2) Condition – controlled loops,
3) Collection - controlled loops,
4) General iteration,
5) Infinite loops,
6) Continuation with the next iteration,
7) Redo the current interation and
8) Restart loop

For more information please visit: http://megacplusplustutorials.blogspot.com

Post a Comment