C++ Boolean Examples

Welcome to The Coding College! In this tutorial, we’ll focus on Boolean Examples in C++ to help you understand and apply Boolean concepts in practical scenarios. By exploring examples, you’ll see how Booleans are used in conditions, loops, and logical operations in real-world programming.

Basic Boolean Examples

1. Declaring and Using Boolean Variables

#include <iostream>
using namespace std;

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

    cout << "Is coding fun? " << isCodingFun << endl;
    cout << "Is math hard? " << isMathHard << endl;

    return 0;
}

Output:

Is coding fun? 1  
Is math hard? 0  

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

2. Boolean with Comparison Operators

#include <iostream>
using namespace std;

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

    bool isEqual = (x == y);
    bool isLess = (x < y);

    cout << "x == y: " << isEqual << endl;
    cout << "x < y: " << isLess << endl;

    return 0;
}

Output:

x == y: 0  
x < y: 1  

Boolean in Conditional Statements

3. Using Boolean in if Statements

#include <iostream>
using namespace std;

int main() {
    bool isWeekend = true;

    if (isWeekend) {
        cout << "It's time to relax!" << endl;
    } else {
        cout << "It's a weekday, back to work!" << endl;
    }

    return 0;
}

Output:

It's time to relax!  

4. Combining Boolean Expressions with Logical Operators

#include <iostream>
using namespace std;

int main() {
    int age = 18;
    bool hasID = true;

    if (age >= 18 && hasID) {
        cout << "Access granted!" << endl;
    } else {
        cout << "Access denied!" << endl;
    }

    return 0;
}

Output:

Access granted!  

Boolean in Loops

5. Using a Boolean to Control a Loop

#include <iostream>
using namespace std;

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

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

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

    return 0;
}

Output:

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

6. Using Boolean Expressions in a for Loop

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        if (i % 2 == 0) {
            cout << i << " is even." << endl;
        } else {
            cout << i << " is odd." << endl;
        }
    }

    return 0;
}

Output:

0 is even.  
1 is odd.  
2 is even.  
3 is odd.  
4 is even.  

Boolean Functions

7. Returning a Boolean from a Function

#include <iostream>
using namespace std;

bool isEven(int number) {
    return number % 2 == 0;
}

int main() {
    int num = 7;

    if (isEven(num)) {
        cout << num << " is even." << endl;
    } else {
        cout << num << " is odd." << endl;
    }

    return 0;
}

Output:

7 is odd.  

8. Boolean for Simple Validations

#include <iostream>
using namespace std;

bool isValidAge(int age) {
    return (age >= 18 && age <= 100);
}

int main() {
    int age = 25;

    if (isValidAge(age)) {
        cout << "Valid age!" << endl;
    } else {
        cout << "Invalid age!" << endl;
    }

    return 0;
}

Output:

Valid age!  

Boolean with Arrays

9. Checking a Condition for Multiple Values

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    bool foundEven = false;

    for (int i = 0; i < 5; i++) {
        if (numbers[i] % 2 == 0) {
            foundEven = true;
            break;  // Exit loop once an even number is found
        }
    }

    if (foundEven) {
        cout << "There is an even number in the array." << endl;
    } else {
        cout << "No even numbers found." << endl;
    }

    return 0;
}

Output:

There is an even number in the array.  

10. Counting Conditions in an Array

#include <iostream>
using namespace std;

int main() {
    int numbers[] = {3, 5, 7, 10, 12};
    int evenCount = 0;

    for (int i = 0; i < 5; i++) {
        if (numbers[i] % 2 == 0) {
            evenCount++;
        }
    }

    cout << "Number of even numbers: " << evenCount << endl;

    return 0;
}

Output:

Number of even numbers: 2  

Boolean with Implicit Conversions

11. Non-Boolean to Boolean Conversion

#include <iostream>
using namespace std;

int main() {
    int x = 10;

    if (x) {  // Non-zero values are treated as true
        cout << "x is true!" << endl;
    }

    int y = 0;

    if (!y) {  // Zero is treated as false
        cout << "y is false!" << endl;
    }

    return 0;
}

Output:

x is true!  
y is false!  

Summary

  • Booleans play a critical role in decision-making and control flow.
  • They are used with comparison and logical operators, in loops, and for validations.
  • Boolean expressions simplify complex conditions and make code dynamic.

Explore More at The Coding College

Discover more tutorials on C++ and programming at The Coding College. Start building a solid foundation in coding today!

What’s Next?

  • Learn about control flow statements in C++.
  • Dive into functions and error handling with Boolean values.
  • Explore advanced logical operations in algorithms.

Leave a Comment