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.
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;
}
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;
}
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.
Want to learn more? View List Of All Chapters
this is a very help full site.
ReplyDeleteIn the tabular example just above the statement :
ReplyDelete/* A c++ program example that demonstrate the working of logical operators. */
You have used && instead of || for the OR case..
The post was helpful anyways..
Fixed!:D Thanks for pointing! ;)
Deletethanks man for that work and i hope to see more staff
ReplyDeletecan i get assistant on writing a program to prompt the user to log into the system using a password,create an account if he is a new member,log out when the session is over...using object oriented programming in c++.
ReplyDeletesuperb way of explaining, and great blog to get wonderful information.
ReplyDeletec++ programming
This comment has been removed by the author.
ReplyDeletec sample codes
ReplyDeleteCircle Through Three Points
nice article for beginners.thank you.
ReplyDeletewelookups C++
javacodegeeks