The concept of Boolean logic has existed since the mid-1800s. We’ve seen it in mathematics and mathematical logic, but Boolean logic also happens to be an integral part of programming, with C++ being no exception. But to effectively use Booleans in C++, we’ll need to understand them first.
In this article, we’ll take a detailed look at the Boolean data type in C++. First, we’ll uncover what Booleans are. Then, through a series of examples, we’ll see how Booleans work in code and when to use them.

What Are Boolean Variables in C++?

We’re often faced with yes/no questions and true/false statements in the real world. Is the sky blue? Yes. An ant is bigger than an elephant. False.
As you learn C++, you’ll see that there will be times when you’ll need to address similar yes/no or true/false situations. Boolean variables in C++ convey these types of statements in code.
Simply put, a Boolean variable can only have two possible values: true or false. In C++, we use the keyword bool to declare this kind of variable. Let’s take a look at an example:

bool b1 = true;
bool b2 = false;

In C++, Boolean values declared true are assigned the value of 1, and false values are assigned 0.
Now, here’s what happens when we print these same boolean variables:

#include

using namespace std;

int main()
{

bool b1 = true;
bool b2 = false;

cout << b1 <<" , "<< b2;

return 0;
}

This code returns the following:

1 , 0

Our output here demonstrates what Boolean values look like in C++.
Further, we can use Boolean values with commands like if statements to return the desired output:

#include

using namespace std;

bool isEqual(int x, int y)
{
return (x == y);
}

int main()
{
cout << "Enter an integer: "; int x{}; cin >> x;

cout << "Enter another integer: "; int y{}; cin >> y;

std::cout << "Are " << x << " and " << y << " equal? "<<endl;
if(isEqual(x,y)==true)
cout << "Yes, they are equal!";
else cout << "No, they are not equal.";

return 0;
}

Here, we created a Boolean variable isEqual and used our program to ask for two integers. Then, the code compares the two integers to see if they are equal (true) or not equal (false).
As we can see below, the program will return one of two strings, depending on whether our variable isEqual is true or false:

Enter an integer: 5
Enter another integer: 5
Are 5 and 5 equal?
Yes, they are equal!

Enter an integer: 8
Enter another integer: 4
Are 8 and 4 equal?
No, they are not equal.

The output shows the values inputted for the integers and then tells us if they’re the same.

What Makes the Boolean Type Useful?

Boolean variables in C++ may seem unnecessary at first glance. After all, they basically represent either a 1 or a 0 in code. These values can be just as easily represented by an integer or something similar.
So why do we use them? The answer lies in the Boolean variable’s readability, convenience and utility.
Any time you use a Boolean variable in C++, you’re declaring that the variable can have only one of two possible values: true (1) or false (0). This helps to limit the scope of if statements to an input of true or false. If we change our variable to an integer, we introduce the potential for issues if the variable is assigned anything other than a 1 or a 0.
Here’s an example below:

#include

using namespace std;

int main()
{
int b1 = 5;

if(b1==true)
cout<<"The value is true";
else
cout<<"The value is false";

return 0;
}

The example shows the program returns The value is false because it’s confused about what’s going on. We would have to expand our code and modify our if statement in order to allow b1 to function as an integer.
Limiting our range of variables means fewer opportunities for error in code when a variable can only be used in one of two ways.

Converting Non-Boolean Types to Boolean

There are times in C++ when a value needs to be converted into another data type. This is typically done to satisfy the requirements of a particular expression. Below we’ll cover the two types of conversion in C++.

Implicit Type Conversion

The compiler performs implicit type conversion on its own without input from the programmer. Variables are upgraded or downgraded to accommodate the expression. Take a look at this example:

#include

using namespace std;

int main()
{
int x=10;
bool y=true;

y=x+y;

cout << y;

return 0;
}

Here we declare an integer and a Boolean value. When we tell the program to add the two together and reassign the value to our Boolean variable, the integer is converted to Boolean. This gives us a Boolean output of 1, which we know is how Booleans store a true statement.

Explicit Type Conversion

Also known as typecasting, explicit type conversion requires the user to manually change the type of a variable to meet the program’s needs.
To do so, the programmer must place the new type in parentheses, followed by the new expression. Here’s an example of what this looks like:

(type) expression

Explicit type conversion works with many different types of variables. In our example below, we get a look at how type casting works with Boolean values:

#include

using namespace std;

int main()
{
int x=5;
x = (bool) 1;
cout << "x equals " << x;

return 0;
}

We initially assign x an integer value (in this case, 5). However, once we explicitly assign x to be the Boolean value 1, we force the program to change its type.
We see here that:

X equals 1

As expected, the value of x after it’s been typecast into a boolean type is now 1.

Converting Integers and Floating Point Numbers to Boolean

When it comes to converting integers or floating-point numbers to Boolean, an interesting pattern emerges.
An integer or floating-point number assigned a value of 0 will remain a 0 when converted to Boolean. Effectively, these values evaluate to “false” when used elsewhere in the program.
However, any other number from negative infinity to infinity evaluates to true when converted to the Boolean data type. The below example shows what this conversion looks like:

#include

using namespace std;

int main()
{
int x=10;
int y=0;

x = (bool) x;
y = (bool) y;

if(x==true)
cout << "x is true" << endl;
else
cout << "x is false" << endl;

if(y==true)
cout << "y is true";
else
cout << "y is false"; return 0; }

It should come as no surprise that we see the following result: x is true y is false Converting Strings to Boolean Interestingly, all valid strings in C++ code return true when converted to the Boolean data type, regardless of what the string is or what it says. In fact, even an empty string returns a value of true. This occurs because all strings declare a non-null pointer at initialization. In simpler terms, a pointer refers to a variable that holds the address of another variable. Since the pointer variable is not null (not zero), it will return as true. Because strings are never considered null, you won’t be able to use logical operators to check if a string is empty. To do so, you’ll need to use the command string::length() > 0.

Fun Fact: The Origin of the Word ‘Boolean’

Just in case you were wondering, the term ‘Boolean’ is thanks to George Boole, a 19th century English mathematician. Though Boole lived well before the introduction of computers, his contributions to mathematical logic gave us the basis for the Boolean data type.
Today, the term ‘Boolean’ not only applies to the C++ data type that represents true/false, but it also appears in many other branches of mathematics, including probability theory and commutative algebra.

Become a Programmer With Udacity

With a deeper understanding of Booleans under your belt, you’re well on your way to becoming a C++ programmer.
Looking to learn C++ online? We’ll get you there with our interactive C++ Nanodegree program, where you’ll learn from industry experts and even code five projects of your own.
Enroll in our C++ Nanodegree program today!