Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Looping Through Arrays | Using Arrays in Programs
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Arrays

bookLooping Through Arrays

When working with arrays in C++, you often need to process every element—whether that's printing values, calculating a sum, or searching for a specific item. Iterating over arrays with loops is an essential technique that lets you access each element in sequence, without repeating code for each index. Loops provide a concise, flexible way to handle arrays of any size.

main.cpp

main.cpp

copy
12345678
#include <iostream> int main() { int numbers[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) std::cout << numbers[i] << std::endl; }

In the code above, the for loop uses an index variable i that starts at 0 and increases by 1 after each iteration. The loop continues as long as i < 5, which matches the number of elements in the array. This ensures every valid index—0 through 4—is accessed. The loop condition is crucial: if you accidentally set it to i <= 5, the loop would try to access numbers[5], which is outside the array and causes undefined behavior. The index variable allows you to access each element using numbers[i] without repeating code for each position.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { int values[5] = {2, 4, 6, 8, 10}; int sum = 0; for (int i = 0; i < 5; i++) sum += values[i]; std::cout << "Sum: " << sum << std::endl; }

A common mistake when looping through arrays is to use the wrong loop boundaries. If you use i <= 5 for a 5-element array, the loop will run one time too many and attempt to access an element beyond the end of the array. This leads to unpredictable results and can cause your program to crash or behave incorrectly. Always ensure your loop condition matches the array's valid index range, which is from 0 up to, but not including, the array size.

Note
Note

Always use < (not <=) in the loop condition when iterating over arrays. This prevents out-of-bounds access and keeps your code safe and correct.

question mark

What is the correct loop condition to iterate over all elements of a 6-element array?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookLooping Through Arrays

Pyyhkäise näyttääksesi valikon

When working with arrays in C++, you often need to process every element—whether that's printing values, calculating a sum, or searching for a specific item. Iterating over arrays with loops is an essential technique that lets you access each element in sequence, without repeating code for each index. Loops provide a concise, flexible way to handle arrays of any size.

main.cpp

main.cpp

copy
12345678
#include <iostream> int main() { int numbers[5] = {10, 20, 30, 40, 50}; for (int i = 0; i < 5; i++) std::cout << numbers[i] << std::endl; }

In the code above, the for loop uses an index variable i that starts at 0 and increases by 1 after each iteration. The loop continues as long as i < 5, which matches the number of elements in the array. This ensures every valid index—0 through 4—is accessed. The loop condition is crucial: if you accidentally set it to i <= 5, the loop would try to access numbers[5], which is outside the array and causes undefined behavior. The index variable allows you to access each element using numbers[i] without repeating code for each position.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { int values[5] = {2, 4, 6, 8, 10}; int sum = 0; for (int i = 0; i < 5; i++) sum += values[i]; std::cout << "Sum: " << sum << std::endl; }

A common mistake when looping through arrays is to use the wrong loop boundaries. If you use i <= 5 for a 5-element array, the loop will run one time too many and attempt to access an element beyond the end of the array. This leads to unpredictable results and can cause your program to crash or behave incorrectly. Always ensure your loop condition matches the array's valid index range, which is from 0 up to, but not including, the array size.

Note
Note

Always use < (not <=) in the loop condition when iterating over arrays. This prevents out-of-bounds access and keeps your code safe and correct.

question mark

What is the correct loop condition to iterate over all elements of a 6-element array?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1
some-alt