The Best Guide to C++ For Loop : For Loops Made Easy

C++ is a general-purpose, object-oriented programming language. It is also a popular programming language as the TIOBE index ranks it 4th, and in this article, you will learn about a very important concept, i.e., C++ For Loop. 

What is C++ For Loop?

A for loop is a control flow statement that is used to execute a piece of code based on the validity of some conditions. It makes the code execute repeatedly. 

The syntax of for loop is:

Syntax : 

              for ( Initial state ; Condition ; Updation )

             {

               Body of loop

             }

Example :

c-for-loop

Fig: C++ For Loop Example

In the example above, the aim was to print the first 10 natural numbers. The condition that was given is - i less than equal to 10, which means the numbers will keep getting printed one after the other until i reaches 10.

Below you can see the output of the code.

c-loop-output.

 Fig: C++ For Loop Output

The Layout of a For Loop

A for loop has three parts. First is initialization or initial state, the second is the condition, and finally the updation. So let’s understand these three parts or expressions.

initial.

Initialization: Initialization or initial state initializes the starting value. In this part of the loop, you declare and initialize loop variables. It executes only once, during the starting of the for loop.

Condition: For every iteration of for loop, a condition is checked. If the condition is true, the loop will continue and the execution will jump to those statements that are after the loop body. Otherwise, the loop will end.

Updation: The updation step is used to update the loop variable. This step increments or decrements the loop counter.

As you have gathered a basic understanding of for loop, now it’s time to understand the working of for loop in C++.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Working of For loop

flowchart-for

Fig: Flowchart of for loop

  • When the execution of the for loop begins, it will execute at first the initialization section first. All the variables that you want to initialize are done here. It will execute only once; for example, if you are initializing a variable with 0 or 1, you don't have to do it every time.

  • After the initialization, the next task is to validate the condition and check whether the condition evaluates to true or false. If the condition results in true, then the code inside the body section would be executed, leading to the updation section. But if the condition results in false, then the code inside the body section wouldn't be executed.

  • After the completion of the body's execution, you will be headed towards the updation part, where there is an increment or decrement of loop variables.

  • After fulfilling the updation section, now again the condition is validated. If it returns true, then the entire process repeats for the updated variable. Else, the for loop terminates.

Now you will learn about excluding the expressions in C++ for loop

Excluding Expressions in for loop

As this article has discussed the three parts or expressions of for loop earlier, you can also write the for loop without these expressions as well. For example :

for ( ; ; )

{

   body

}

As you can see, there are no expressions in the for loop, so it will produce an infinite loop. However, it is not used much.

Here is another example in which you will not include initial expression and updation expression inside the loop statement.

excluding-exp

                                    Fig: Excluding expression of For loop

As you can see, you haven’t initialized and updated the looping variable. You are initializing "i" with 0 before the loop begins and incrementing the loop using the increment operator with "i" inside the loop body.

The output of the program is as shown below:

output-for-loop

Fig: Output of For loop

Nested for Loops

Nested for loops are mostly used while dealing with a two-dimensional array. It can be defined as a loop that is defined inside the body of another for loop. The loop which is inside the outer loop must finish the number of repetitions before the outer loop can continue to its next repetition. 

Syntax:  

                                                 nested-loop-syntax.

Fig: Nested For loop syntax

Example: 

nested-loop-ex

                                Fig: Nested For loop Example

As you can see, this demo has used two for loops, i.e., x and y. So, when x=0, every value of y is evaluated under the condition y < 3. If it evaluates to be true, then for that particular value of x, every value of y, i.e., 0, 1, and 2, is printed until the condition is false.

Similarly, the flow repeats for x equal to 1 and 2.

It shows the output of the above code below:

Output 

nested-loop-output

Fig: Nested for loop output

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Conclusion

In this article, you learned one of the fundamental concepts of programming, i.e., for loop. You looked at how a for loop works and the different expressions of for loop with some examples. Want to learn more about loops in C++? Check out our next tutorial on C++ While loop.

Are you looking to learn further and master today’s top programming languages used by companies world over? If yes, Simplilearn’s Post Graduate Program in Full Stack Web Development is the route for you. This world-class 9-month bootcamp is delivered in collaboration with Caltech CTME and covers 30+ in-demand tools and skills and makes you job-ready and equipped to grab top software development job roles.

Do you have any questions regarding this article on C++ For Loops? If you do, then please put them in the comment section. We’ll help you solve your queries. To learn more about C++ For Loops, click on the following link: For Loops in C++.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.