C++ Logical Operators

C++ Logical Operators are used to test the conditions in a program and make decisions based on the result of the conditions. They allow you to evaluate multiple conditions and take different actions based on the results. In C++, there are three logical operators: &&, ||, and !.

OperatorNameDescriptionExample
&&Logical ANDReturns true if both operands are true, otherwise returns false(5 > 4) && (3 < 4) returns true
||Logical ORReturns true if either of the operands is true, otherwise returns false(5 > 4)
!Logical NOTReverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false!(5 > 4) returns false

Logical AND (&&)

The logical AND operator returns true if both operands are true, otherwise it returns false. In other words, both conditions need to be true for the expression to evaluate to true. The operator is represented by two ampersands &&.

1int x = 5, y = 6; 2if (x > 4 && y > 5) { 3 cout << "Both conditions are true." << endl; 4}

In the above example, the condition x > 4 && y > 5 returns true because both conditions are true.

Logical OR (||)

The logical OR operator returns true if either of the operands is true, otherwise it returns false. In other words, at least one of the conditions needs to be true for the expression to evaluate to true. The operator is represented by two vertical lines ||.

1int x = 5, y = 6; 2if (x > 4 || y < 5) { 3 cout << "At least one of the conditions is true." << endl; 4}

In the above example, the condition x > 4 || y < 5 returns true because at least one of the conditions is true.

Logical NOT (!)

The logical NOT operator reverses the logical state of its operand. If a condition is true, then the logical NOT operator will make it false. If a condition is false, then the logical NOT operator will make it true. The operator is represented by an exclamation point !.

1int x = 5, y = 6; 2if (!(y < 5)) { 3 cout << "The condition is false." << endl; 4}

In the above example, the condition y < 5 is false, so the expression !(y < 5) returns true.

1#include <iostream> 2using namespace std; 3 4int main() { 5 int x = 5, y = 6; 6 bool z = (x > 4) && (y > 5); // z will be true because both conditions are true 7 cout << "The value of z is: " << z << endl; 8 9 z = (x > 4) || (y < 5); // z will be true because at least one of the conditions is true 10 cout << "The value of z is: " << z << endl; 11 12 z = !(y < 5); // z will be true because the condition is false 13 cout << "The value of z is: " << z << endl; 14 return 0; 15}

In the above example, the logical AND (&&) operator is used to check if both conditions (x > 4) and (y > 5) are true. If both conditions are true, the expression returns true. Similarly, the logical OR (||) operator is used to check if either of the conditions (x > 4) or (y < 5) is true. If either condition is true, the expression returns true. The logical NOT (!) operator is used to reverse the logical state of the operand. In this case, the condition (y < 5) is false, so the expression !(y < 5) returns true.