C++ Logical Operators

www.igi‮c.aeditf‬om

Logical operators in C++ are used to combine two or more relational expressions and evaluate them as a single expression. The following are the logical operators in C++:

  1. Logical AND (&&): The logical AND operator is used to check if two or more expressions are true. The expression evaluates to true if and only if all the expressions are true.

Example:

int a = 10, b = 5;
bool c = (a > 5) && (b < 10); // c is assigned the value true
  1. Logical OR (||): The logical OR operator is used to check if at least one of two or more expressions is true. The expression evaluates to true if at least one expression is true.

Example:

int a = 10, b = 5;
bool c = (a > 5) || (b > 10); // c is assigned the value true
  1. Logical NOT (!): The logical NOT operator is used to reverse the truth value of an expression. If the expression is true, it evaluates to false, and if the expression is false, it evaluates to true.

Example:

int a = 10, b = 5;
bool c = !(a > b); // c is assigned the value false

In summary, logical operators in C++ are used to combine two or more relational expressions and evaluate them as a single expression. The logical AND (&&) operator is used to check if all expressions are true, the logical OR (||) operator is used to check if at least one expression is true, and the logical NOT (!) operator is used to reverse the truth value of an expression.