Expressions, arithmetic, and deliberate conversions
An expression combines values and operators to produce a new value. Before relying on a calculation, predict its type and result—especially when dividing whole numbers.
Learning objectives
- Evaluate simple arithmetic expressions using
+,-,*,/, and%. - Predict integer division and remainder.
- Use parentheses to state grouping clearly.
- Make one numeric conversion explicit when a fractional result is needed.
Prerequisites: know how to declare and initialize simple variables, and recognize the basic types int and double. Review Variables and data types if needed.
What is an expression?
An expression is a piece of code that computes a value. 7 + 2, pages_read * 2, and temperature / 3.0 are expressions. An operator is the symbol that combines or transforms values. The values around a binary operator are its operands.
Think of an expression as a small calculation on paper. C++ needs the same information: which values to use, which operation to apply, and how to group the operations.
One program, several calculations
#include <iostream>
int main() {
int notebooks{7};
int whole_shares{notebooks / 2};
int leftover_notebooks{notebooks % 2};
double exact_share{static_cast<double>(notebooks) / 2};
int first_result{4 + 3 * 2};
int grouped_result{(4 + 3) * 2};
std::cout << "Whole shares: " << whole_shares << '\n';
std::cout << "Left over: " << leftover_notebooks << '\n';
std::cout << "Exact share: " << exact_share << '\n';
std::cout << "4 + 3 * 2: " << first_result << '\n';
std::cout << "(4 + 3) * 2: " << grouped_result << '\n';
}Code, line by line
notebooksstarts at 7.notebooks / 2uses integer division because both operands are integers. The fractional part is discarded, sowhole_sharesbecomes 3.%is the remainder operator for integer operands. Seven divided by two leaves remainder 1.static_cast<double>(notebooks)explicitly converts the value 7 to adoublefor this calculation. Dividing by 2 then produces 3.5, stored inexact_share.- Multiplication binds more tightly than addition, so
4 + 3 * 2is 4 + 6, or 10. - Parentheses group the addition first, so
(4 + 3) * 2is 7 * 2, or 14. - The output statements show each result with an explanatory label.
Integer division is a design choice
Integer division answers “how many complete groups fit?” Seven notebooks shared two at a time produce three complete pairs. It does not answer “what is the mathematical fraction?” When the situation needs a fractional result, ensure an appropriate floating-point value participates in the division. An explicit static_cast<double> makes that decision visible to the reader.
7 / 2 → 3 complete groups 7 % 2 → 1 remainder 7.0 / 2 → 3.5 fractional quotient
What happens internally
When the program evaluates an initializer, it evaluates the expression on the right and uses that result to initialize the variable on the left. Arithmetic operators have rules for operand types and conversions. The compiler can determine much of this from the declared types. At run time, target-specific instructions perform the selected arithmetic, but the source-level rules determine the values this lesson traces.
Common beginner mistakes
- Expecting
5 / 2to be 2.5: with two integers, the result is integer division: 2. - Using
%with floating-point values: this operator is for integer operands. A different library function is used for floating-point remainder. - Relying on remembered precedence: add parentheses when grouping matters to the reader or to avoid a mistake.
- Converting after division:
static_cast<double>(7 / 2)converts the already-truncated result 3. Convert an operand before dividing. - Using floating point for exact money without a policy: many decimal fractions are not exact in binary floating point. Real money programs often use integer minor units or a chosen decimal strategy.
Practical exercises
- For 17 students placed in groups of 4, calculate complete groups and students left over. Verify with
/and%. - Predict and test
18 / 5,18 % 5, and18.0 / 5. - Write expressions for the perimeter and area of a rectangle with width 6 and height 4. Add parentheses wherever they help your future self read the formula.
- Explain why
static_cast<double>(9 / 4)differs fromstatic_cast<double>(9) / 4. - Change the notebook count to 9 and trace every displayed result before compiling.
Assessment: check your understanding
1. What is the value of 11 / 3 when both operands are int?
Answer: 3. Why: Integer division produces the whole-number quotient and discards the fractional part.
2. What is the value of 11 % 3?
Answer: 2. Why: 3 fits into 11 three times, leaving 2.
3. Why does 4 + 3 * 2 produce 10?
Answer: Multiplication is grouped before addition in this expression. Why: It becomes 4 + 6.
4. How can you obtain 3.5 from two integer variables holding 7 and 2?
Answer: Convert one operand before division, for example static_cast<double>(seven) / two. Why: The division then uses floating-point arithmetic instead of truncating an integer quotient first.
5. Why are parentheses useful even when you know precedence rules?
Answer: They make intended grouping explicit to both the compiler and a reader. Why: Clear grouping reduces mistakes during later edits.
Revision summary
Expressions calculate values. Arithmetic is governed by operator rules and operand types: integer division gives a whole-number quotient, % gives a remainder, and parentheses make grouping explicit. Convert deliberately before an operation when a fractional result is intended. The next lesson uses expressions that produce true or false to let programs choose a path.