For Loop

Learn about for loop and its implementation in C++.

A for loop in C++ is another control flow statement that allows code to be executed repeatedly as long as the given condition is being met. It's commonly used when the programmer is aware of the number of iterations before entering the loop. It includes initialization, condition, and increment statements within a single declaration, making it more compact and easier to understand. Unlike the while loop, a single declaration in the for loop also minimizes the chances of error.

The syntax of a for loop is as follows. Notice how semicolons have been used.

Press + to interact
// Syntax
for (expression for initialization ; expression for testing ; expression for updating) {
//body
}

for loop components

The for loop consists of the following three parts:

...