Booleans are the simplest data type in C++ — they can only be true or
false. But do not let that simplicity fool you. Booleans are the foundation
of decision-making in programming. Every if-statement, every loop condition, every
logical check comes down to a boolean value.
Comparison Operators
You create boolean values using comparison operators:
==— equal to!=— not equal to>— greater than<— less than>=— greater than or equal to<=— less than or equal to
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
cout << "x == y: " << (x == y) << endl;
cout << "x != y: " << (x != y) << endl;
cout << "x > y: " << (x > y) << endl;
cout << "x < y: " << (x < y) << endl;
bool isAdult = true;
bool isStudent = false;
cout << "Adult: " << isAdult << " Student: " << isStudent;
return 0;
}
When you print a boolean with cout, it shows 1 for true
and 0 for false. That is just how C++ displays them. Internally, true
is stored as 1 and false as 0. You can actually use them in math — but do not do that.
It makes your code confusing.