C++ Operators
The arrays page ended with twenty-two lines on the screen, and the last two were 4 and 6, the values in scores[0] and scores[2]. This page starts from that screen. The first box still holds the word pine, the array scores is still there, and the comment // the word stays pine still sits above the declarations. The new work adds lines at the end, so the screen grows to twenty-four.
This page starts the Operators module. It stays in types.cpp, so every line on the screen still comes from one file that you can follow from top to bottom. Operators are the symbols that tell the computer what to do with values. A plus sign adds, a minus sign subtracts, an asterisk multiplies, and a slash divides. Several of them already appear on the screen, such as count * 4 and 7 / 2. This page names the family again, then adds two operators that have not appeared yet: the remainder operator % and the compound assignment +=.
Programs use operators for almost every calculation. A game adds points to a score, a timer splits seconds into minutes, and a shop multiplies a price by a count of items. Each of those steps needs the right operator, and a wrong one often gives a number that looks plausible but is still wrong. Learning what each symbol does with whole numbers is the first step toward reading and checking those calculations.
Arithmetic operators follow the type of the values they work on. When both sides of / are int values, the result is also an int, so C++ drops the part after the decimal point. That is why 7 / 2 prints 3, not 3.5. The line 7.0 / 2 prints 3.5, because 7.0 is a double, and a double keeps the fraction. The same rule applies to any division of two whole numbers, so 17 divided by 5 gives 3, not 3.4.
The remainder operator % answers the question that division throws away. It gives what is left over after the whole-number division. Seventeen contains three groups of five, with two left over, so 17 % 5 gives 2. Programs use this often. A number that leaves remainder 0 when divided by 2 is even, and a clock uses the remainder to wrap minutes past sixty back to zero. The operator works only on whole numbers. A line such as 7.5 % 2 stops the build, because the compiler refuses % when either side is a double.
The remainder keeps the same sign rules as division for the positive numbers used on this page, so the examples here stay simple. You only need to remember one idea for now: a % b is what remains after a is split into as many whole groups of b as possible.
The compound assignment += is a shortcut with a precise meaning. The line step += 4; reads the value in the box step, adds 4 to it, and stores the sum back in the same box. Written out in full, it is step = step + 4;. The shortcut matters when the name is long or the expression on the right is long, because the reader sees the change in one place. The other compound forms follow the same pattern: -= subtracts, *= multiplies, /= divides, and %= keeps the remainder.
Do not confuse += with = and ==. A single equals sign assigns a value. The line int step = 3; creates the box and puts 3 into it. A plus-equals changes a box that already exists. A double equals sign asks a question and gives a true or false answer, which this page only prints and does not study. In the new lines, the box step begins at 3, then step += 4; changes it to 7, and the screen prints 7 because the print line comes after the change.
The order of lines matters here. A declaration must come before the line that changes the box, and both must come before any print line that reads it. The compiler reads the file from top to bottom, so a name that has not been declared yet stops the build, and a change that comes after the print shows the old value on the screen.
Type the file in this order, or add only the new lines if types.cpp is already on disk. Under the line std::string joined = word + " " + other;, add the two declarations int total = 17; and int step = 3;, then the shortcut line step += 4;. At the end of the writing lines, under the line for scores[2], add two more lines: one for total % 5 and one for step.
Write the new lines in the same style as the rest of the file. Each print line ends with std::endl, and each name is spelled exactly as it was declared. The file name on disk stays types.cpp. The checked file is below, and the new lines are the two declarations, the shortcut line, and the two print lines at the end.
#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;
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;
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-four lines. The first twenty-two are the same as on the arrays page. The twenty-third line is total % 5, which prints 2. The twenty-fourth line is step, which prints 7.
Check the twenty-third line first. Seventeen divided by five leaves two behind, and the screen shows 2. Then check the twenty-fourth line. The box starts at 3, the shortcut adds 4, and the screen shows 7, not 3 and not 4. The table gives each row its piece, so compare your run with it line by line. If the screen has more than twenty-four lines, check that no print line was copied twice.
📊 Two new lines and what they show
| Piece | What the screen shows |
|---|---|
| First | 2, total % 5 leaves the remainder 2 of 17 |
| Last | 7, step starts at 3 and += 4 makes it 7 |
| Rule | += adds to the box and stores the sum back in it |
| 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 |
Try one change at a time. Replace total % 5 with total % 4, and the twenty-third line becomes 1, because seventeen holds four groups of four with one left over. Put the 5 back. Next, change step += 4; to step *= 2;, and the twenty-fourth line becomes 6, because three times two is six. Put the line back to step += 4;. 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 comparisons. The next page is comparison, which covers ==, !=, <, and >. The line (count == 3) already prints 0 on this screen, and the next page explains that answer. You leave with twenty-four lines on the screen, the remainder 2, and the step value 7. Count the lines before you leave. There are twenty-four, and the last one is 7.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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