C++ For Loops Explained: Syntax, Workflow, and Practical Examples
What Is a For Loop?
A for loop is a control structure that repeats a block of code a predetermined number of times. It evaluates a condition before each iteration; if the condition is true, the loop body executes, otherwise the loop ends immediately.
In this tutorial you’ll discover:
- The core concept of a for loop
- How a for loop executes internally
- When to choose a for loop over other constructs
- The official syntax
- Two illustrative examples
How a For Loop Executes
The execution flow is best visualized in the diagram below.

Step‑by‑step:
- The compiler evaluates the initialization once when the loop starts.
- The condition is checked.
- If true, the loop body runs; then the increment expression updates the loop variable.
- Control jumps back to step 2. The loop continues until the condition becomes false.
- When false is encountered, the loop exits and execution continues after the loop body.
Note: The condition is re‑evaluated after every iteration, allowing the loop variable to change each cycle.
When to Use a For Loop?
Use a for loop when:
- You know the exact number of iterations beforehand.
- You need tight control over the loop counter and its update logic.
For Loop Syntax
for (initialization; condition; increment) {
// statements
}
Each part plays a specific role:
- Initialization: Executed once; declares and sets the loop counter.
- Condition: Evaluated before each iteration; if
falsethe loop terminates. - Increment: Runs after the loop body; updates the counter.
- The body runs only when the condition is
true.
Example 1: Basic Counting
#include <iostream>
using namespace std;
int main() {
for (int x = 0; x < 5; x = x + 1) {
cout << "X is: " << x << endl;
}
return 0;
}
Output:

Code walkthrough:
- Include
<iostream>to access console I/O. - Use the
stdnamespace for convenience. - Define
main()as the entry point. - Initialize
xto 0; loop whilex < 5; incrementxeach cycle. - Print the current value of
x. - Return 0 to indicate successful execution.
Example 2: Calculating Factorial
#include <iostream>
using namespace std;
int main() {
int x, num, factorial = 1;
cout << "Type positive number: ";
cin >> num;
for (x = 1; x <= num; ++x) {
factorial *= x;
}
cout << "Factorial of " << num << " = " << factorial;
return 0;
}
Output:

Key points in this program:
- Prompt the user for a positive integer.
- Initialize
factorialto 1. - Loop from 1 to
num, multiplyingfactorialby the loop counter each iteration. - Display the result.
Summary
- A for loop repeats a code block a set number of times.
- The loop continues while the condition remains
true. - Initialization declares and sets loop counters.
- The condition gate controls execution of the loop body.
- The increment can be omitted or replaced with a semicolon if no update is needed.
C Language
- Mastering C# For Loops: Syntax, Flow, and Practical Examples
- Master C++ For Loops: A Step-by-Step Guide
- C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
- Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
- Master C++ Dynamic Array Allocation: A Practical Guide with Code Examples
- Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
- C++ Structs Explained with a Practical Example
- C++ Classes & Objects: A Practical Guide with Code Examples
- C++ Polymorphism Explained: Practical Examples & Key Concepts
- Mastering std::list in C++: Syntax, Functions & Practical Examples