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.
- true (typically representing a condition that is correct or valid).
- 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:
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:
C++ Boolean – Interview Questions
Q 1: What is Boolean in C++?
Q 2: Which keyword represents Boolean?
Q 3: What value does true represent?
Q 4: What value does false represent?
Q 5: Where are Boolean values used?
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++?