Operators: Logical Operators

Learn about the logical operators in C++ and how to use them.

We'll cover the following

In this lesson, we’ll learn about the C++ logical operators.

Logical operators

Logical operators combine conditional expressions. A logical operator is used to extend a logical condition. We can combine many conditions by logical operators to make a complex condition. There are three types of logical operators:

  • AND operation represented by the symbol &&
  • OR operation represented by the symbol ||
  • NOT operation represented by the symbol !

We can see the examples of the logical operators in the table below:

Logical Operator

Symbol

Meaning

Example of syntax


AND &&

(condition1 && condition2)

If both conditions are true, then it is true; otherwise false

  • (1 < 0 && 1 == 0) false
  • (1 > 0 && 1 == 0) false
  • (1 < 0 && 1 == 1) false
  • (1 > 0 && 1 == 1) true


OR ||

(condition1 || condition2)

If one condition is true, then it is true; otherwise false


  • (1 < 0 || 1 == 0) false
  • (1 > 0 || 1 == 0) true
  • (1 < 0 || 1 == 1) true
  • (1 > 0 || 1 == 1) true



NOT !

!(condition1 && condition2)

!(condition1 || condition2)

If a condition is true, then the NOT operator will make it false(reverse logical state)

  • !(1 < 0 && 1 == 0) true
  • !(1 > 0 && 1 == 0) true
  • !(1 > 0 || 1 == 0) false
  • !(1 < 0 || 1 == 0) true

In C++, a nonzero value in any condition is true, and zero is considered as false.

Short-circuiting

Sometimes when we use the AND (&&) and OR (||) operators in an expression, the entire expression need not be evaluated as soon as the first condition in && is false or the first || is true. This is called short-circuiting.

Following are the two situations with logical operators when the right hand side of the expression is not evaluated:

  1. When the first condition in && is already false, the rest of the conditions need not be evaluated.

    For example,

   (4 > 5) && (4 < 7)

The condition on the right-hand side (4 < 7) will not be evaluated in the expression above since with an && operator, both conditions need to be true. But the first condition (4 > 5) of the AND operator is already false so the second condition will not be evaluated.

  1. When the || operator is used and the first condition becomes true, the rest of the conditions need not be evaluated.

    For example,

   (4 == 4) || (4 < 7)

The right-hand side condition (4 < 7) will not be evaluated in the expression above because the first condition (4 == 4) of the OR operator is already true.

Quiz on short-circuiting

Question

What is wrong with the following code:

The code below makes use of an if statement, which is a conditional statement. If the condition within the statement is true, the code inside the curly braces, {}, is executed. If it is false, the code inside {} is not executed. We’ll learn more about if statements in further detail later on.

   int n, d;
   cout << "Enter a number and its divisor: ";
   cin>>n>>d;
   if(n%d==0 && d!=0) // the second condition was added to stop divide-by-zero error
   {
      cout << n << " is divisible by "<<d<<endl;
   }
Show Answer