Pages

Logical Operators: AND(&&) OR(||) NOT(!) in 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)

/* A c++ program example that demonstrate the working of logical operators. */


// Program 1

#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;
}


/* A c++ program example that uses a logical operator in selection statement if-else.*/


// Program 2

#include <iostream>
#include <string>

using namespace std;

const string userName = "computergeek";
const string passWord = "break_codes";

int main ()
{
    string name, pass;

    cout << "Username: ";
    cin >> name;

    cout << "Password: ";
    cin >> pass;

    if (name == userName && pass == passWord)
    {
        cout << "You are logged in!" << endl;
    }

    else
        cout << "Incorrect username or password." << endl;

    return 0;
}


/* A simple c++ program example that uses AND logical operator */


// Program 3

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int first, second, third;

    cout << "Enter three integers." << endl;

    cout << "First "<< setw (3) << ": ";
    cin >> first;

    cout << "Second "<< setw (2) << ": ";
    cin >> second;

    cout << "Third "<< setw (3) << ": ";
    cin >> third;

    if (first > second && first > third)
        cout << "first is greater than second and third." << endl;

    else if (second > first && second > third)
        cout << "second is greater than first and third." << endl;

    else if (third > first && third > second)
        cout << "third is greater than first and second." << endl;

    else
        cout << "first, second and third are equal." << endl;

    return 0;
}

/* A simple c++ program example that uses OR logical operator*/


// Program 4

#include <iostream>

using namespace std;

int main ()
{
    char agree;

    cout << "Would you like to meet me (y/n): ";
    cin >> agree;

    if (agree == 'y' || agree == 'Y')
    {
        cout << "Your name: ";
        string name;
        cin >> name;
        cout << "Glad to see you, "+name << endl;
    }

    else if (agree == 'n' || agree == 'N')
        cout << "See you later!" << endl;

    else
        cout << "Please enter 'y' or 'n' for yes or no." << endl;

    return 0;
}


/* A simple c++ program example that uses NOT logical operator*/


// Program 5

#include <iostream>

using namespace std;

int main ()
{
    char agree;

    cout << "Would you like to meet me?" << endl;
    cout << "Press 'y' for yes and any other character for no: ";
    cin >> agree;

    if ( ! (agree == 'y' || agree == 'Y') )
    {
        cout << "See you later!" << endl;
    }

    else
    {
        cout << "Your name: ";
        string name;
        cin >> name;
        cout << "Glad to see you, "+name << "!" << endl;
    }

    return 0;
}

1 comment:

  1. Lucky Club: Live Blackjack, Slots and Table Games
    Lucky Club offers a live casino experience in an authentic way. luckyclub.live Live Blackjack and Roulette have become household favorites in the casino industry since the

    ReplyDelete