Kamis, 12 Januari 2012

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).


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


// Program 1

#include

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

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.


// Program 2

#include
#include

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


// Program 3

#include
#include

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


// Program 4

#include

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


// Program 5

#include

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


Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


View the original article here



Peliculas Online

0 komentar:

Posting Komentar