C++ for Loop
In this page:
for (initialization; condition; update) {
// body
}
Introduction to For Loops
A for loop packs initialization, a condition, and an update step into one line — for (int i = 0; i < 10; i++) — making it the natural choice whenever you already know exactly how many times you need to repeat something.
Compared to a while loop, keeping all three parts together makes the loop's start, stop, and progress conditions easy to see at a glance.
Example: Introduction to For Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 10; i++) {
// Print to the console with cout
std::cout << i << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Loop Steps
The update step isn't limited to simple +1 increments — you can add any amount, multiply, or divide the counter each pass, which changes the loop from stepping linearly to stepping geometrically.
This is exactly how algorithms that repeatedly halve or double a value, like binary search, structure their loop.
Example: Loop Steps
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 20; i += 5) {
// Print to the console with cout
std::cout << i << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested For Loops
Placing one for loop inside another lets the inner loop run to completion once for every single iteration of the outer loop, which is exactly the pattern behind processing a 2D grid — the outer loop walks rows while the inner loop walks columns within each row.
The total number of iterations is the product of both loops' counts.
Example: Nested For Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 2; i++) {
// Loop: repeat while the condition holds
for (int j = 1; j <= 2; j++) {
// Print to the console with cout
std::cout << i << "," << j << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Looping Over Arrays
Using a for loop's counter as an array index, like for (int i = 0; i < size; i++) cout << arr[i];, is one of the most common patterns in C++ for visiting every element in sequence.
It gives you the index itself if you need it, unlike some alternatives that only give you the value.
Example: Looping Over Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
// Declare size and set it to 3
int size = 3;
// Loop: repeat while the condition holds
for (int i = 0; i < size; i++) {
// Print to the console with cout
std::cout << arr[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Range-based For Loop
A range-based for loop, written for (int x : arr), iterates directly over each element's value without you managing an index at all, which removes an entire category of off-by-one bugs.
It's the preferred style in modern C++ whenever you don't actually need the index, only the values themselves.
Example: Range-based For Loop
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {10, 20, 30};
for (int x : arr) {
// Print to the console with cout
std::cout << x << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
<=with an array length, likei <= n, which reads one element past the end. - Adding a semicolon after
for (...);, which makes the loop body empty. - Using commas or colons instead of semicolons in
for (int i = 0, i < 5, i++).
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: