C++ Booleans

Welcome to The Coding College! In this tutorial, we will explore Booleans in C++, a fundamental concept used to represent logical states like true and false. Booleans are crucial in decision-making and control flow in your programs.

What Is a Boolean in C++?

A Boolean is a data type in C++ used to represent one of two possible values:

  1. true (logical true)
  2. false (logical false)

C++ uses the keyword bool to define Boolean variables.

Syntax for Booleans

#include <iostream>
using namespace std;

int main() {
    bool isCodingFun = true;
    bool isWeatherGood = false;

    cout << "Is coding fun? " << isCodingFun << endl;
    cout << "Is the weather good? " << isWeatherGood << endl;

    return 0;
}

Output:

Is coding fun? 1  
Is the weather good? 0  

Note: In C++, true is represented as 1 and false as 0 when outputted.

Declaring Boolean Variables

You can declare and initialize Boolean variables like any other data type.

Example:

bool isLearning = true;
bool isTired = false;

Boolean Expressions

Boolean values are often the result of comparison operators or logical operations.

Comparison Operators

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
<Less thana < b
>Greater thana > b
<=Less than or equal toa <= b
>=Greater than or equal toa >= b

Logical Operators

OperatorDescriptionExample
&&Logical AND (both conditions true)(a > b) && (b < c)
``
!Logical NOT (invert true/false)!(a > b)

Example: Boolean Expressions

#include <iostream>
using namespace std;

int main() {
    int x = 5, y = 10;

    bool result1 = (x < y);  // true
    bool result2 = (x > y);  // false
    bool result3 = (x == 5) && (y == 10);  // true
    bool result4 = (x == 5) || (y == 15);  // true
    bool result5 = !(x == y);  // true

    cout << "x < y: " << result1 << endl;
    cout << "x > y: " << result2 << endl;
    cout << "(x == 5) && (y == 10): " << result3 << endl;
    cout << "(x == 5) || (y == 15): " << result4 << endl;
    cout << "!(x == y): " << result5 << endl;

    return 0;
}

Output:

x < y: 1  
x > y: 0  
(x == 5) && (y == 10): 1  
(x == 5) || (y == 15): 1  
!(x == y): 1  

Boolean in Conditional Statements

Booleans are commonly used in conditional statements like if, while, and for.

Example: Using Booleans in if

#include <iostream>
using namespace std;

int main() {
    bool isAdmin = true;

    if (isAdmin) {
        cout << "Welcome, Admin!" << endl;
    } else {
        cout << "Access Denied!" << endl;
    }

    return 0;
}

Output:

Welcome, Admin!  

Example: Using Booleans in Loops

#include <iostream>
using namespace std;

int main() {
    int count = 0;
    bool keepCounting = true;

    while (keepCounting) {
        cout << "Count: " << count << endl;
        count++;

        if (count == 5) {
            keepCounting = false;  // Stop the loop
        }
    }

    return 0;
}

Output:

Count: 0  
Count: 1  
Count: 2  
Count: 3  
Count: 4  

Booleans and Implicit Conversion

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

  • Non-zero values are true.
  • Zero is false.

Example:

#include <iostream>
using namespace std;

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

    if (x) {
        cout << "x is true!" << endl;
    }

    if (!y) {
        cout << "y is false!" << endl;
    }

    return 0;
}

Output:

x is true!  
y is false!  

Boolean Size in Memory

A bool typically occupies 1 byte of memory.

Example:

#include <iostream>
using namespace std;

int main() {
    cout << "Size of bool: " << sizeof(bool) << " byte" << endl;
    return 0;
}

Output:

Size of bool: 1 byte  

Summary

  • Booleans in C++ are represented by the bool type and can have values true or false.
  • Commonly used with comparison and logical operators.
  • Essential in control flow statements (if, while, etc.).
  • Non-zero values are implicitly converted to true, while zero is false.

Explore More at The Coding College

Visit The Coding College for more tutorials on C++ basics, advanced concepts, and programming techniques.

What’s Next?

  • Learn about Boolean operators in depth.
  • Explore how Booleans are used in functions and return values.
  • Dive into control flow structures in C++.

Leave a Comment