Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Logical Operators

The sample programs presented so far generally execute one statement after another. They usually do exactly the same thing each time they run. Although this is a good way to learn C++ programming, that's not how games operate. Games need to present players with different experiences each time they play. The most important tool for doing this is the C++ if-else statement, which we will discuss shortly. However, to use the if-else statement, programmers have to understand logical operators. Therefore, we'll take a brief look at them now.

Logical operators are pretty straightforward. In fact, you have seen them before in your math classes in school. Table 3.1 shows the C++ logical operators.

Table 3.1. The C++ Logical Operators

Operator

Description

<

Less than

<=

Less than or equal to

==

Equal to

!=

Not equal

!

Not

>=

Greater than or equal to

>

Greater than

&&

And

||

Or

Although you may have used different symbols for these in school, you're probably familiar with what they do. You use these operators in comparisons, which programmers call logical expressions. All logical expressions evaluate to true or false. Recall from chapter 2 that true and false are actual values in C++, just like numbers.

You've actually used logical operators in your programs already. chapter 2 showed their use with while and do-while loops. Table 3.2 gives some more examples of how to use logical operators.

Table 3.2. Using Logical Operators In Expressions

Expression

Meaning

thisValue <= thatValue

Evaluates to true if the variable thisValue contains a value that is less than or equal to the value in the variable thatValue. Otherwise, it evaluates to false.

thisValue != 100

Evaluates to true if thisValue is not equal to 100. Otherwise, it evaluates to false.

thisValue == 100

Evaluates to true if thisValue is equal to 100. Otherwise, it evaluates to false.

!(thisValue == 100)

Evaluates to true if thisValue is not equal to 100. Otherwise, it evaluates to false.

(thisValue < 100) && (thatValue != 10)

Evaluates to true if thisValue is less than 100 and that Value is not equal to 10. Otherwise, it evaluates to false.

(thisValue < 100) || (thatValue != 10)

Evaluates to true if thisValue is less than 100 or that Value is not equal to 10. Otherwise, it evaluates to false.

Some of the expressions in Table 3.2 deserve particular attention. The expression

!(thisValue == 100)

says the same thing as

thisValue != 100

To understand this expression, you need to know that in C++, everything in parentheses is done first. To evaluate

!(thisValue == 100)

you first evaluate

thisValue == 100

because it's in the parentheses. This expression evaluates to true when the variable

thisValue contains 100. If it contains any other value, the expression is false. If you then add the Not operator, which is the exclamation point, it negates the expression. That means if

thisValue == 100

evaluates to true, then

!(thisValue == 100)

evaluates to false. And any time

thisValue == 100

evaluates to false, it makes

!(thisValue == 100)

evaluate to true. The ! operator negates a logical condition.

The last two expressions in Table 3.2 are also of particular interest. These are often called compound logical expressions because they put together more than one comparison or condition. When you're writing your programs, you make compound logical expressions with the And operator. You can also create them with the Or operator. How is this done?

The expression

(thisValue<100) && (thatValue!=10)

has two conditions:

thisValue<100

and

thatValue!=10

In order for the entire expression to evaluate to true, both the first and the second conditions must evaluate to true. If either of them evaluates to false, then the whole expression is false. As a result, expressions with an And operator tend to evaluate to false more than they evaluate to true.

If we rewrite the expression to make this

(thisValue<100) || (thatValue!=10)

then we're using the Or operator. This says that the entire expression is true if either the first or the second condition evaluates to true. The only time it's false is if both conditions are false.

  • + Share This
  • 🔖 Save To Your Account