C++ Program to Implement Logical operators in C++.

Here, the program discusses the logical operators in C++. These relational operators are used in decision-making.

What are Logical operators?

A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.
True: false
False: true

Logical operators in C++

As we’ll see, logical operators are well suited for checking the validity of two (or more) comparative operations. The operators then output a specific response based on the nature of the operator and whether one or both operands are true. In C++, we often see this in the form of an if/else statement.
If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Operator    Example    Meaning
&&    expression1 && expression2    Logical AND.
True only if all the operands are true.
||    expression1 || expression2    Logical OR.
True if at least one of the operands is true.
!    !expression    Logical NOT.
True only if the operand is false.

In C++, logical operators are commonly used in decision-making. To further understand the logical operators, let's see the following examples,
Suppose,
a = 5
b = 8

Then,

(a > 3) && (b > 5) evaluates to true
(a > 3)  && (b < 5) evaluates to false

(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false

!(a < 3) evaluates to true
!(a > 3) evaluates to false


C++ program to implement the various relational operators 

Declare a Boolean type variable to store the status of the logical operations.
Explanation of logical operator program
•    (3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
•    (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false).
•    (3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
•    (3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
•    (3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
•    (3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
•    !(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
•    !(5 == 5) evaluates to 0 because the operand (5 == 5) is 1 (true).
 
Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main();

Step 4: Declare a boolean type variable result.

Step 5: check (3 != 5) && (3 < 5); call step 13

Step 6: check (3 == 5) && (3 < 5); call step 13

Step 7: Check (3 == 5) && (3 > 5); call step 13

Step 8: Check (3 != 5) || (3 < 5); call step 13

Step 9: Check (3 != 5) || (3 > 5); call step 13

Step 10: Check (3 == 5) || (3 > 5); call step 13

Step 11: Check !(5 == 2);  call step 13
;
Step 12: Check !(5 == 5); call step 13

Step 13: Print the value of the variable result.

Step 14: Exit;
 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
    bool result;

    result = (3 != 5) && (3 < 5);     // true
    cout << "(3 != 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 < 5);    // false
    cout << "(3 == 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 > 5);    // false
    cout << "(3 == 5) && (3 > 5) is " << result << endl;

    result = (3 != 5) || (3 < 5);    // true
    cout << "(3 != 5) || (3 < 5) is " << result << endl;

    result = (3 != 5) || (3 > 5);    // true
    cout << "(3 != 5) || (3 > 5) is " << result << endl;

    result = (3 == 5) || (3 > 5);    // false
    cout << "(3 == 5) || (3 > 5) is " << result << endl;

    result = !(5 == 2);    // true
    cout << "!(5 == 2) is " << result << endl;

    result = !(5 == 5);    // false
    cout << "!(5 == 5) is " << result << endl;

    return 0;
}

                                      

OUTPUT

(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0