← Back to C++ Course | Chapter 4: Control Flow | Lesson 11 of 13

C++ for Loop

A for loop repeats steps a set number of times, like doing ten jumping jacks while counting. The start, stop, and counting all sit in one line.
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using <= with an array length, like i <= n, which reads one element past the end.
  2. Adding a semicolon after for (...);, which makes the loop body empty.
  3. Using commas or colons instead of semicolons in for (int i = 0, i < 5, i++).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.