< Previous | Contents | Next >

Understanding Truth

Truth is black and white, at least as far as C++ is concerned. You can represent true and false with their corresponding keywords, true and false. You can store such a Boolean value with a bool variable, as you saw in Chapter 1. Heres a quick refresher:

bool fact = true, fiction = false;

This code creates two bool variables, fact and fiction. fact is true and fiction

is false. Although the keywords true and false are handy, any expression or


39

40 Chapter 2 n Truth, Branching, and the Game Loop: Guess My Number


value can be interpreted as true or false, too. Any non-zero value can be interpreted as true, while 0 can be interpreted as false.

A common kind of expression interpreted as true or false involves comparing things. Comparisons are often made by using built-in relational operators. Table 2.1 lists the operators and a few sample expressions.


Table 2.1 Relational Operators

Operator

Meaning

Sample Expression

Evaluates To

==

equal to

5 == 5

true



5 == 8

false

!=

not equal to

5 != 8

true



5 != 5

false

>

greater than

8 > 5

true



5 > 8

false

<

less than

5 < 8

true



8 < 5

false

>=

greater than or equal to

8 >= 5

true



5 >= 8

false

<=

less than or equal to

5 <= 8

true



8 <= 5

false