Game Development Reference
In-Depth Information
Chapter 6
Making Decisions with Flow
Control
The programs that we have been writing until now have been very linear. That is, they begin with the
first line in main and carry on executing each line at a time until the program finishes with the return
statement on the last line of main . Sometimes we might decide to carry out different code paths
based on values we have stored in variables or repeat a section of code until some condition is
satisfied.
C++ provides us with statements that allow us to carry out these decisions and repetitions. This
chapter introduces the if , switch , for , and while statements. These statements collectively are
known as the flow control statements. We'll end the chapter by using flow control statements to add
some decision making to our Text Adventure game.
The if Statement
The first flow control statement that we will look at is the if statement. This statement allows us to
execute inside its curly braced block if the boolean we pass to it is true. Listing 6-1 shows a simple
if statement.
Listing 6-1. The if Statement
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
57
 
Search WWH ::




Custom Search