The for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. It is the most commonly used loop in C++ because it bundles the three essential parts of loop control—initialization, condition, and update—into a single, readable line.
Theoretically, the for loop is designed for Deterministic Iteration. This means it is best used when you know, or can calculate, exactly how many times the loop should run before it even starts.
Initialization: This happens exactly once when the loop starts. It is typically used to declare and set a starting value for a "loop counter" variable.
Condition: This boolean expression is checked before every iteration. If it evaluates to true, the loop body executes. If false, the loop terminates.
Update (Increment/Decrement): This happens at the end of every iteration, immediately after the loop body has finished. It modifies the loop counter to move toward the termination condition.
for (initialization; condition; update) { // Body of the loop }
The initialization expression is executed.
The condition is evaluated.
If the condition is true:
The loop body code is executed.
The update expression is executed after the body.
The process jumps back to step 2.
If the condition is false, the loop ends, and the program continues with the next statement after the loop.
In modern C++, if you declare a variable inside the initialization part (e.g., for(int i = 0; ...)), that variable has Block Scope. It exists only within the loop. Once the loop ends, the memory for i is released. This is a crucial safety feature that prevents "namespace pollution."
C++ allows you to initialize or update multiple variables in a single for loop by separating them with commas.
Example: for(int i = 0, j = 10; i < j; i++, j--)
All three parts of the for loop are optional.
for(;;) is a valid C++ statement that creates an Infinite Loop. Theoretically, it is the same as while(true), but some compilers optimize it slightly differently.
This program demonstrates a standard for loop to calculate the factorial of a number and a nested for loop to create a simple pattern.
#include <iostream> using namespace std; int main() { // Example 1: Calculating Factorial int n; long long factorial = 1; cout << "Enter a positive integer: "; cin >> n; for (int i = 1; i <= n; i++) { factorial *= i; // Multiplies factorial by i in each step } cout << "Factorial of " << n << " is: " << factorial << endl; cout << "\n--- Pattern Generation (Nested For) ---\n"; // Example 2: Nested Loops for (int row = 1; row <= 5; row++) { for (int col = 1; col <= row; col++) { cout << "* "; } cout << endl; // Move to next line after each row } return 0; }
A Nested Loop is a loop inside another loop.
Theory of Complexity: For every single iteration of the "outer" loop, the "inner" loop runs to completion. If the outer loop runs $N$ times and the inner loop runs $M$ times, the code inside the inner loop executes $N \times M$ times.
Usage: Nested loops are the primary way to process multi-dimensional data, such as 2D Arrays (matrices) or grids in game development.
| Component | When it runs? | Purpose |
| Initialization | Once at the very start. | Define the starting point/counter. |
| Condition | Before every iteration. | Decides whether to continue or stop. |
| Loop Body | If condition is true. | The actual work the loop performs. |
| Update | After the body finishes. | Changes the counter for the next round. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION