Difficulty: Beginner
Programming Language: C++

Article Index

Logical Operators

These are a series of operators that perform an operation such as a comparison, and return a boolean result. For example if you compare to numbers using the less than operator, the result will be the boolean value that indicates if the expression on the left of the operator is less than the expression on the right of the operator.


Logical Not !
!expression1;

Returns the inverse of the boolean expression on the right of the operator.

// Configures `cout` to print "true" or "false" for boolean values
std::cout << std::boolalpha;

bool var1 = true;       // Create variable `var1` and initialise to `true`
std::cout << ( !var1 ) << "\n";     // Prints "false" to the terminal

Logical And &&
expression1 && expression2;

Returns true only if both expressions on the left and the right of the operator, evaluate as logical true.

std::cout << std::boolalpha;
std::cout << ( false && false ) << "\n";    // Prints "false" to the terminal
std::cout << ( false &&  true ) << "\n";    // Prints "false" to the terminal
std::cout << (  true && false ) << "\n";    // Prints "false" to the terminal
std::cout << (  true &&  true ) << "\n";    // Prints "true" to the terminal

Logical Or ||
expression1 || expression2;

Return true if either one or both of the expressions on the left or the right side of the operator evaluate as logical true.

std::cout << std::boolalpha;
std::cout << ( false || false ) << "\n";    // Prints "false" to the terminal
std::cout << ( false ||  true ) << "\n";    // Prints "true" to the terminal
std::cout << (  true || false ) << "\n";    // Prints "true" to the terminal
std::cout << (  true ||  true ) << "\n";    // Prints "true" to the terminal

Equality ==
expression1 == expression2;

Returns true if two variables are equal in value.

std::cout << std::boolalpha;
std::cout << (  true == false ) << "\n";    // Prints "false" to the terminal
std::cout << ( false == false ) << "\n";    // Prints "true" to the terminal
std::cout << (   5.0 ==  2.34 ) << "\n";    // Prints "false" to the terminal
std::cout << (   3.2 ==  3.2  ) << "\n";    // Prints "true" to the terminal

Inequality !=
expression1 != expression2;

Returns true if two variable are not equal in value.

std::cout << std::boolalpha;
std::cout << (  true != false ) << "\n";    // Prints "true" to the terminal
std::cout << ( false != false ) << "\n";    // Prints "false" to the terminal
std::cout << (   5.0 !=  2.34 ) << "\n";    // Prints "true" to the terminal
std::cout << (   3.2 !=  3.2  ) << "\n";    // Prints "false" to the terminal

Less Than <
expression1 < expression2;

Returns true if the expression on the left of the operator is smaller in value than the expression on the right of the operator.

std::cout << ( 5.0 < 2.34 ) << "\n";    // Prints "false" to the terminal
std::cout << ( 3.2 < 3.2  ) << "\n";    // Prints "false" to the terminal
std::cout << ( 2.9 < 8.35 ) << "\n";    // Prints "true" to the terminal

Less Than Or Equal To <=
expression1 <= expression2;

Returns true if the expression on the left of the operator is smaller in value or equal in value to the expression on the right side of the operator.

std::cout << ( 5.0 <= 2.34 ) << "\n";    // Prints "false" to the terminal
std::cout << ( 3.2 <= 3.2  ) << "\n";    // Prints "true" to the terminal
std::cout << ( 2.9 <= 8.35 ) << "\n";    // Prints "true" to the terminal

Greater Than >
expression1 > expression2;

Returns true if the expression on the left of the operator is greater in value than the expression on the right of the operator.

std::cout << ( 5.0 > 2.34 ) << "\n";    // Prints "true" to the terminal
std::cout << ( 3.2 > 3.2  ) << "\n";    // Prints "false" to the terminal
std::cout << ( 2.9 > 8.35 ) << "\n";    // Prints "false" to the terminal

Greater Than Or Equal To >=
expression1 >= expression2;

Returns true if the expression on the left of the operator is greater in value or equal in value to the expression on the right side of the operator.

std::cout << ( 5.0 >= 2.34 ) << "\n";    // Prints "true" to the terminal
std::cout << ( 3.2 >= 3.2  ) << "\n";    // Prints "false" to the terminal
std::cout << ( 2.9 >= 8.35 ) << "\n";    // Prints "false" to the terminal​