Loading...

C++ Booleans

This page keeps the twelve lines from the numbers page and adds three lines under them. That page ended with pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, and 3.5. That twelve-line screen is the step this page builds on. The first box still holds the word pine, and the comment // the word stays pine still sits above the declarations.
This module stays on the same file. types.cpp keeps its name, its boxes, and its build command, so the example changes only by three new writing lines. The new lines ask questions instead of calculating. Each question has a yes or no answer, and C++ stores that answer in a bool, the same type as the ready box. This page is about how comparisons create bool values and how cout prints them.
Yes-or-no questions are everywhere in programs. Is the password long enough, is the cart empty, is the file missing, is the score above the high score. Each one produces a bool, and the decisions in the next modules depend on those values. This page prepares them, so the later if statements have something to read.

A comparison operator asks a question about two values and gives back a bool. The greater-than operator, written >, asks whether the left value is bigger than the right. The expression count > 1 asks whether 2 is bigger than 1. The answer is yes, so the result is true. The equal-to operator is two equals signs, written ==, and it asks whether two values are the same. The expression count == 3 asks whether 2 is 3. The answer is no, so the result is false.
The other comparison operators follow the same pattern. Less-than, written <, asks whether the left value is smaller. Greater-or-equal, written >=, accepts equal values too. Not-equal, written !=, is true when the two values differ. Each two-character operator is one operator, written with no space between its characters, so >= and != are not two separate symbols. Comparisons also work on double values, so 0.5 > 1 asks whether one half is bigger than one, and the answer is false. Every comparison reads the boxes as they stand at that line, and none of them changes count.

A bool holds true or false, but cout prints it as 1 for true and 0 for false. This is the same as the ready box on the types page, which printed 1 on the screen. The default output does not print the words true or false. The std::boolalpha setting changes that output, and this page leaves it off so the screen stays consistent with the earlier lines.
The parentheses matter. The line std::cout << count > 1 looks like a question, but the << operator binds tighter than >, so the compiler reads the line in a different way and stops with an error. The comparison must be wrapped in parentheses: std::cout << (count > 1). Keep that rule in mind whenever a comparison sits inside a cout line. The parentheses tell the compiler that the question is asked first and the answer is printed second.
The not operator, written with an exclamation mark, flips a bool. The expression !ready reads the ready box, which holds true, and gives false, so the line prints 0. The box ready does not change, because the not operator only produces a new value. Applying ! twice returns the original value, so the not operator acts as its own undo. Also watch the difference between one equals sign and two. The line count = 3 stores 3 in count, while count == 3 asks a question and changes nothing. The first is an assignment, and the second is a comparison. Mixing them up is a common early mistake, and the screen shows the difference at once.

Type the file in this order, or add only the three new lines if types.cpp is already on disk. Keep the twelve lines from the numbers page. Under the 3.5 writing line, add three lines: one for count > 1, one for count == 3, and one for !ready. The checked file is below. The file name on disk stays types.cpp.

types.cpp adds three comparison lines under the twelve

cpp
#include <iostream>
#include <string>

int main()
{
    // the word stays pine
    std::string word = "pine";
    int count = 2;
    double ratio = 0.5;
    bool ready = true;
    std::string other = "cone";
    std::string joined = word + " " + other;
    std::cout << word << std::endl;
    std::cout << count << std::endl;
    std::cout << ratio << std::endl;
    std::cout << ready << std::endl;
    std::cout << word.size() << std::endl;
    std::cout << joined << std::endl;
    std::cout << word[0] << std::endl;
    std::cout << joined.find("cone") << std::endl;
    std::cout << joined.substr(5, 4) << std::endl;
    std::cout << count * 4 << std::endl;
    std::cout << 7 / 2 << std::endl;
    std::cout << 7.0 / 2 << std::endl;
    std::cout << (count > 1) << std::endl;
    std::cout << (count == 3) << std::endl;
    std::cout << !ready << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 types.cpp -o types and then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows fifteen lines: pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, and 0. The first twelve are the same as on the numbers page. The thirteenth line is the comparison count > 1, which is true and prints 1. The fourteenth line is count == 3, which is false and prints 0. The fifteenth line is !ready, which flips true to false and prints 0.
Check the thirteenth line first. The box count holds 2, so the question is answered yes. Then check the fourteenth line. The box holds 2, not 3, so the answer is no. The fifteenth line flips the value of ready. The table gives each row its piece, so compare your run with it line by line. If the screen has more than fifteen lines, check that no writing line was copied twice.

📊 Three comparison lines and what they show

Piece What the screen shows
Greater 1, count > 1 is true
Equal 0, count == 3 is false
Not 0, !ready flips true to false
Screen pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, 0

Each change should match a screen line that you can predict before you rebuild. If a line surprises you, compare the question with the answer before you change anything else, because the question is the part that decides the output. Change the number in the greater-than line to count > 2, then rebuild. The thirteenth line becomes 0, because 2 is not bigger than 2. Put the 1 back. Next, change count == 3 to count == 2. The fourteenth line becomes 1. Put the original 3 back. Then delete the parentheses around count > 1 and rebuild. The compiler stops with an error, which shows the rule from the middle of the page. Put the parentheses back before you leave.
The next page is casting. Its job is to convert a value from one type to another on purpose, with static_cast, and to see what happens to the fraction when a double becomes an int. This page stops before any conversion. You leave with fifteen lines on the screen, the three bool results 1, 0, and 0, and the box ready still holding true. Count the lines before you leave. There are fifteen, and the last one is 0.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top