Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Looping Through Arrays | Using Arrays in Programs
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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how to determine the correct loop boundaries for different array sizes?

What happens if I accidentally access an array element outside its valid range?

Can you show an example of iterating over an array safely?

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
some-alt