Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++: The Range-Based for Loop (iterate through an array) #200

Open Qingquan-Li opened 2 years ago

Qingquan-Li commented 2 years ago

Concept: The range-based for loop is a loop that iterates once for each element in an array. Each time the loop iterates, it copies an element from the array to a variable.

The range-based for loop was introduced in C++ 11.

Format:

for (dataType rangeVariable : array)
    statement;

Example (regular for loop):

#include <iostream>
using namespace std;

int main() {
    int numbers[] = { 10, 20, 30, 40, 50};
    int array_length = sizeof(numbers) / sizeof(numbers[0]);
    for (int i = 0; i < array_length; i++)
            cout << numbers[i] << " ";
    return 0;
}

Output:

10 20 30 40 50 

Example (range-based for loop):

Because the range-based for loop automatically knows the number of elements in an array, you do not have to use a counter variable to control its iterations, as with a regular for loop.

Additionally, you do not have to worry about stepping outside the bounds of an array when you use the range-based for loop.

#include <iostream>
using namespace std;

int main() {
    int numbers[] = { 10, 20, 30, 40, 50 };

    // It creates a copy of the element for each iteration.
    for (int val : numbers)
        cout << val << " ";

    cout << endl;

    // You can also use the `auto` key word with a reference range variable.
    // It gets a reference instead of a copy.
    for (auto& val : numbers)
        cout << val << " ";
    return 0;
}

Output:

10 20 30 40 50
10 20 30 40 50 


Example (range-based for loop with reference variable):

Without Reference Variable

Range-Based for Loop without Reference Variable

(Range-Based for Loop without Reference Variable)

#include <iostream>
using namespace std;

int main() {
    int numbers[5];

    // Work with copies.
    for (int val : numbers) {
        cout << "Enter an integer value: ";
        cin >> val;
    }

    // Display the values in the array.
    cout << "Here are the values you entered:\n";
    for (int val : numbers) {
        cout << val << " ";
    }

    return 0;
}

Output:

Enter an integer value: 1 2 3 4 5
Enter an integer value: Enter an integer value: Enter an integer value: Enter an integer value: Here are the values you entered:
1 16346240 -312279039 14233764 1 

// second time, output:
1 83668096 100761601 82342052 1 

With Reference Variable

Range-Based for Loop with Reference Variable

(Range-Based for Loop with Reference Variable)

#include <iostream>
using namespace std;

int main() {
    int numbers[5];

    // Work with original items and may modify them.
    for (int& val : numbers) {
        cout << "Enter an integer value: ";
        cin >> val;
    }

    // Display the values in the array.
    cout << "Here are the values you entered:\n";
    for (int val : numbers) {
        cout << val << " ";
    }

    return 0;
}

Output:

Enter an integer value: 1 2 3 4 5
Enter an integer value: Enter an integer value: Enter an integer value: Enter an integer value: Here are the values you entered:
1 2 3 4 5 

Use range-based for loop to output the values of an array, without reference variable V.S. with reference variable:

range-based for loop outputs without reference variable range-based for loop outputs with reference variable