Thursday, November 21, 2013

                                       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.

OperatorOperator's NameExampleResult
&&AND3>2 && 3>11(true)
&&AND3>2 && 3<10(false)
&&AND3<2 && 3<10(false)
||OR3>2 || 3>11(true)
||OR3>2 || 3<11(true)
||OR3<2 || 3<10(false)
!NOT!(3==2)1(true)
!NOT!(3==3)0(false)

Program-I


#include <iostream>

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