Logical Expression in C++ language

The logical expression is the combination of relational expressions, logical variables or constants separated by logical operators. The result of a logical expression is in the Boolean. The logical operators used to specify the relationship between the operands are AND (&&), NOT (!)and OR (||). Logical expressions determine the path your program logic takes through conditional and looping statements like if, while, for and case. 

1. AND (&&) operator returns true only if both operand are true in the expression. For example, in this expression result = (3>2 && 4>1), the value of the result is true. Similarly, in this expression result = (3>12 && 4>1), the value of the result is false. 
2. NOT (!) operator convert true to false and false to true in the expression. For example, in the expression result = !(23==23) then the value of the result is false. Similarly, in the expression result = !(13==23) then the value of the result is true.  
3. OR (||) operator returns true if one of the operand is true in the expression. For example, in this expression result = (3>2 && 4<1), the value of the result is true. Similarly, in this expression result = (3>12 && 14>1), the value of the result is true.

Write a C++ program which determine a Logical Operator.

#include<iostream>
using namespace std;
int main()
{
int x, y, z, result;
cout<<" Enter the values of x, y and z : ";
cin>>x>>y>>z;
result = (x>y) && (x>z);
cout<<" The value of the expression (x>y) && (x>z) is :"<<result;
cout<<endl;
result = (x>y) || (x>z);
cout<<" The value of the expression (x>y) || (x>z) is :"<<result;
cout<<endl;
result = !(x>y) ;
cout<<" The value of the expression !(x>y)  :"<<result;
cout<<endl;
return 0;
}

Explanation: In the above program we are using Logical expression ( the set of operators and operands) like result = (x>y) && (x>z) for AND operator, result = (x>y) || (x>z) for OR operator and result = !(x>y) for NOT operator;

Note: This program output is tested on www.jdoodle.con

Output:

Enter the values of x, y and z : 2  1  3

The value of the expression (x>y) && (x>z) is :0 ( false)
The value of the expression (x>y) || (x>z) is :1 (true)
The value of the expression !(x>y)  :0 (false)


Thanks 
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: