← 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.
Syntax
cpp
bool variable_name = true;  // or false
std::cout << std::boolalpha << variable_name;

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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare isReady and set it to true
	bool isReady = true;
	// Declare isDone and set it to false
	bool isDone = false;
	// Print to the console with cout
	std::cout << isReady << " " << isDone << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare hasStock and set it to true
	bool hasStock = true;
	// Check whether hasStock
	if (hasStock) {
		// Print to the console with cout
		std::cout << "In stock" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting cout << true to print the word true, when it prints 1 unless you use boolalpha.
  2. Writing if (x = 5) instead of x == 5, which assigns 5 and is always true.
  3. Assuming any non-zero number is not valid as a bool, when any non-zero value converts to true and only 0 is false.

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.