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


January 25, 2023, Learn eTutorial
1217

Here, the program discusses the logical operators in C++.

What are Logical operators in C++?

Logical operators in C++ can be defined as the operators or the symbols which help to expressions and to make the resultant expression. The value of the resultant will only depend on the input expression and the logical operator's meaning. Common logical operators include AND, OR, and NOT. it is mainly used to check the validity of comparative operations and in decision-making. 

  • True: false
  • False: true

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

  1. (3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
  2. (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false).
  3. (3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
  4. (3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
  5. (3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
  6. (3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
  7. !(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
  8. !(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