1.3.4 Logical Operators

Logical operators are operators such as and, or, and not. You should be familiar with these concepts from previous computer science courses, and in C++ they work exactly the same.

And

The and operator is denoted with &&, the same as Java. Any conditions joined with an and operator must both be true for the expression to be true.

Or

The or operator is denoted with ||, again the same as Java. For an or expression to return true, either of the expressions need to be true, It is ok if both are also true.

Not

The not operator is again the same as Java, !. When applied to a boolean value, it returns the opposite value. For example, not true returns false.

Notice in this example how logical operators are used as the conditional statement for the if block. Since logical operators ultimately evaluate to a true/false value, they can be used as the condition for an if block.

Alternatively, you can set up your code with nested if statements.

Try This Example

Last updated