for loop in C++

Hello Readers,

Today, we’ll discuss the for loop in C++. Before studying the for loop, let’s see why we even need it. There is absolutely no point in understanding something which has of no use.

Note: If you’ve already studies for loop, you directly jump to problems.

To understand the use of for loop, let’s take an example, you want to print first 10 numbers, you’ll simple print using “cout”, but, if I ask you to print first 1000 numbers, what you’ll do? Will do follow the same approach as of above? No, you’ll not.

Well, let me inform you that printing numbers is one of the most basic and important task in programming. There are hell lot of complex problems where you want to repeat a part of code. In the above example, you’ll basically put a “cout” statement inside a for loop that will run 1000 times. It’s just hardly 9-10 lines of code; isn’t it great? You’re printing 1000 numbers with just 9-10 lines of code. That’s the power of loop.

In this article, we’ll going to study for loops, there are other types of loops like while loop, do-while loop etc., we’ll see them in other tutorials.

for loop syntax

Below is the syntax of for loop in C++:

for(initialization; exit condition; iteration){
    // code statement
}

Now, let’s understand the syntax in detail. The initialisation is the initialisation of a variable you want to initialise because that variable may be used for the exit of the loop.

The exit condition decides the exit of your program from the loop. Exit condition is necessary because otherwise your loop may not end and because of that, your program will go in infinite loop.

The iteration part is the iteration of the variable, based on which the exit condition will be decided. The iteration will decide the number of times your loop will run.

Now, let’s look at the control flow of the for loop structurally, as it will help in understanding, how the for loop internally works.

  • Save
for loop control flow

So, in the above image, one can clearly see that the initialisation step is performed only once & at every step compiler checks the exit statement so that whenever the exit statement turns to be false, the control comes out the of loop.

Lastly the iteration part comes at the end when the compiler executes all the code inside the for loop.

So, enough of the theory now, & let’s practically understand the for loop working with the same example of printing 1000 numbers. Now, printing first 100 numbers, we’ll take as 1-1000.

Below is the C++ code for printing first 1000 numbers.

#include <iostream>
using namespace std;
int main(){
	int i; // variable declaration
	for(i=1;i<=1000;i++) // loop
        {
		cout<<i<<endl;  // code inside for loop
        } // closing braces of for loop
        return 0;
}

Before moving to the syntax, let’s discuss the logic first. We’ve declared an integer variable i. After that we initialised i=1, apply the loop till i<=1000 and iterate I, one by one.

Now, let’s apply all the above concepts that we have studied so far in this example.

Let’s analyse the code line by line.

int i;

The above line is the variable declaration. I hope that you already know about this. If you don’t know about this, just click here.

for(i=1;i<=1000;i++)

In the above code snippet, “i=1” is the initialisation.

“i<=1000” is the exit condition which if turns false, the control will reach out of the loop.

“i++” is the iteration, which increases “i”, one-by-one.

cout<<i;

“cout<<i” is the code inside the for loop, which executes in every iteration. Since “i” is incrementing one-by-one, the program will print 1,2,3 and so on since the value of “i” is also 1,2,3.. and so on.

So, that was all about the for loop in c++, if you have any doubts regarding that write it in the comment section.

We highly recommend you to practice this as much as you can. You can practice on online compiler here. However, if you want to download your own C++ compiler, you can checkout here.

If you enjoy reading this tutorial, & most important understood it correctly, don’t forget to share it with your friends. Next tutorial will be out soon. Till then stay tuned.

Leave a Reply

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

Share via
Copy link
Powered by Social Snap