Loops

Along with conditional expressions another powerful language facility is loops. Loops allow for programs to run in an iterative manner, that is a block will be executed in series. This is powerful feature that enables us to repeat a set of instructions effectively and efficiently.

While Loop

A while loop is the most basic kind of loop. while loops will repeat its code block as long as its condition is met.

#include <iostream>

auto main () -> int
{
    auto a {10};

    while (a > 0)
    {
        std::cout << "a = " << a << std::endl;
        --a;
    }

    return 0;
}

Example

Do-While Loop

There is another kind of while loop in C++ called a do-while loop. This works the exact same way a regular while loop works except that the condition is checked at the end of each loop rather than the start. This means that the code block will be executed at least once.

#include <iostream>

auto main () -> int
{
    auto a {0};

    do
    {
        std::cout << "a = " << a << std::endl;
        --a;
    } while (a > 0);

    return 0;
}

Example

Note: You can break out of a while or do-while loop with break or a return-expression.

For Loop

Another common loop in C++ is the for loop. for loops will generate an initial value, validate it meets a condition and proceed through the sequences.

#include <iostream>

auto main () -> int
{
    for (auto i {0}; i < 10; ++i)
        std::cout << "i = " << i << std::endl;

    return 0;
}

Example

As we can see, loops through the power of conditional checking make programs much smaller and allow us to abstract repeated actions into a single statement.

Range For

There is one other loop in C++. This is the range-for. This is a special for loop that is able to iterate through a sequence of values, yielding a single value from the sequence each loop. It automatically knows the size of the sequence and when to stop.

#include <iostream>

auto main () -> int
{
    std::cout << "[ ";
    for (auto i : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
        std::cout << i << ", ";

    std::cout << "]" << std::endl;

    for (auto s : {"It's over Anakin!", "I have the high ground!"})
        std::cout << s << std::endl;

    return 0;
}

Example