Core C++

Lesson 5 : Logical Operators

This C++ tutorial teaches about the use of logical operators. Logical operators are most commonly used to control the flow of the programs, as we will show in the upcoming lessons. The intention of this lesson and the video is help you learn the C++ syntax for logical operators. As far as the logic involved, it is assumed that you have some familiarity with how these operations work.

There a three base logical operators available in C++. From these, we can build up more complex operations. The three operators that are available are and, or, and not.

The table above gives the operators with the corresponding C++ syntax.




The simplest logical operator is the not operator. It takes a Boolean value and turns it into the opposite: true to false and false to true.

In this example, "bNotP" is initialized as true due to the not operator. The truth table for the not operator looks like this:




This not operator is a called unary operator since it operates on only one value, say P. The and and or operators are called binary operators because they operate on two values, say P and Q.

In this example, "bPAndQ" is initialized with the value false, since "bP" is false. The full truth table for the and operator looks like this:




The or operator is the other binary operator. In this example, "bPOrQ" is initialized with the value true since "bQ" is true.

The full truth table for or looks like this:

These operators are the building blocks for other operators. We can make expressions from these three operators for other operators that we might use, like an exclusive-or. We can write an exclusive-or operator like this:

 

© 2007–2024 XoaX.net LLC. All rights reserved.