For repeating a block, the general form is:
Here, initialization sets the loop control variable to an initial value. Condition is an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps running. Increment is an expression that determines how the loop control variable is incremented each time the loop repeats successfully (that is, each time condition is evaluated to be true).
Example
// this program uses the for loop to print the numbers from 1 through 100
#include <iostream.h>
main()
{
int x;
for(x=1;x<=100;x=x+1) cout << x <<' ';
return 0;
}
Example
//This program finds the square roots of all numbers between 1 and 100
#include <iostream.h>
#include <math.h>
main()
{
int num;
double sq_root; // notice how we write the variable name
for(num=1;num<=100;num++) //increments can be written using ++
{
sq_root=sqrt(num);
cout << "square root of "<<num <<" is" <<sq_root <<endl;
}
return 0;
}
The for loop can proceed in a positive or negative fashion, and it can increment the loop control variable by any amount. For example, the following program prints numbers between 100 to -100 in increments of 5.
#include <iostream.h>
main()
{
int k;
for(k=100;k>=-100;k=k-5) cout<<k<<' ';
return 0;
}
An important point about the for loops is that the conditional expression is
always tested at the top of the loop. This means that the code inside the loop
may not be executed at all if the condition is false to begin with. For
example
for(x=10;x<4;x++) cout<<x; //will not be executed
This loop will never be executed because its control x is greater than 4 when the loop is first entered. This makes the conditional expression x<4 false at the top of the loop.
The for is one of the most versatile statements in the C++ language because it allows a wide range of variation from its traditional use. For example, multiple loop control variables can be used. Consider the following fragment of code:
for(x=0,y=10;x<=10;++x,- y) cout<< x <<' '<<y;
Here, commas separate the two initialization statements and the two increment expressions. This is necessary in order for the compiler to understand that there are two initialization and two increment statements. In C++, the comma is an operator that essentially means "do this and this". You can have any number of initialization and increment statements, but in practice, limit yourself to two or three. The condition controlling the loop may be any valid C++ expression.
Another aspect for the for loop that is different in C++ than in many computer languages is that pieces of the loop definition need not be there. For example, if you want to write a loop that runs until the number 24 is typed in at the keyboard, it could looks like this:
#include <iostream.h>
main()
{
int x;
for(x=0;x !=24;){
cout<<"enter a number:";
cin>> x;
}
return 0;
}
The increment portion of the for definition is blank. This means that each
time the loop repeats, x, is tested to see whether it equals to 24, but no further
action takes place. If however, you type 24 at the keyboard, the loop condition
becomes false and the loop exits. The C++ for loop will not modify the loop
control variable if no increment portion of the loop is present. Another
variation on the for loop is to move the initialization section outside the loop,
as shown in this fragment:
x=50;
for(;x>=5;) {
cout << x;
-x; // you can write this also as x-
}
Here, the initialization section has been left blank, and x is initialized before the loop is entered. Placing the initialization outside of the loop is generally done only when the initial value id derived through a complex process that does not lend itself to containment inside the for statement. Notice that in this example the increment portion of the for is located inside the body of the loop.