EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Boolean Operators in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Boolean Operators in C++

Boolean Operators in C++

Overview of Boolean Operators in C++

Boolean operators are used for performing boolean operations, in order to validate the relationship between the operands and it return either 0 or 1. This 0 or 1 output is equivalent to false or true return value respectively. In C++, for performing the boolean operations along with the object oriented concepts programming, three types of boolean operators are used. They are ‘&&’ for AND operation that results in true value when both the input is true, ‘||’ for OR operation that results in true when at least one input is true, and ‘!’ for NOT operation that results in exact opposite value from that of the input value.

Examples of Boolean Operators in C++

The examples of boolean operators in c++ are explained below.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Example # 1 – AND Boolean Operator!

This Boolean operator is represented by “&&” together in C++ programming language and it is also known as an ampersand. This operator has conditions on both sides. So it takes left value from the operators and then the right value from the operator if both values match it returns true otherwise it returns a false value. In a simpler word, we can say that in regular English writing we only use and when we need both the things or both the conditions are necessary. It means if both conditions are true then the only output will be true otherwise for the rest of the conditions it will be false. “If an expression needs to be proved true then both the conditions must be true.”

Here is the C++ code to demonstrate AND Boolean operator by checking age between the given range for medical insurance:

Code:

#include <iostream>
using namespace std;
int main ()
{
int your_age;
cout << " Please enter your age here: " ;
cin >> your_age;
if ( your_age >= 20 && your_age <= 70 )
{
cout << " Congratulations ! You're between 20 and 70 and you can save money on your medical insurance!" << endl;
}
else
{
cout << " We are so sorry, we don't have any deals for you for this month! " << endl;
}
return 0;
}

Output:

Boolean Operators in C++ 1-1

Example #2 – OR Boolean Operator!

This Boolean operator is represented by “||” together in C++ programming language and it is also known as logical OR. This operator also has conditions on both sides. But it’s not like AND operator as it is OR operators which means if even a single condition of any side is true then it will return a true value. In a simpler word, we can say that in regular English writing we only use or when we have the choice out of two options that even if other is not fine you will anyhow pick the left one. Only one condition is necessary. It means if any single conditions are true then the only output will be true otherwise for the rest of the conditions it will be false. “If an expression needs to be proved true then only one condition needs to be true.”

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,890 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Here is the C++ code to demonstrate OR Boolean operator by checking age between the given range:

Code:

#include <iostream>
using namespace std;
int main ()
{
int age;
cout << " Please enter your age here: " ;
cin >> age;
if ( age < 0 || age > 125 )
{
cout << " Ha Ha Ha You're lying - you CANNOT be that age. Impossible " << endl;
}
else
{
cout << " Great! Thanks for providing your age ! " << endl;
}
return 0;
}

Output:

OR Boolean Operator!

Example #3 – NOT Boolean Operator!

This Boolean operator is represented by “!” in C++ programming language and it is also known as logical NOT operator. This operator has no such conditions on both sides. In fact, it has only one aim to invert the value of the given Boolean expression as only one single expression can be prefixed to it. In a simpler word, we can say that in regular English writing we only use not when we don’t want something or we can say that not in favor like opposition. “If an expression needs to be proved false or true depending upon the expression prefixed to it always use NOT operator.”

Here is the C++ code to demonstrate NOT Boolean operator by checking age between the given range:

Code:

#include <iostream>
using namespace std;
int main ()
{
bool initiate;
cout << " Hey ! Do you really want to initialise the application ? [0: No] [1: Yes] " << endl;
cin >> initiate ; // 0 input is false', and 1 is 'true'
if ( !initiate )
{
cout << " Then why would you open the application ? Haha Funny, too bad, I'm starting now anyway. " << endl;
}
cout << " Application initialized. " << endl;
// you can continue writing the main program code here
return 0;
}

Output:

C++ code

Conclusion

Above all the three codes are separately demonstrated using three Boolean C + + operators such as AND, OR and NOT. Every single operator has its own specialty which can be used depending upon features and operations you want to implement in software or programs. AND operator is used when you want both of the given conditions to be the same or satisfied to proceed with the code. OR operators are used when you want only one of the given conditions to be true to proceed with the code.

NOT operator is used when you want to give one statement that can be used to handle two statements simultaneously. In Conclusion, Boolean operators in the C + + programming language are very useful in programming as it helps to solve complex operations in bits of time without occupying any memory space. Boolean operators are widely used in database management because it helps in narrowing and broadening the search based on a given query.

Recommended Articles

This is a guide to Boolean Operators in C++. Here we discuss the overview and examples of boolean operators in c++ along with the code implementation. You may also look at the following articles to learn more-

  1. Unary Operators in C++
  2. Storage Class in C++
  3. Deque in C++?
  4. Friend Function in C++

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More