C++ Iterate Through Array

January 21, 20242 min read

In the C++ programming language, arrays are fundamental data structures that allow storing multiple elements of the same type in contiguous memory locations. They reserve contiguous memory space for each element, making them efficient in accessing and manipulating data. But the main question here is how you access each element individually. This is where repetition comes in. Iterating through an array involves sequentially accessing each element, performing operations on and potentially modifying their values.

What is recurrence?

Iteration, also known as looping, allows you to systematically look at each element in an array, perform operations on it, and potentially modify their values. Iterating through an array in C++ refers to repeating a function or statement until a condition is met. Think of it as if you're looking at a bookshelf: You have to look at each book individually to find the specific book you want. Similarly, to access and handle each element in an array, you have to iterate over it. This article explores various methods for iterating through C++ arrays, providing examples and insight into each approach.

Method 1: Using a traditional “for” loop

Among the various techniques of iterating through arrays in C++, the “for” loop stands out for its simplicity and clear control. It provides a structured approach to visit each element in the sequence and convenient access to their values.

In this example a “for” loop is used to iterate through the number array. The loop moves from array size zero to one, accessing each element using the “st” index. This code demonstrates a simple example of iterating through an array in C++ using a “for” loop. Each time an element in the array is iterated over, the loop releases the value of that element to the console. This fundamental concept in C++ programming forms the basis of many more complex operations involving arrays.


using namespace std;
int main,,,

int nmbr[] , ,5, 18, 2, 10, 80, 70,,

For ,int st = 0, scheduled tribe<size of,nmbr,,size of,nmbr[0],, st++,,
return 0,

Now, let's figure out what each line of code is doing. input and output operations are supported by headers. The program starts with the function “int main()” which is the entry point of the program. The array named “nmbr” of type “int” is then initialized with five elements: 5, 18, 2, 10, 80, and 70. Next, the “for” loop is defined. Here are the details of the loop components:

int st = 0;:This initializes the “st” integer variable to 0. This variable acts as a loop counter.

st[0],:This is a loop condition. The loop continues until the value of “st” is less than the size of the array divided by the size of each element.

st++:It increments the value of “st” after each iteration.

cout<[st] , The statement prints the element at the current “st” index to the console, followed by a space. Finally, Return 0 returns an exit code of 0 indicating successful program execution. The following is the output of the program:

Method 2: Using a range-based “for” loop (C++11 onwards)

C++11 introduced a more concise and readable way to iterate through containers, using a range-based “for” loop. C++11 introduced a game-changer for iterating through collections like arrays and vectors. This concise and readable syntax replaced the longer, less intuitive traditional “for” loops, making the code cleaner and easier to understand.

Think of it like this:

Traditional “for” loop: A complex, multi-line construct that requires explicit loop counter management and element indexing.

Range-based “for” loop: A single, concise line that focuses on the element itself, removing any non-essential details.

This range-based “for” loop approach offers several benefits:

    • Brevity: Single-line iteration eliminates the need for counters and index calculations, making code shorter and more readable.
    • Readability: It focuses on element access rather than iteration mechanics, leading to more accurate and more intuitive code.
    • Better maintainability: It eliminates error-prone elements like loop counters, making the code easier to maintain and modify.
    • Modern syntax: This aligns with modern C++ coding styles, promoting a cleaner and more consistent code structure.

Now, let's look at an example of a range-based “for” loop to fully understand how it works:

using namespace std;
int main,,,
string name array[] , ,“Kalsoom_1”, “Kalsoom_2”, “Kalsoom_3”, “Kalsoom_4”, “Kalsoom_5”,,
For ,constant string, NA: Name Array,,
return 0,

The following is the output of a range-based “for” loop program:

Notice how the range-based loop achieves the same functionality in a row, highlighting the element and removing unnecessary detail. The range-based method uses the range provided by the array and automatically binds each element to a variable number within the loop. This eliminates the need for explicit loop counter and index calculations, making code more readable and less prone to errors.

Each element in the array is visited and processed using both methods which gives the same result. However, the range-based “for” loop provides a more concise and modern approach, making it the preferred choice for most scenarios in C++11 and later versions.

The range-based “for” loop simplifies the syntax by iterating directly through the elements of the array without the need for explicit indexes. This increases the readability of the code and reduces the risk of recurring errors.

Method 3: Using standard library algorithms

The C++ standard library algorithm provides a powerful and flexible alternative to iterating through arrays: std::for_each. Unlike previous methods, which focus on element access, this approach emphasizes applying a specific function or operation to each array element. Let us demonstrate an example to help you understand how the “for_each” function works in C++ iterating through an array.

using namespace std;
int main,, ,
int numArray[] , ,5, 4, 7, 11, 20,,
auto class = [],integer elements, , return Elephant , Elephant; ,,
For each,Start,numArray,Ending,numArray,Social class,,
For ,int ns : numArray, ,
court , NS , ,,
court , Andal;
return 0,

Let's explain the program step by step. <एल्गोरिदम> The header provides access to the “std::for_each” function and other useful algorithms that are used later in the program. A “square = [](int elem) { return elem * elem;};” function is created that encapsulates the desired operation to be applied to each element. The “for_each” function takes the initial and final iterations of the array and passes your operation as arguments. It does. The “for_each” algorithm takes a range that is defined by iterators and applies the provided function on each element.

Here is the resulting output of the given program:


Iterating through arrays is a fundamental skill in C++, and there are several methods available to achieve this task. Whether traditional loops, modern range-based loops, or standard library algorithms are used, choosing the right method depends on the specific needs and preferences of the programmer. Understanding these different approaches allows developers to write efficient and readable code when working with arrays in C++.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.

What do you think?

Show comments / Leave a comment

Leave a reply