Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
An operator tells C++ to perform an action, and its operands are the values that the action uses. In points + bonus, + is the operator, while points and bonus are its operands. The complete combination is an expression because C++ can evaluate it and produce a value.
int points = 17;
int bonus = 5;
int players = 4;
points + bonus; // produces 22
points - bonus; // produces 12Evaluating points + bonus produces 22, and evaluating points - bonus produces 12. Neither expression changes points or bonus. After both expressions are evaluated, points is still 17 and bonus is still 5. Calculating a value and storing a value are separate actions in C++.
The slash operator performs division, but the operand types determine the kind of result. Since points and players are both int variables, points / players performs integer division. With the running values, 17 / 4 produces 4, because the fractional part is discarded rather than stored in an integer result.
points / players; // 17 / 4 produces 4
points % players; // 17 % 4 produces 1The percent operator calculates the remainder after integer division. Since four fits into 17 four complete times, the quotient is 4 and the leftover amount is 1, so points % players produces 1. Division and remainder by zero have no valid result. In C++, evaluating either operation with a zero divisor causes undefined behavior.
When points is 17 and players is 4, what are the results of points / players and points % players?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
C++ does not evaluate every arithmetic expression strictly from left to right. Some operators have higher precedence, so they are grouped first. In points + bonus * players, multiplication has higher precedence than addition. Substituting the running values gives 17 + 5 * 4, and C++ evaluates 5 * 4 before adding its result to 17.
points + bonus * players; // 17 + 5 * 4, then 17 + 20, so 37
(points + bonus) * players; // 22 * 4, so 88The unparenthesized expression produces 37, not 88, because multiplication supplies 20 to the addition. Parentheses create a different grouping: points + bonus is evaluated first, producing 22, and that result is multiplied by players to produce 88. Use parentheses when the grouping you want is different from the precedence C++ already applies.
Add parentheses to points + bonus * players so that addition happens first and the result is 88.
points + bonus * playersCheckpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The expression points + bonus calculates 22 but leaves points unchanged at 17. The assignment operator stores a value in the variable on its left, so points = points + bonus first calculates 22 and then stores 22 in points. After that statement finishes, points is 22.
int points = 17;
int bonus = 5;
points + bonus; // calculates 22, points remains 17
points = points + bonus; // calculates 22, then points becomes 22The compound assignment points += bonus is a shorter form of the same update. With points equal to 17 and bonus equal to 5, it adds bonus to the current value of points and stores 22 back in points. The distinction is the same: points + bonus only calculates, while points += bonus changes the stored variable.
int points = 17;
int bonus = 5;
points += bonus; // equivalent update: points = points + bonus
// points is now 22