ADET Present
Logical Operators: AND(&&) OR(||) NOT(!) in c++
- You must be understand about first go through relational operators.
- We need to test more than one condition.
- To simplify this logical operators were introduced.
- You are touched and learned from your eldest school you might have learnt Boolean Algebra (Logic).
- That as same formulate it.But it is translate code program in C and C++.
The three logical operators ( AND, OR and NOT ) are as follows:
1) AND (&&) : Returns true only if both operand are true.
2) OR (||) : Returns true if one of the operand is true.
3) NOT (!) : Converts false to true and true to false.
Operator | Operator's Name | Example | Result |
---|---|---|---|
&& | AND | 3>2 && 3>1 | 1(true) |
&& | AND | 3>2 && 3<1 | 0(false) |
&& | AND | 3<2 && 3<1 | 0(false) |
|| | OR | 3>2 || 3>1 | 1(true) |
|| | OR | 3>2 || 3<1 | 1(true) |
|| | OR | 3<2 || 3<1 | 0(false) |
! | NOT | !(3==2) | 1(true) |
! | NOT | !(3==3) | 0(false) |
using namespace std;
int main ()
{
cout << "3 > 2 && 3 > 1: " << (3 > 2 && 3 > 1) << endl;
cout << "3 > 2 && 3 < 1: " << (3 > 2 && 3 < 1) << endl;
cout << "3 < 2 && 3 < 1: " << (3 < 2 && 3 < 1) << endl;
cout << endl;
cout << "3 > 2 || 3 > 1: " << (3 > 2 || 3 > 1) << endl;
cout << "3 > 2 || 3 < 1: " << (3 > 2 || 3 < 1) << endl;
cout << "3 < 2 || 3 < 1: " << (3 < 2 || 3 < 1) << endl;
cout << endl;
cout << "! (3 == 2): " << ( ! (3 == 2) ) << endl;
cout << "! (3 == 3): " << ( ! (3 == 3) ) << endl;
return 0;
}
No comments:
Post a Comment