The for Loop
Syntax
for (variable initializations; conditional value; variable increment)
{
//code
}
Examples
for(int i=0; i<10; i++)
{
cout << "i = " << i;
}
int sum;
for(int i=0, sum = 1; i<20; i++, sum += sum + i);
cout << sum << endl;
int sum = 1;
for(int i=0; i<20; i++)
{
sum += sum + i;
}
cout << sum << endl;
The do while Loop
Syntax
do
{
//code
} while(conditional value);
Examples
int sum = 1, i = 0;
do
{
sum += sum +i;
i++;
} while ( i < 20);
cout << sum;
The while Loop
Syntax
while(conditional value)
{
//code
}
Examples
int sum = 1, i = 0;
while( i < 20)
{
sum += sum + i;
i++;
}
cout << sum;
|