< Previous | Contents | Next >
The Score Rater program comments on a player’s score using an if statement. Figure 2.1 shows the program in action.
Figure 2.1
Messages are displayed (or not displayed) based on different if statements.
You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 2 folder; the filename is score_rater.cpp.
// Score Rater
// Demonstrates the if statement
#include <iostream> using namespace std;
int main()
{
if (true)
{
cout << "This is always displayed.\n\n";
}
42 Chapter 2 n Truth, Branching, and the Game Loop: Guess My Number
if (false)
{
cout << "This is never displayed.\n\n";
}
int score = 1000;
if (score)
{
cout << "At least you didn’t score zero.\n\n";
if (score >= 250)
{
cout << "You scored 250 or more. Decent.\n\n";
}
if (score >= 500)
{
cout << "You scored 500 or more. Nice.\n\n";
if (score >= 1000)
{
cout << "You scored 1000 or more. Impressive!\n";
}
}
return 0;
}