Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Suppose values contains five integers in this order: 4, 2, 7, 2, and 9. A C++ ranged for loop takes each element from the array in order and places it in the loop variable value. The loop body can then work directly with the current number, without asking for its index.
int values[5] = {4, 2, 7, 2, 9};
for (int value : values) {
cout << value << " ";
}The loop variable receives 4 during the first iteration, 2 during the second, 7 during the third, 2 during the fourth, and 9 during the fifth. The repeated 2 is visited twice because the array has two elements with that value. An index-based loop would make you write values[i], while the ranged form gives the element itself to the loop body.
Given values = {4, 2, 7, 2, 9}, what sequence does this loop print? for (int value : values) { cout << value << " "; }
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
In for (int value : values), int value is an ordinary variable. At each iteration, C++ copies the current array element into that variable. The loop body can change the copy, but changing the copy does not change the array slot that supplied it.
int values[5] = {4, 2, 7, 2, 9};
for (int value : values) {
value = 0;
}
// values is still {4, 2, 7, 2, 9}For the first iteration, value is a copy of 4, and value = 0 changes that copy. The next iterations create new copies of 2, 7, 2, and 9, and each copy is also changed to 0. When the loop ends, every temporary copy has disappeared, while values is still [4, 2, 7, 2, 9].
Place an ampersand after the type to declare a reference: for (int &value : values). Now value is not a new integer containing the element's value. It is another name for the current array element, so an assignment through value reaches the corresponding slot in values.
int values[5] = {4, 2, 7, 2, 9};
for (int &value : values) {
value *= 2;
}
// values is now {8, 4, 14, 4, 18}The first reference points to the slot containing 4, so value *= 2 changes that slot to 8. The next references reach the slots containing 2, 7, 2, and 9, changing them to 4, 14, 4, and 18. The final array is [8, 4, 14, 4, 18]. The ampersand changes the connection between value and the array, not the order in which elements are visited.
Fix this loop so the C++ array becomes [8, 4, 14, 4, 18]. Type the corrected loop header.
for (int value : values) { value *= 2; }Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A ranged for loop tells you the current number, but it does not give you the number's position. When value is 7, for example, the loop body knows the value but has no direct way to tell whether it came from index 0, 1, 2, 3, or 4. Equal values make this distinction especially clear: both visits to 2 have the same value, but they are separate array elements at different positions.
int values[5] = {4, 2, 7, 2, 9};
for (int i = 0; i < 5; i++) {
cout << "index " << i << ": " << values[i] << "\n";
}Use the ranged form when the task needs each value, such as printing values or changing every element through a reference. Use the familiar index-based form when the task needs an element's position, such as printing index 0 through index 4 beside the corresponding values. The choice depends on whether the body needs only the element or both the element and its index.