Game Development Reference
In-Depth Information
{
if (true)
{
cout << "Print This!";
}
return 0;
}
This if statement will always enter into the block associated with it. This block begins with the
opening curly brace immediately following the statement and ends with the corresponding closing
curly brace. If we were to change the value in the expression from true to false the block would not
be entered and we would not see the output printed on the console.
Note The examples in this chapter have been created using Xcode. Be sure to check that the main
statement is correct for the integrated development environment (IDE) and platform you are using.
The else and else if Statements
The if statement allows us to make decisions in our code but usually we also want to be able to
execute code on other conditions or as a default. We can use the else and else if statements to
add more execution blocks to our if statements. Listing 6-2 shows how to use the else statement.
Listing 6-2. The else Statement
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
if (false)
{
cout << "Print This When True!";
}
else
{
cout << "Print This When False!";
}
return 0;
}
Our if statement now evaluates to false. If this were to happen in Listing 6-1, we would see no
output from our program; however, the else statement in Listing 6-2 ensures that we will still see
output. Listing 6-3 adds an extra if into the code.
 
 
Search WWH ::




Custom Search