C++ · ОСНОВЫ · LESSON 18

Each element

range for · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

How it works.

The colon loop takes the elements of vector in turn.

for (int number : nums) {
    cout << number << endl;
}

✦ number is the current list number.

STEP 2

Try to understand.

number is the current list number.

vector<int> nums = {2, 4};
for (int number : nums) {
    cout << number << endl;
}

✦ Compare this to the first one. Then do the cool job yourself.

IN-CLASS PRACTICE

Type the code yourself

Manually type the program from the sample. So you'll remember commands and brackets.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {2, 4};
    for (int number : nums) {
        cout << number << endl;
    }
    return 0;
}
Open the editor for class work
HOMEWORK

Solve it yourself

Создай vector<int> coins из 1, 3, 5. Через range for с переменной coin выведи все числа.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework