Loading...

C++ Comparison

The operators page ended with twenty-four lines on the screen. The last two were 2 and 7, the remainder of 17 divided by 5 and the box step after its shortcut. This page starts from that screen, in the same file types.cpp, and the first box still holds the word pine. The new lines at the end ask questions about numbers instead of changing them, so the screen grows to twenty-six lines.
This page covers the comparison operators: ==, !=, <, >, <=, and >=. Each one compares two values and answers a yes or no question. The screen already has a line of this kind, (count == 3), which has printed 0 since the booleans pages. Until now that line was only a result on the screen. Here it becomes the main subject, and the file gains two more questions beside it.
Programs ask questions like these all the time. Is the score high enough to pass? Is the password the same as the one stored on the disk? Is the timer still below its limit? Each answer decides what the program does next. A program that cannot ask a question cannot choose between two paths, so comparisons come before any decision the code makes.

A comparison produces a bool, which holds one of two values: true or false. The expression score >= goal asks whether score is greater than or equal to goal. When score is 72 and goal is 50, the answer is true. The expression score < goal asks whether score is less than goal, and the answer is false. Each of the six symbols asks a different question. == asks whether two values are equal, and != asks whether they differ. < asks whether the left value is smaller, and > asks whether it is larger. <= asks whether the left value is smaller or equal, and >= asks whether it is larger or equal.
The left side and the right side can be any values of compatible types. You can compare a box with a number, two boxes with each other, or the result of a calculation with a box. The expression 7 / 2 == 3 is true, because the whole-number division gives 3, and total % 5 == 2 is true for the box total from the previous page. The comparison does not change either side. It reads the values, asks its question, and gives one answer, and the boxes keep their values afterward.
Comparisons are most useful when they are written in terms of names that explain the purpose. A line such as score >= goal tells the reader what the program checks, while a line such as a >= b leaves the reader to guess. Choosing clear names is part of the work, even on a short page like this one.

cout prints a bool as a number. true prints 1 and false prints 0, which is why the screen shows numbers and not words. The earlier line (count > 1) printed 1 for the same reason, because 2 is greater than 1. The parentheses around each comparison in the code are not decoration. Without them, << binds more tightly than > or ==, so a line such as std::cout << count == 3 is read as (std::cout << count) == 3. That compares the output stream with 3, and the build stops with an error. Wrap every comparison in parentheses when you print it, the way the file already does.
The result of a comparison is also a value you can store. A line such as bool passed = score >= goal; creates a bool box named passed and stores true in it. Later lines can read passed the same way they read ready from the first pages. The box does not know the question that created it, so if score changes, passed keeps the old answer until a new comparison writes it again. This is why a stored answer should be read close to the place where it was made.
Do not confuse the comparison with the assignment from the previous page. The single = stores a value, and the double == only asks. Writing (score = goal) copies 50 into score and prints 50, and that changes the box. Writing (score == goal) only asks whether they are equal, and prints 0 because they are not.

Type the file in this order, or add only the new lines if types.cpp is already on disk. Under the line step += 4;, add two boxes: int score = 72; and int goal = 50;. At the end of the writing lines, under the line for step, add two comparison lines: one for (score >= goal) and one for (score < goal). The file name on disk stays types.cpp. The checked file is below, and the new lines are the two boxes and the two print lines at the end.

types.cpp adds two int boxes and two comparison lines to the twenty-four

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;
    double price = 7.9;
    const int limit = 10;
    int scores[3] = {4, 5, 6};
    std::string other = "cone";
    std::string joined = word + " " + other;
    int total = 17;
    int step = 3;
    step += 4;
    int score = 72;
    int goal = 50;
    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;
    std::cout << static_cast<int>(price) << std::endl;
    std::cout << static_cast<double>(count) / 4 << std::endl;
    std::cout << static_cast<int>(ratio * 3) << std::endl;
    std::cout << limit << std::endl;
    std::cout << limit * count << std::endl;
    std::cout << scores[0] << std::endl;
    std::cout << scores[2] << std::endl;
    std::cout << total % 5 << std::endl;
    std::cout << step << std::endl;
    std::cout << (score >= goal) << std::endl;
    std::cout << (score < goal) << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 types.cpp -o types, 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 twenty-six lines. The first twenty-four are the same as on the operators page. The twenty-fifth line is (score >= goal), which prints 1. The twenty-sixth line is (score < goal), which prints 0.
Check the twenty-fifth line first. The score of 72 is at least the goal of 50, so the comparison is true, and the screen shows 1. Then check the twenty-sixth line. 72 is not less than 50, so the comparison is false, and the screen shows 0. The table gives each row its piece, so compare your run with it line by line. If the screen has more than twenty-six lines, check that no print line was copied twice.

📊 Two comparisons and what they show

Piece What the screen shows
First 1, score >= goal is true because 72 is at least 50
Last 0, score < goal is false because 72 is not below 50
Rule a comparison gives true or false, and cout prints them as 1 or 0
Screen pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, 0, 7, 0.5, 1, 10, 20, 4, 6, 2, 7, 1, 0

Try one change at a time. Change the goal line to int goal = 80;, and the twenty-fifth line becomes 0, because 72 is not at least 80. The twenty-sixth line becomes 1, because 72 is below 80. Put the 50 back. Next, change (score < goal) to (score == goal), and the twenty-sixth line becomes 0, because the two values are not equal. Put the line back to (score < goal). Each experiment changes one thing, so the screen shows exactly which line caused the difference. Undo each change before the next one starts, so the file always returns to the checked version.
This page stops before combining comparisons. The next page is logical, which covers &&, ||, and !, the operators that join several yes or no answers into one. The line !ready has already printed 0 on this screen, and the logical page explains that operator in full. You leave with twenty-six lines on the screen, the comparison 1, and the comparison 0. Count the lines before you leave. There are twenty-six, 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