0% found this document useful (0 votes)
17 views3 pages

Array Iteration with For Loops

The document provides a guide on array iteration using for loops, emphasizing that the loop should start from 0 and continue while the index is less than the array size. It includes a code example in C++ demonstrating how to print values from an array and read user input to populate the array. The guide highlights the simplicity and effectiveness of using for loops for iterating through arrays.

Uploaded by

bongbeyeu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Array Iteration with For Loops

The document provides a guide on array iteration using for loops, emphasizing that the loop should start from 0 and continue while the index is less than the array size. It includes a code example in C++ demonstrating how to print values from an array and read user input to populate the array. The guide highlights the simplicity and effectiveness of using for loops for iterating through arrays.

Uploaded by

bongbeyeu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

11/09/2025, 21:28 Array iteration | Field Guide

Field Guide

Array iteration

For loops work nicely with arrays, allowing you to easily craft a loop that iterates through valid
indexes. The simplest version loops from 0, while the index variable is less than the size of the
array.
The following image illustrates how the for loop allows you to iterate across the indexes of the
array.

Note
The for loop works nicely with the array.
Basic iteration involves looping from 0, while the control variable is less than the size
of the array.
You can also loop over parts of the array using the same pattern.

[Link] 1/3
Example
11/09/2025, 21:28 Array iteration | Field Guide

#include "splashkit.h"
#include "utilities.h"

const int SIZE = 10;

void print_array(int arr[], int size)


{
for(int i = 0; i < size; i++)
{
int value = arr[i];
write_line("Value " + to_string(i + 1) + " is " + to_string(value));
}
}

int main()
{
int my_array[SIZE];
int other[3] = {-5, 7, 10};

my_array[0] = 7;
my_array[1] = 10;

for(int i = 0; i < SIZE; i++)


{
my_array[i] = read_integer("Enter value " + to_string(i+1) + ": ");
}

for(int i = 0; i < SIZE; i++)


{
int value = my_array[i];
write_line("The value of my_array[" + to_string(i) + "] = " +
to_string(value));
}

print_array(my_array, SIZE);
}

[Link] 2/3
11/09/2025, 21:28 Array iteration | Field Guide

[Link] 3/3

You might also like