C++ Booleans

Understanding true and false values in C++

🔢 What are C++ Booleans?

C++ booleans represent true or false values. They are essential for making decisions in your programs and controlling program flow with logical operations.


#include <iostream>
using namespace std;

int main() {
    bool isTrue = true;
    bool isFalse = false;
    
    cout << "True value: " << isTrue << endl;
    cout << "False value: " << isFalse << endl;
    
    return 0;
}
                                    

Output:

True value: 1

False value: 0

Key Boolean Concepts

True Value

Represents a true condition

bool isReady = true;

False Value

Represents a false condition

bool isFinished = false;
🔍

Comparisons

Compare values to get booleans

bool result = (5 > 3);
🔗

Logical Operations

Combine boolean values

bool combined = true && false;

🔹 Boolean Declaration

Boolean variables in C++ are declared with the bool keyword, storing true (1) or false (0) values. They represent logical states, like isStudent = true (1) or canVote = true (1) based on age checks. Booleans are fundamental for control flow, enabling conditional execution in if statements, loops, and flags. Their use improves code readability and logic clarity, forming the backbone of decision-making structures in algorithms and user-state management.

#include <iostream>
using namespace std;

int main() {
    // Direct assignment
    bool isStudent = true;
    bool hasLicense = false;
    
    // From expressions
    bool canVote = (18 >= 18);
    bool isAdult = (25 > 21);
    
    cout << "Is student: " << isStudent << endl;
    cout << "Can vote: " << canVote << endl;
    
    return 0;
}

Output:

Is student: 1

Can vote: 1

🔹 Comparison Operators

Comparison operators (==, !=, >, <,>=, <=) evaluate relationships between two values, returning a boolean true or false. They are the backbone of conditional statements and loops, enabling decision-making in programs. Proper use involves ensuring operands are compatible types and being wary of floating-point comparisons due to precision issues. These operators control program flow and logic gates in everything from sorting algorithms to user authentication.

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;
    
    cout << "a == b: " << (a == b) << endl;  // Equal to
    cout << "a != b: " << (a != b) << endl;  // Not equal
    cout << "a > b: " << (a > b) << endl;    // Greater than
    cout << "a < b: " << (a < b) << endl;    // Less than
    cout << "a >= b: " << (a >= b) << endl;  // Greater or equal
    cout << "a <= b: " << (a <= b) << endl;  // Less or equal
    
    return 0;
}

Output:

a == b: 0

a != b: 1

a > b: 1

a < b: 0

a >= b: 1

a <= b: 0

🔹 Logical Operators

Logical operators combine or invert boolean expressions to form complex conditions. Using && (AND), || (OR), and ! (NOT), you can chain tests: a && b yields true only if both are true, a || b if at least one is true, and !a inverts the value. These are vital in control flow, enabling multi-part conditions (e.g., authentication checks), state machines, and filtering logic, making programs more expressive and adaptable to varied scenarios.

#include <iostream>
using namespace std;

int main() {
    bool a = true, b = false;
    
    cout << "a && b: " << (a && b) << endl;  // AND
    cout << "a || b: " << (a || b) << endl;  // OR
    cout << "!a: " << (!a) << endl;         // NOT
    cout << "!b: " << (!b) << endl;         // NOT
    
    return 0;
}

Output:

a && b: 0

a || b: 1

!a: 0

!b: 1

🧠 Test Your Knowledge

What is the output of: cout << (5 > 3);