Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Logical Operators: AND(&&) OR(||) NOT(!) in c++

Tags: ltlt

Logical Operators: AND(&&) OR(||) NOT(!) in c++



To understand this chapter better, first go through relational operators. In a program, we often need to test more than one condition. To simplify this logical operators were introduced. In your school you might have learnt Boolean Algebra (Logic).


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)


/* 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 2 && 3     cout << "3     cout << endl;
    cout << "3 > 2 || 3 > 1: " << (3 > 2 || 3 > 1) << endl;
    cout << "3 > 2 || 3 2 || 3     cout << "3     cout << endl;
    cout << "! (3 == 2): " << ( ! (3 == 2) ) << endl;
    cout << "! (3 == 3): " << ( ! (3 == 3) ) << endl;

    return 0;
}


In earlier chapter Selection Statement (if-else if-else), we were required to check two conditons: for username and password. If both username and password are correct then only "You are logged in!" message appears. We checked these two condition using nested if-else statement. However i told you at the end of that chapter that in the "Program 1" nesting can be avoided by using logical operator. Here we will rewrite the "Program 1" of Selection Statement (if-else if-else) using logical operator.

/* 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;
}


This post first appeared on C++ Programming, please read the originial post: here

Share the post

Logical Operators: AND(&&) OR(||) NOT(!) in c++

×

Subscribe to C++ Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×