← Back to C++ Course | Chapter 4: Control Flow | Lesson 1 of 13

C++ Booleans

The bool type stores one of exactly two values, true or false, and drives the decisions made by every conditional statement and loop in a C++ program.
Syntax
cpp
bool variable_name = true;  // or false
bool variable_name = expression;  // e.g. x > 5

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, used whenever a piece of information is a simple yes-or-no flag.

Example: The bool Type

cpp
// Include std::cout and std::cin
#include <iostream>

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

// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// Declare result and set it to x > 5
	bool result = x > 5;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Booleans 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: Booleans in Conditions

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare isOpen and set it to true
	bool isOpen = true;
	// Check whether isOpen
	if (isOpen) {
		// Print to the console with cout
		std::cout << "Store is open" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Boolean Variables from Comparisons

Storing the result of a comparison in a named bool variable, rather than repeating the comparison inline, makes conditions easier to read and lets the same computed result be reused in multiple places.

Example: Boolean Variables from Comparisons

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 70
	int score = 70;
	bool passed = score >= 60;
	// Check whether passed
	if (passed) {
		// Print to the console with cout
		std::cout << "Passed" << std::endl;
	}
	// Print to the console with cout
	std::cout << passed << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing Booleans with boolalpha

By default cout prints a bool as 1 or 0, but the boolalpha stream manipulator makes it print the words true and false instead, which can be more readable in output.

Example: Printing Booleans with boolalpha

cpp
#include <iostream>

int main() {
	bool active = true;
	std::cout << active << std::endl; // 1
	std::cout << std::boolalpha << active << std::endl; // true
	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. Printing a bool and expecting true, when cout prints 1 unless you use boolalpha.
  2. Reading a bool with cin and typing true, which fails; without boolalpha it expects 0 or 1.
  3. Assuming only 1 is true, when any non-zero value is true.

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.