C++ For Loop: Explained with Examples

Mastering C++ for loops: A step-by-step guide to code control

What is for loop in C++

The for loop is a way to iterate a certain block of code a given number of times.

You may exit a for entirely or may code to exit only one or more iterations by using specified statements – explained in the sections below.

How to write for loop in C++ – the Syntax

The general syntax of writing a for loop in C++ is:

for (initialization; condition expression; update expression) {

  // code block to be executed

}

In the above syntax:

Part Description
initialization e.g. x=1. This is an initialization expression i.e. the loop counter is initialized here. This part executes only once.
Condition expression In this part of the for loop, the condition is given. If it evaluates as true, the code block inside the curly braces is executed. E.g. x=10.
Update expression After executing the statements in the above part, the control comes to this part. There, the value of the variable can be incremented/decremented. E.g. x++

 

Tip: The for loop is used when you know how many times a block of code should be executed. Otherwise, you may use a while loop.

A simple example of using a for loop

  • In the example below, we will simply execute the for loop ten times.
  • In each iteration, we will display the value of a variable.
  • The target is to display values from 1 to 10.

Have a look at the code below and its output:

#include <iostream>

using namespace std;

int main() {

  for (int x = 1; x <= 10; x++) {

    cout << x << "\n";

  }

  return 0;

}

Output:

1
2
3
4
5
6
7
8
9
10

Reversing the display in for loop

In this example, we will use the decrement operator in the third part of the for loop.

  • The value of the variable is initiated as 10 and the condition is to execute the loop till the value is greater than or equal to 1.
  • In each iteration, the value of the variable x is decremented by 1.
  • Inside the curly braces, we will execute the current value of the variable.
  • The task is to display the number from 10 to 1 in this example.
#include <iostream>

using namespace std;

int main() {

  for (int x = 10; x >= 1; x--) {

    cout << x << "\n";

  }

  return 0;

}

CPP-for-loop-decrement

Display value with the gap of two

This example displays the value of a variable with a gap of 2. In each iteration, we will increase the value by two in the third expression of the for loop:

#include <iostream>

using namespace std;

int main() {

  for (int x = 0; x <= 10; x=x+2) {

    cout << x << "\n";

  }

  return 0;

}

Output:

0
2
4
6
8
10

Displaying text in the for loop

This example displays the text as well as the current value of the variable in the for loop.

#include <iostream>

using namespace std;

int main() {

  for (int x = 1; x <= 10; ++x) {

          cout << "Current value of x= " << x << endl;

  }

  return 0;

}

CPP-for-text

Exiting an iteration by using the continue statement in for loop

The task of this example is to display the numbers from 1 through 10 and omit the loop to display the number 5.

For this, we have to exit the loop only once as the variable value reaches five. Have a look:

#include <iostream>

using namespace std;


int main() {

  for (int x = 1; x <= 10; ++x) {

  if (x == 5) {

    continue;

  }

     cout << "Current value of x= " << x << endl;

  }

  return 0;

}

Output:

Current value of x= 1
Current value of x= 2
Current value of x= 3
Current value of x= 4
Current value of x= 6
Current value of x= 7
Current value of x= 8
Current value of x= 9
Current value of x= 10

As you saw in the above output, number 5 is not displayed.

This is accomplished by using the continue statement. So, the continue statement is used to break the iteration once whereas the for loop keeps on iterating until the given expression is true in the third part of for loop.

Using the break statement example to exit the loop

For exiting the for loop completely, you may use the break statement as the given condition is met.

In this example, we will exit the for loop as the value of the variable reaches 6 and the remaining numbers will not display.

See the code and output below:

#include <iostream>

using namespace std;

int main() {

  for (int x = 1; x <= 10; ++x) {

  if (x == 6) {

    break;

  }

     cout << "Current value of x= " << x << endl;

  }

  return 0;

}

CPP-for-break

As you saw in the above output, the variable is displayed till value 5.

In the next iteration, as the value of the x is 6, the if condition is evaluated as true. There we used the break statement and rather than displaying 6, 7, 8, 9, and 10, the loop exited.

Iterating through an array example

Though this is an advanced level example for beginners – just for the taste, we are using a string array and for loop. In the array, we stored the names of five fruits.

By using a for loop, we will iterate through string array elements and display it’s values:

#include <iostream>

using namespace std;


int main() {

  string fruits[5] = {"Apple", "Banana", "Mango", "Orange", "Watermelon"};

  for (int x = 0; x < 5; x++) {

    cout << fruits[x] << "\n";

  }

  return 0;

}

Output:

Apple
Banana
Mango
Orange
Watermelon

An example of a range-based for loop

For displaying array elements in the above example, you may also use a range-based for loop.

This is also a separate topic, but again for your taste here is an example.

#include <iostream>

using namespace std;


int main() {

  string fruits[5] = {"Apple", "Banana", "Mango", "Orange", "Watermelon"};


    for (string fru : fruits) {

        cout << fru << " ";

    }

  return 0;

}

CPP-for-range-based

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!