← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 11 of 15

C++ bool Data Type

The bool type stores exactly one of two values, true or false, internally represented as 1 or 0, and is what every condition in an if, while, or for loop ultimately evaluates to.

The bool Type

The bool type represents exactly one of two possible values, true or false, and is C++'s dedicated type for logical values, unlike C which historically used plain integers for the same purpose.

Example: The bool Type

cpp
#include <iostream>

int main() {
	bool isReady = true;
	bool isDone = false;
	std::cout << isReady << " " << isDone << std::endl;
	return 0;
}

bool Prints as 1 or 0

By default, cout prints a bool value as the integer 1 for true or 0 for false, since bool is internally stored as a small integer, though boolalpha can be used to print the words instead.

Example: bool Prints as 1 or 0

cpp
#include <iostream>

int main() {
	bool active = true;
	std::cout << active << std::endl; // prints 1
	std::cout << std::boolalpha << active << std::endl; // prints true
	return 0;
}

Boolean Expressions

A boolean expression is any expression that evaluates to true or false, most commonly formed by comparing two values with operators like >, <, ==, or != rather than by writing a literal true or false directly.

Example: Boolean Expressions

cpp
#include <iostream>

int main() {
	int score = 85;
	bool passed = score >= 60; // comparison, not a literal true/false
	std::cout << passed << std::endl;
	return 0;
}

bool in Conditions

Boolean values are what control flow statements like if, while, and for actually test: an if statement runs its block only when the boolean expression inside its parentheses evaluates to true.

Example: bool in Conditions

cpp
#include <iostream>

int main() {
	bool hasStock = true;
	if (hasStock) {
		std::cout << "In stock" << std::endl;
	}
	return 0;
}

Implicit Conversion to bool

In C++, any non-bool value used where a bool is expected is implicitly converted: zero converts to false and any non-zero numeric value converts to true, which is why an if statement can test a plain int directly.

Example: Implicit Conversion to bool

cpp
#include <iostream>

int main() {
	int value = 5;
	if (value) { // non-zero converts to true
		std::cout << "Treated as true" << std::endl;
	}
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.