Skip to content

4.9 — Boolean values ​


🎯 Mục Tiêu Bài Học ​

  • Nắm vững bản chất lý thuyết và cơ chế hoạt động của Boolean values trong tiêu chuẩn ISO C++.
  • Hiểu rõ cách máy tính tổ chức bộ nhớ (RAM, CPU registers) khi thực thi mã nguồn liên quan.
  • Nhận biết các cạm bẫy lỗi kinh điển (Undefined Behavior, Memory Leak, Bug logic) và cách phòng tránh.
  • Áp dụng các quy tắc thực hành tốt nhất (Best Practices) chuẩn công nghiệp và cập nhật C++20/C++23 hiện đại.

4.9 — Boolean values ​

In real-life, it’s common to ask or be asked questions that can be answered with “yes” or “no”. “Is an apple a fruit?” Yes. “Do you like asparagus?” No.

Now consider a similar câu lệnh that can be answered with a “true” or “false”: “Apples are a fruit”. It’s clearly true. Or how about, “I like asparagus”. Absolutely false (yuck!).

These kinds of sentences that have only two possible outcomes: yes/true, or no/false are so common, that many programming languages include a special type for dealing with them. That type is called a Boolean type (note: Boolean is properly capitalized in the English language because it’s named after its inventor, George Boole).

Boolean các biến

Boolean các biến are các biến that can have only two possible values: true, and false.

To declare a Boolean biến, we use the keyword bool.

cpp
bool b;

To khởi tạo or assign a true or false value to a Boolean biến, we use the keywords true and false.

cpp
bool b1 { true };
bool b2 { false };
b1 = false;
bool b3 {}; // default initialize to false

Just as the unary minus toán tử (-) can be used to make an integer negative, the logical NOT toán tử (!) can be used to flip a Boolean value from true to false, or false to true:

cpp
bool b1 { !true }; // b1 will be initialized with the value false
bool b2 { !false }; // b2 will be initialized with the value true

Boolean values are not actually stored in Boolean các biến as the words “true” or “false”. Instead, they are stored as integral values: true is stored as integer 1, and false is stored as integer 0. Similarly, when Boolean values are evaluated, they don’t actually evaluate to “true” or “false”. They evaluate to the integers 0 (false) or 1 (true). Because Booleans store integral values, they are considered to be an integral type.

Printing Boolean values

When we print Boolean values, std::cout prints 0 for false, and 1 for true:

cpp
#include <iostream>

int main()
{
    std::cout << true << '\n'; // true evaluates to 1
    std::cout << !true << '\n'; // !true evaluates to 0

    bool b {false};
    std::cout << b << '\n'; // b is false, which evaluates to 0
    std::cout << !b << '\n'; // !b is true, which evaluates to 1
    return 0;
}

Outputs:

cpp
1
0
0
1

Use std::boolalpha to print true or false

If you want std::cout to print true or false instead of 0 or 1, you can output std::boolalpha. This doesn’t output anything, but manipulates the way std::cout outputs bool values.

Here’s an example:

cpp
#include <iostream>

int main()
{
    std::cout << true << '\n';
    std::cout << false << '\n';

    std::cout << std::boolalpha; // print bools as true or false

    std::cout << true << '\n';
    std::cout << false << '\n';
    return 0;
}

This prints:

cpp
1
0
true
false

You can use std::noboolalpha to turn it back off.

Integer to Boolean conversion

When using uniform sự khởi tạo, you can khởi tạo a biến using integer các hằng giá trị (literals) 0 (for false) and 1 (for true) (but you really should be using false and true instead). Other integer các hằng giá trị (literals) cause quá trình biên dịch errors:

cpp
#include <iostream>

int main()
{
	bool bFalse { 0 }; // okay: initialized to false
	bool bTrue  { 1 }; // okay: initialized to true
	bool bNo    { 2 }; // error: narrowing conversions disallowed

	std::cout << bFalse << bTrue << bNo << '\n';
	
	return 0;
}

However, in any context where an integer can be converted to a Boolean, the integer 0 is converted to false, and any other integer is converted to true.

cpp
#include <iostream>

int main()
{
	std::cout << std::boolalpha; // print bools as true or false

	bool b1 = 4 ; // copy initialization allows implicit conversion from int to bool
	std::cout << b1 << '\n';

	bool b2 = 0 ; // copy initialization allows implicit conversion from int to bool
	std::cout << b2 << '\n';

	return 0;
}

This prints:

cpp
true
false

Note: bool b1 = 4; may generate a cảnh báo. If so you’ll have to disable treating warnings as errors to compile the example.

Inputting Boolean values

Inputting Boolean values using std::cin sometimes trips new programmers up.

Consider the following program:

cpp
#include <iostream>

int main()
{
	bool b{}; // default initialize to false
	std::cout << "Enter a boolean value: ";
	std::cin >> b;
	std::cout << "You entered: " << b << '\n';

	return 0;
}
cpp
Enter a Boolean value: true
You entered: 0

Wait, what?

By default, std::cin only accepts numeric input for Boolean các biến: 0 is false, and 1 is true. Any other numeric value will be interpreted as true, and will cause std::cin to enter failure mode. Any non-numeric value will be interpreted as false and will cause std::cin to enter failure mode.

Related content

We discuss failure mode (and how to get out of it) in lesson 9.5 -- std::cin and handling invalid input.

In this case, because we entered true, std::cin silently failed and set b to false. Consequently, when std::cout prints a value for b, it prints 0.

To allow std::cin to accept the words false and true as inputs, you must first input to std::boolalpha:

cpp
#include <iostream>

int main()
{
	bool b{};
	std::cout << "Enter a boolean value: ";

	// Allow the user to input 'true' or 'false' for boolean values
	// This is case-sensitive, so True or TRUE will not work
	std::cin >> std::boolalpha;
	std::cin >> b;

	// Let's also output bool values as `true` or `false`
	std::cout << std::boolalpha;
	std::cout << "You entered: " << b << '\n';

	return 0;
}

However, when std::boolalpha is enabled for input, numeric values will no longer be accepted (they evaluate to false and cause std::cin to enter failure mode).

Cảnh báo nguy hiểm

Enabling std::boolalpha for input will only allow lower-cased false or true to be accepted. Variations with capital letters will not be accepted. 0 and 1 will also no longer be accepted.

Note that we use std::cin >> std::boolalpha; to input bool values as true or false, and std::cout << std::boolalpha; to output bool values as true or false. These are independent controls that can be turned on (using std::boolalpha) or off (using std::noboolalpha) separately.

Boolean các giá trị trả về

Boolean values are often used as the các giá trị trả về for các hàm that check whether something is true or not. Such các hàm are typically named starting with the word is (e.g. isEqual) or has (e.g. hasCommonDivisor).

Consider the following example, which is quite similar to the above:

cpp
#include <iostream>

// returns true if x and y are equal, false otherwise
bool isEqual(int x, int y)
{
    return x == y; // operator== returns true if x equals y, and false otherwise
}

int main()
{
    std::cout << "Enter an integer: ";
    int x{};
    std::cin >> x;

    std::cout << "Enter another integer: ";
    int y{};
    std::cin >> y;

    std::cout << std::boolalpha; // print bools as true or false
    
    std::cout << x << " and " << y << " are equal? ";
    std::cout << isEqual(x, y) << '\n'; // will return true or false

    return 0;
}

Here’s output from two runs of this program:

cpp
Enter an integer: 5
Enter another integer: 5
5 and 5 are equal? true
cpp
Enter an integer: 6
Enter another integer: 4
6 and 4 are equal? false

How does this work? First we read in integer values for x and y. Next, the biểu thức isEqual(x, y) is evaluated. In the first run, this results in a hàm call to isEqual(5, 5). Inside that hàm, 5 == 5 is evaluated, producing the value true. The value true is returned back to the caller to be printed by std::cout. In the second run, the call to isEqual(6, 4) returns the value false.

Boolean values take a little bit of getting used to, but once you get your mind wrapped around them, they’re quite refreshing in their simplicity! Boolean values are also a huge part of the language -- you’ll end up using them more than all the other các kiểu dữ liệu cơ bản put together!

We’ll continue our exploration of Boolean values in the next lesson.


💡 Tóm Tắt Quy Tắc Vàng (Core Guidelines)

  1. Luôn hiểu rõ vòng đời và quyền sở hữu (Ownership) của dữ liệu trước khi viết code.
  2. Ưu tiên khởi tạo đồng nhất với dấu ngoặc nhọn {} để ngăn chặn lỗi thu hẹp kiểu dữ liệu (Narrowing conversions).
  3. Bật toàn bộ cờ cảnh báo -Wall -Wextra -Wpedantic để compiler giúp bạn phát hiện bug từ sớm!