← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 2 of 15

C++ Looping Through Arrays

A for loop with an index, or a range-based for loop, is the standard way to visit every element of an array in C++, whether printing, summing, searching, or modifying values.
Syntax
cpp
for (int i = 0; i < length; i++) {
  array_name[i];
}

for (type element : array_name) {
  // use element
}

Looping Through an Array with a for Loop

The standard way to loop through an array in C++ is a for loop whose index variable runs from 0 up to, but not including, the array's length, accessing one element per iteration with square-bracket indexing.

Example: Looping Through an Array with a for Loop

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4};
	// Declare length and set it to 4
	int length = 4;
	// Loop: repeat while the condition holds
	for (int i = 0; i < length; 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;
}

Looping with Range-Based for

A range-based for loop iterates directly over an array's elements without needing an explicit index variable, making the loop shorter and less error-prone.

Example: Looping with Range-Based for

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4};
	for (int value : arr) {
		// Print to the console with cout
		std::cout << value << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Summing Array Elements

A running total variable, initialized before the loop and updated inside it, is the standard pattern for summing every element in an array, which can then be divided by the count to compute an average.

Example: Summing Array Elements

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {10, 20, 30};
	// Declare total and set it to 0
	int total = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		total += arr[i];
	}
	// Print to the console with cout
	std::cout << total << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Finding a Value in an Array

Searching an array for a specific value loops through its elements checking each one against the target, and break is commonly used to stop the loop immediately once a match is found rather than continuing needlessly.

Example: Finding a Value in an Array

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {5, 8, 12, 3};
	// Declare target and set it to 12
	int target = 12;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 4; i++) {
		// Check whether arr[i] == target
		if (arr[i] == target) {
			// Print to the console with cout
			std::cout << "Found at index " << i << std::endl;
			// Leave the switch/loop
			break;
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Elements in a Loop

A loop can modify an array's elements in place by both reading and writing to the same index within its body, which is how operations like doubling every value or clamping negative values are applied across an entire array.

Example: Modifying Elements in a Loop

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		arr[i] *= 2;
	}
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; 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;
}
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. Looping with i <= size, which reads past the last element.
  2. Using sizeof(arr) on a pointer or function parameter to find the length.
  3. Modifying elements in a range-based for loop without using &, changing only a copy.

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.