Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
The bool type holds one of exactly two logical values: true or false. In the concert-entry example, hasTicket and hasAdultPermission are both bool variables because each answer is either yes or no. The word true in the code is a bool literal. It is not text containing the characters t, r, u, e, and it is not the integer 1, even though C++ can display a true bool as 1.
bool hasTicket = true;
bool hasAdultPermission = true;The quotes change the meaning completely. A value written as "true" is text, while true without quotes is a bool literal. The integer 1 is also a different value with a different type. Choose bool when the value represents a yes-or-no fact, so the type records what the value means as well as what value it holds.
What value and type does hasAdultPermission = true represent?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A comparison is an expression with a value, and that value is a bool. Here age is 16 and adultAge is 18, so age >= adultAge asks whether 16 is at least 18. The answer is false. You can store that answer in isAdult, just as you store any other value in a variable.
int age = 16;
int adultAge = 18;
bool isAdult = age >= adultAge;After the assignment, isAdult contains false. The comparison has not merely performed a test and disappeared. It has produced a reusable bool value that another expression can read later. This lets you give a name to a fact, such as whether the visitor is an adult, before combining that fact with other facts.
Logical operators take bool values and produce another bool value. First evaluate the parentheses in hasTicket && (isAdult || hasAdultPermission). With isAdult equal to false and hasAdultPermission equal to true, the OR expression becomes false || true, which produces true. Then hasTicket is combined with that result. Since hasTicket is true, the final AND expression is true && true, which produces true.
bool entryApproved = hasTicket && (isAdult || hasAdultPermission);The parentheses show the intended intermediate value directly. The visitor is not an adult, but has adult permission, so the parenthesized expression is true. The visitor also has a ticket, so entryApproved becomes true. Every stage still has a bool result that can be stored or consumed by the next operator.
For the concert-entry values, what is the final value of entryApproved?
bool entryApproved = hasTicket && (isAdult || hasAdultPermission);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
C++ uses 1 and 0 as the default output forms for true and false. Therefore printing entryApproved normally displays 1 for this example. That output is only a presentation choice. entryApproved still has type bool and still stores the logical value true. A printed 1 does not prove that the variable is an int.
cout << entryApproved << '\n';
cout << boolalpha << entryApproved << '\n';The stream manipulator boolalpha changes how bool values are displayed, not how they are stored. The first line prints 1 using the default form. After boolalpha is enabled, the second line prints true. Both lines read the same bool variable, so storage, type, logical value, and presentation remain separate ideas.