Syntax of for Loop
The syntax of for loop in C++ is shown below:
for ( initialization; test condition; updation) { // body of for loop }
The various parts of the for loop are:
1. Initialization Expression in for Loop
We have to initialize the loop variable to some value in this expression.
2. Test Condition in for Loop
In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to the update expression. Otherwise, we will exit from the for loop.
3. Update Expression in for Loop
After executing the loop body, this expression increments/decrements the loop variable by some value.
for Loop in C++
In C++, for loop is an entry-controlled loop that is used to execute a block of code repeatedly for the specified range of values. Basically, for loop allows you to repeat a set of instructions for a specific number of iterations.
for loop is generally preferred over while and do-while loops in case the number of iterations is known beforehand.
Contact Us