Loops
Welcome to the world of loops in C++! Loops are general programming constructs not unique to C++ that allow you to repeat a certain block of code multiple times. They are crucial for making our games more efficient and flexible. It is probably the vital thing that makes computers so useful: doing the same thing but with different values repeatedly. In C++, there are several types of loops, each serving specific purposes. In this chapter, we’ll explore the fundamental loop structures and cover relatively recent updates to C++ that affect your loop-related programming options. The obvious example that we have seen so far is the game loop. With all the code stripped out, our game loop looks like this.
while (window.isOpen())
{
}
The correct term for this type of loop is a while loop. Let’s look at that first.
while loops
The while
loop is quite straightforward. Think back to the if
statements and their expressions that evaluated to either true...