January 13, 2021

C++ Loops

A loop allows you to execute a statement or groups of statements multiple times based on certain condition. In this lesson, we would consider four types of loops. We would also examine a few loop control statements.

  1. while Loop
  2. for Loop
  3. do..while Loop
  4. Nested Loop
  5. Loop Control Statements

 

1. while Loop

A while loop is used to execute a statement or group of statements repeatedly so long as a given condition remains true.

The syntax is:

while(condition) {
   statement(s);
}

The flow diagram for the while loop is shown below:

While Loop

The code below is an example of the while loop.

int main () {
   int x = 0;

   // while loop
   while( x < 10 ) {
	   //Statement
      cout << "x =  " << x << endl;
      x++;
   } 
   return 0;
}

If you run the above code,  you’ll have the output:

x =  0
x =  1
x =  2
x =  3
x =  4
x =  5
x =  6
x =  7
x =  8
x =  9

 

2. for Loop

In the case of a for loop, the  statements are executed a specific number of times. So, the number times the loop with iterate is specified. The basic syntax of the for loop is:

for ( init; condition; increment ) {
   statements;
}
  • init is the loop variable
  • condition is the conditional statement to the checked each time. Normally  the check is if the loop variable have reached certain threshold
  • increment is by what step to increase the loop variable after each iteration.

The flow diagram for the for loop is given below:

For Loop

The code below illustrates the for loop

int main () {
   // for loop
   for( int x = 1; x < 10; x = x + 1 ) {
      cout << "value of x is " << x << endl;
   }
 
   return 0;
}

When code execute, x is displayed 10 times as well

Note that in the for loop, the loop variable is declared and initialized inside the for statement.

 

3. do..while Loop

Similar to the while loop, the do…while loop executes statements a number of times. However, unlike the while loop, the condition is tested at the end of the loop. Therefore, this loop is guaranteed to execute at least, once.

Basic syntax for the do..while loop is:

do {
   statement;
} 
while( condition );

The flow diagram is shown below:

Do While Loop

An example of the do…while loop is given below.

int main () {
   // Local variable, x
   int x = 0;

   // do loop statements
   do {
      cout << "value of x is " << x << endl;
      x = x + 1;
   } while( x < 10 );
 
   return 0;
}

As before, if you run this code,  x is printed 10 times from 0 to 9.

 

4. Nested Loop

This is simply a situation when you nest a loop inside another loop. For example, we want to print the running sums of number between 1 and 10. So like this:

  • 1 = 1
  • 1 + 2 = 3
  • 1 + 2 + 3 = 6
  • 1 + 2 + 3 + 4 = 10

So the output would be 1, 3, 6, 10,….

Can you attempt it! Let’s use two nested for loop.

int main () {
	int sum;

	//External loop
	for(int i = 1; i <= 10; i++) {
		sum = 0;
		//Nested internal loop
		for(int j = 1; j <= i; j++){
			sum = sum + j;
		}
		cout << sum << ", ";
	}
   return 0;
}

The output of the code would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 

 

5. Loop Control Statements

Discussion  on loop is not complete without learning about loop control statements.

Loop control statements allow you to alter the normal loop execution sequence. For instance, you saw from switch statement that we used break statement to exit from the switch statements. Lets  look at three loop control statements.

break statement – this statement is used to completely terminate the loop and exit to the statements following the loop

continue statement – this statement is used to terminate the current iteration of the loop

goto statement – this statement is used to transfer control to another part of the program with a label. Goto statements are not recommended for most scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *