C++ conditions and range loops

A range-based for loop visits each element of a container. This version copies each int into value, which is inexpensive for a small scalar.

C++
#include <iostream>
#include <vector>
#include <cassert>
int main() {
    std::vector<int> values{-2, 0, 3, 5};
    int total = 0;
    for (int value : values) {
        if (value > 0) total += value;
    }
    std::cout << total << '\n';
    assert(total == 8);
}
Read the full lesson