Logical Operators in C++

Logical Operators in C++

Logical Operators in C++ are the symbols used to perform logical operations and make decisions. Whether you have to check multiple conditions or control loops based on Boolean logic, logical operators help you in all these tasks. In this article, we will discuss what logical operators are in C++, types of logical operators, precedence and associativity, short-circuit evaluation, and comparison with the bitwise operators in C++.

Table of Contents:

What are Logical Operators in C++?

Logical operators in C++ are used to perform logical operations on two or more boolean expressions and give the result in either true or false. These operators are commonly used in conditional statements such as if, while, etc., to control the flow of a program.

Types of Logical Operators in C++

There are three types of logical operators:

Operator NameSymbolDescription
Logical AND&&Returns true if both conditions are true.
Logical OR||Returns true if at least one condition is true.
Logical NOT!Reverses the logical value of the condition.

Let’s discuss each of the types of logical operators in brief, with the examples in C++:

1. Logical AND (&&) in C++

The logical AND operator in C++ is used to check whether both conditions are true or not and returns true if both conditions are true, otherwise, it returns false.

Syntax:

condition1 && condition2

Truth Table for Logical AND (&&):

condition1condition2(condition1 && condition2)
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Example:

Cpp

Output:

Logical AND

The code shows how the logical AND operator compares the conditions if a person is 18 or older and has a score above 80, and then prints “Eligible for the program” if both conditions are true, otherwise, it prints “Not eligible”.

2. Logical OR (||) in C++

The logical OR operator in C++ checks if at least one of the conditions is true and then returns true if it is so. It returns false if both conditions are false.

Syntax:

condition1 || condition2

Truth Table for Logical OR (||):

condition1condition2(condition1 && condition2)
true true true 
true false true 
false true true 
false falsefalse 

Example:

Cpp

Output:

Logical OR

The code shows how the logical OR operator is used to check if a student is allowed to take the exam, if the student scored 50 or more marks, or if the student has at least 75% attendance.

3. Logical NOT (!) in C++

The logical NOT operator in C++ is an operator that reverses the truth value of a condition, which means that if the condition is true, this operator will make it false, and vice versa.

Syntax:

!condition

Truth Table for Logical NOT (!)

Condition(!Condition)
true false 
false true 

Example:

Cpp

Output:

Logical NOT

The code shows how the logical NOT operator is used to check if it is not raining, and it suggests “Go for a walk” since it’s not raining, otherwise, it will suggest “Stay inside”.

Precedence and Associativity of Logical Operators in C++

The precedence defines the order in which different operators are evaluated in an expression, and the associativity defines the order in which the operator is evaluated when multiple operators have the same precedence. 

The precedence level and associativity of the logical operators are:

OperatorPrecedence Associativity
!Highest Right to Left
&&Middle Left to Right
||LowestLogical OR

Short-Circuit Evaluation of Logical Operators in C++

A short-circuit evaluation of logical operators means checking the behavior of the logical operators. In other words, C++ stops evaluating as soon as the result is found. This evaluation is applied to logical AND(&&) and logical OR(||) operators only.

1. Short-Circuit Evaluation of Logical AND (&&) in C++

Short-circuit evaluation: If the first operand is false, then the entire expression will be false, so there is no need to check the second operand.

Example:

Cpp

Output:

Short-Circuit Evaluation of Logical AND

The code shows that the behavior of the logical AND operator, as x != 0 is false, so there is no need to evaluate the second condition because the result will always be false.

2. Short-Circuit Evaluation of Logical OR (||) in C++

Short-circuit evaluation: If the operand is true, then the entire expression will be true, so there is no need to check the second operand.

Example:

Cpp

Output:

Short-Circuit Evaluation of Logical OR

The code shows that the behavior of the logical OR operator as x == 0 is true, so there is no need to evaluate the second condition because the result will always be true.

Bitwise Operators vs Logical Operators

FeatureBitwise OperatorsLogical Operators
Used ForManipulating bits of dataEvaluating Boolean logic
Operands TypeWorks on integers (int, char, etc.)Works on boolean expressions
Operators&, |, ^, ~, <<, >>&&, ||, !
ReturnsInteger resultBoolean result (true or false)
Short-CircuitingNo short-circuitingSupport short-circuiting
Evaluation BasisBit-levelLogical condition

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

Logical operators in C++ play an important role in decision-making by evaluating expressions that return Boolean values. These operators help control program flow in statements such as if, while, and for loops. So, by understanding the behavior, precedence, associativity, and differences with bitwise, you can efficiently write C++ programs with the logical operators.

FAQs – Logical Operators in C++

Q1. What is the use of logical operators in C++?

The logical operators are used to evaluate conditions and return true or false.

Q2. What is short-circuiting in logical operators?

Short-circuiting is a technique that skips evaluating the second condition if the result is already determined by the first.

Q3. Difference between && and &?

The difference between && and & is that && is logical AND (boolean), and & is bitwise AND (binary).

Q4. Can logical operators work with non-boolean values?

Yes, non-zero is treated as true and zero as false.

Q5. Which logical operator has the highest precedence?

The Logical NOT operator (!) has the highest precedence.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner