C++ Boolean

In C++, a Boolean is a data type that represents logical values. It can hold one of two possible values:

A Boolean, bool, can have one of the two values true or false. A Boolean is used to express the results of logical operations.

  1. true (typically representing a condition that is correct or valid).
  2. false (typically representing a condition that is incorrect or invalid).

The bool type in C++ is used to store these values, and it’s commonly used in decision-making structures like if statements or loops, where the program needs to evaluate conditions as either true.

Example: Use Boolean value true


#include <iostream>
using namespace std;

int main() {
    bool isManager = true; 
    if (isManager) {
        cout << "John is Manager." << endl;
    } else {
        cout << "John is Developer." << endl;
    }

    return 0;
}

Output:

John is Manager.

Example: Use Boolean value false


#include <iostream>
using namespace std;

int main() {
    bool isEmployeeActive = false; 
    if (isEmployeeActive) {
        cout << "Employee is active." << endl;
    } else {
        cout << "Employee is not active." << endl;
    }

    return 0;
}

Output:

Employee is not active.

C++ Boolean – Interview Questions

Q 1: What is Boolean in C++?
Ans: A data type that stores true or false.
Q 2: Which keyword represents Boolean?
Ans: bool
Q 3: What value does true represent?
Ans: Non-zero (usually 1).
Q 4: What value does false represent?
Ans: 0.
Q 5: Where are Boolean values used?
Ans: Conditions and decision-making statements.

C++ Boolean – Objective Questions (MCQs)

Q1. What are the two possible values of a Boolean variable in C++?






Q2. What is the output of the following code?

bool a = true;
bool b = false;
cout << a + b;






Q3. Which header file is required to use Boolean data type in C++?






Q4. What will be the output of the following code?

bool x = (5 > 10);
cout << x;






Q5. Which of the following statements is correct about Boolean type in C++?






Related C++ Boolean Topics