C++ Arrays and Loops

In C++, arrays are often used with loops to efficiently process multiple elements without manually handling individual values.

Tutorials dojo strip

Looping Through an Array

Using a for loop, you can iterate through each element in an array.

Example: Displaying Motorcycle Brands

C++

Output:

C++

Displaying Index Alongside Values

To show the index along with each value, use i within the loop.

Example

C++

Output:

C++

Looping Through an Array of Numbers

Arrays can also store numerical values, such as engine capacities.

Example: Displaying Engine Sizes

C++

Output:

C++

The For-Each Loop (Range-Based Loop)

Introduced in C++11, the for-each loop (or range-based for loop) simplifies iteration by automatically handling each array element.

Syntax

C++

Example: Iterating Over Engine Sizes

C++

Example: Iterating Over Motorcycle Brands

C++
Tutorials dojo strip