In this tutorial, we’ll explore and learn about for loops in C++. This is an extremely basic and easy-to-understand lesson in which we will cover about syntax and usage of for loops in C++ in a great detail. We will also touch upon some examples which will help us in understanding the concept in a practical setting. Make sure to stick with us throughout this interesting journey of learning for loops in C++.

What are Loops?

In programming languages, a loop is a functionality which allows us to run a sequence of instructions until a certain condition is matched. Generally, there are two common kinds of loops supported in almost every programming language; while loop and for loop.

If you’re not familiar with while loops in C++ and want to learn about them, please see our detailed tutorial here

Similar to other programming languages, C++ also offers the functionality of a for loop. In practical programming, loops are frequently used to achieve meaningful results out of the program. Without further ado, let’s jump straight into learning for loops in C++.

C++ For Loop Syntax

In this section, we’ll explore the basic syntax of for loop and explain it with the help of an example. 

We use the following syntax to write a for loop in C++.

for (<your_initialisation_variable>; <your_condition>; <variable_increment>)
{
  // the code chunk which will execute if the program enters the loop.
}

Confusing, yeah? Don’t worry. Let’s break it down. If you notice in above syntax, the for loop is dependent on three parameters, each separated by a semi-colon ;. The three parameters are <your_initialisation_variable>, <your_condition> and <your_increment_value>.

We need to define a starting point, a terminating condition and some level of increment in our starting point so that the loop can reach a terminating condition. In for loop, we do this all at once as shown in the above syntax. 

Let’s look at an example to better understand it. 

Example #1

for (int i = 0; i < 10; i++)
{
  cout << i << " " << end;
}

In the above example, int i = 0 is our starting point of the loop. This is where the loop initiates. Moving further, i < 10 is our terminating condition. It means that the loop will execute until the value stored in the variable i is less than 10. As soon as the value is greater than or equal to 10, the loop halts. Finally, i++ is the increment factor to make sure that in each iteration we move closer to our terminating condition. In this case, i is increased by one in each iteration.

It should be clear from the above example that the loop will run 10 times and hence will produce the following result as the output. 

0 1 2 3 4 5 6 7 8 9

Let’s look at another example.

Example #2

int numberOfCodeZenTutorials = 1000;
for (int i = 0; i < numberOfCodeZenTutorials; i++)
{
  keepReadingCodeZenTutorials();
}

Above mentioned is a dummy example to understand for loops. In this example, the function keepReadingCodeZenTutorials() will be called 1000 times because the loop runs for 1000 times i.e. 0, 1, 2, 3, …, 999.

Easy, right? Let’s now have a quick activity to assess our learning so far. 

In the following code snippet, how many times will the phrase I am learning quite well! get printed?

for (int j = 10; j < 30; j+=1)
{
  cout << "I am learning quite well!" << endl;
}

If your answer is 20 times then you’re absolutely correct. Good job! Congratulations for coming this far. 

Why not try another one? This time a bit tricky.

for (int k = 0; k < 30; k+=2)
{
   cout << "Hmm, something is suspicious!" << endl;
}

In the above code, how many times will the phrase Hmm, something is suspicious! be printed?

Hint: Keep a close eye on the increment factor.

Correct answer: 14 times (because ‘k’ is incremented by 2 in each iteration)

Key Note

That’s all about for loops in C++. I hope it was an exciting journey and was fun learning about the implementations and use cases of for loop in one of the most popular programming languages out there. You don’t get to gauge your skills unless you apply the learning in some kind of a practical setting. So, don’t forget to do some hands-on exercises related to this lesson.

If you’re new to C++ programming and don’t know about conditional statements then make sure to check our tutorial about conditionals (if, else if and else) in C++. Try figuring out some interesting applications where you can combine for loop and conditional statements to make interesting programs in C++ and share it with us. 

If you wish to learn more about C++, you can check out our collection of C++ tutorials.