loops

Examples of the for loop in C++ 

1)

int i;
for ( i=0;  i<10;  i++)
   sum +=i;

2)

int i;
for (i=1; i<10; i++)
{
   sum += i;
   cout << "Current sum is: " << sum;
}

3)

int maxValue=100;
for (int i=5; i < maxValue; i+=5)
   cout <<  i << "\n";

4)

int i=0;
int maxValue = 10;
for ( ; i < maxValue; i++)
   cout << i << " ";

Example of the while loop in C++

1)

int x = 10;
while ( x > 10 ) 
{
   cout << "x = " << x << endl;
   x -= 1;
}


Example of the do while loop in C++

int x=5;
do
{
   x = x - 1;
} while ( x > 0);


No comments:

Post a Comment