C++Data TypesBoolean

The bool Data Type in C++

In C++, the bool (Boolean) data type is used to represent logical values: true or false. It is essential in decision-making and conditional expressions, forming the backbone of control structures like if, while, and for.


What is bool?

The bool type can only hold one of two values:

  • true (internally represented as 1)
  • false (internally represented as 0)
bool_basic.cpp
#include <iostream>
 
int main() {
    bool isRaining = true;
    std::cout << "Is it raining? " << isRaining << std::endl;
    return 0;
}

Output:

Is it raining? 1

std::cout prints 1 for true and 0 for false by default.


Declaring and Initializing bool

You can declare a bool variable just like any other variable, using the keyword bool.

bool_declaration.cpp
#include <iostream>
 
int main() {
    bool isLoggedIn = false;
    std::cout << "User logged in: " << isLoggedIn << std::endl;
    return 0;
}

Boolean Expressions

Boolean values often result from relational or logical expressions.

bool_expression.cpp
#include <iostream>
 
int main() {
    int age = 20;
    bool isAdult = age >= 18;
 
    std::cout << "Is adult? " << isAdult << std::endl;
    return 0;
}

Logical Operators with bool

OperatorDescriptionExample
&&Logical ANDtrue && false
``Logical OR`truefalse`
!Logical NOT!true
bool_operators.cpp
#include <iostream>
 
int main() {
    bool a = true;
    bool b = false;
 
    std::cout << "a && b: " << (a && b) << std::endl;
    std::cout << "a || b: " << (a || b) << std::endl;
    std::cout << "!a: " << (!a) << std::endl;
 
    return 0;
}

Conditional Usage

bool values are commonly used in if statements to control the flow of programs.

bool_if.cpp
#include <iostream>
 
int main() {
    bool isOnline = true;
 
    if (isOnline) {
        std::cout << "The user is online." << std::endl;
    }
 
    return 0;
}

Type Conversion to/from bool

  • Non-zero numbers are treated as true
  • Zero is treated as false
bool_conversion.cpp
#include <iostream>
 
int main() {
    bool x = 0;     // false
    bool y = 42;    // true
 
    std::cout << "x: " << x << ", y: " << y << std::endl;
    return 0;
}

You can also convert a bool to an int explicitly:

bool_to_int.cpp
#include <iostream>
 
int main() {
    bool condition = true;
    int value = static_cast<int>(condition);
 
    std::cout << "Integer value: " << value << std::endl;
    return 0;
}

Practice Problem 1: Voter Eligibility

Check if a person is eligible to vote based on age.

voter_eligibility.cpp
#include <iostream>
 
int main() {
    int age;
    std::cout << "Enter your age: ";
    std::cin >> age;
 
    bool eligible = age >= 18;
    std::cout << "Eligible to vote? " << eligible << std::endl;
 
    return 0;
}

Practice Problem 2: Even or Odd Checker

Use a boolean expression to check if a number is even.

even_checker.cpp
#include <iostream>
 
int main() {
    int num = 12;
    bool isEven = (num % 2 == 0);
 
    std::cout << "Is even? " << isEven << std::endl;
    return 0;
}

Practice Problem 3: Login System Simulation

Simulate a simple login check using a boolean.

login_simulation.cpp
#include <iostream>
#include <string>
 
int main() {
    std::string username = "admin";
    std::string password = "1234";
 
    bool isAuthenticated = (username == "admin" && password == "1234");
 
    std::cout << "Access granted? " << isAuthenticated << std::endl;
    return 0;
}

Summary

FeatureDescription
Typebool
Size1 byte
Valuestrue (1) and false (0)
Common usesConditions, flags, logical testing
Default output1 for true, 0 for false
Can be convertedImplicitly to/from integers

The bool data type is vital in building logic-driven programs. It controls flow, manages flags, and simplifies conditional operations.