C++ Booleans


What Are Booleans in C++?

In C++, Booleans represent the simplest form of data — values that are either true or false. They are mainly used in decision-making, logical operations, and conditional statements like if, while, and for.

A Boolean variable is declared using the bool keyword.
It can hold only two possible values:

  • true (which represents 1)

  • false (which represents 0)

Example:

#include <iostream>
using namespace std;

int main() {
    bool isRaining = true;
    bool isSunny = false;

    cout << "Is it raining? " << isRaining << endl;
    cout << "Is it sunny? " << isSunny;
    return 0;
}

Output:

Is it raining? 1
Is it sunny? 0

Here, true is displayed as 1 and false as 0.
Even though the values are printed as numbers, they are treated as logical states in the program.

Declaring Boolean Variables

A Boolean variable is declared using the syntax:

bool variable_name = value;

Example:

bool isStudent = true;
bool isPassed = false;

You can also assign the values 1 or 0 directly, and C++ automatically converts them to true or false.

Example:

bool isValid = 1;
bool isExpired = 0;

Boolean Output Formatting

By default, C++ prints true as 1 and false as 0.
If you want to print the actual words “true” or “false,” you can use boolalpha.

Example:

#include <iostream>
using namespace std;

int main() {
    bool hasAccess = true;
    bool isBlocked = false;

    cout << boolalpha; // Enables textual representation
    cout << "Has access: " << hasAccess << endl;
    cout << "Is blocked: " << isBlocked;
    return 0;
}

Output:

Has access: true
Is blocked: false

The boolalpha manipulator makes the output more readable, especially in condition-based programs.

Boolean Expressions in C++

A Boolean expression is an expression that results in either true or false.
It often involves relational operators or logical operators.

Example:

int a = 10, b = 5;
bool result = a > b;
cout << result;

Output:

1

Here, the expression a > b is true, so the result is 1.

Relational Operators That Return Booleans

Relational operators are used to compare two values.
They return a Boolean result — true or false.

Operator Description Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 10 > 3 true
< Less than 2 < 1 false
>= Greater than or equal to 4 >= 4 true
<= Less than or equal to 6 <= 7 true

Example Program:

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    bool canVote = (age >= 18);
    cout << boolalpha;
    cout << "Can vote: " << canVote;
    return 0;
}

Output:

Can vote: true

Logical Operators with Booleans

Logical operators combine multiple Boolean expressions.
They return true or false depending on the combined result.

Operator Description Example Result
&& Logical AND (a > 0 && b > 0) true if both are true
`   ` Logical OR
! Logical NOT !(a > b) reverses the result

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 8, y = 12;

    bool result1 = (x > 0 && y > 0);
    bool result2 = (x > 10 || y > 10);
    bool result3 = !(x == y);

    cout << boolalpha;
    cout << "Both positive: " << result1 << endl;
    cout << "Any greater than 10: " << result2 << endl;
    cout << "Not equal: " << result3;
    return 0;
}

Output:

Both positive: true
Any greater than 10: true
Not equal: true

Booleans in Conditional Statements

Booleans are commonly used in if, else if, and while conditions.
These statements execute code based on whether a Boolean expression is true or false.

Example:

#include <iostream>
using namespace std;

int main() {
    bool isRegistered = true;

    if (isRegistered) {
        cout << "Access granted";
    } else {
        cout << "Access denied";
    }

    return 0;
}

Output:

Access granted

You can also directly use expressions without creating separate Boolean variables.

Example:

int marks = 75;
if (marks >= 50)
    cout << "Pass";
else
    cout << "Fail";

Booleans in Loops

Booleans also control loops.
A loop continues running as long as the condition remains true.

Example:

#include <iostream>
using namespace std;

int main() {
    int count = 1;
    while (count <= 5) {
        cout << "Count: " << count << endl;
        count++;
    }
    return 0;
}

The loop runs five times because the condition (count <= 5) stays true until count becomes 6.

Boolean Conversion in C++

C++ automatically converts non-Boolean values to Boolean:

  • Zero (0) → false

  • Non-zero → true

Example:

#include <iostream>
using namespace std;

int main() {
    int num = 0;

    if (num)
        cout << "True block";
    else
        cout << "False block";

    return 0;
}

Output:

False block

Here, num is 0, which means false. If it were any non-zero value, the if condition would be true.

Summary of C++ Booleans

Booleans in C++ are essential for making decisions in programs. They represent true and false values and are used in comparisons, loops, and logical operations. With boolalpha, you can display Booleans in a more readable form. Understanding Boolean logic helps you control program flow effectively and build intelligent conditions in your code.


Practice Questions

  1. What is a Boolean data type in C++ and what values can it store?

  2. How does C++ represent true and false when printing Boolean values without boolalpha?

  3. What is the purpose of the boolalpha manipulator in C++?

  4. Write a program to check whether a person is eligible to vote based on their age.

  5. Explain the difference between relational operators and logical operators in C++.

  6. Write a program using logical operators to check if a number is between 10 and 50.

  7. What does the ! (NOT) operator do in a Boolean expression?

  8. Write a program that checks whether a number entered by the user is positive, negative, or zero using Boolean logic.

  9. How does C++ treat non-Boolean values (like integers) when used in conditional statements?

  10. Write a program using a Boolean-controlled while loop that runs until the user enters a negative number.


Try a Short Quiz.

C++ Booleans Quiz

Finished the tutorial? Play this medium level quiz to strengthen your concepts.


Go Back Top