Syntax of for Loop

The syntax of for loop in C++ is shown below:

for ( initialization; test condition; updation)
{ 
     // body of for loop
}

C++ for Loop Syntax Breakdown

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.

Similar Reads

Syntax of for Loop

The syntax of for loop in C++ is shown below:...

Flowchart of for Loop in C++

Flowchart of for Loop in C++...

Examples of for Loop in C++

Example 1...

C++ Nested for Loop

...

Multiple Variables in for Loop

...

C++ Infinite for Loop

...

Range-Based for Loop in C++

A nested for loop is basically putting one loop inside another loop. Every time the outer loop runs, the inner loop runs all the way through. It’s a way to repeat tasks within tasks in your program....

for_each Loop in C++

...