C++ Operators, Arithmetic, and Conversions
Calculate with integer and floating-point values, understand precedence and division, and make conversions visible when they can change meaning.
Before this lesson
Predict arithmetic results from operand types
Use comparison and logical operators correctly
Identify narrowing overflow and mixed-type risks
The short answer
Operators produce results according to operand types. Integer division drops the fractional remainder, overflow and narrowing can invalidate results, and parentheses or explicit conversions should make important intent visible.
Operand types control arithmetic
The same operator can produce different behavior for different operand types. With two integers, division produces an integer quotient. If either operand is floating point, floating-point division occurs. That means assigning the final result to double is too late if the division already happened as integers.
Multiplication can overflow before assignment to a wider destination for the same reason. Promote an operand before the operation when the wider range is required. Units matter too: adding seconds to bytes is type-correct at the language level but wrong for the domain.
#include <iostream>
int main()
{
int total{17};
int count{4};
double wrong{total / count};
double correct{static_cast<double>(total) / count};
std::cout << "Integer first: " << wrong << '\n';
std::cout << "Converted first: " << correct << '\n';
}Expected output
Integer first: 4 Converted first: 4.25
Precedence is not a design tool
Multiplication and division bind more tightly than addition and subtraction, so subtotal + tax * quantity does not group like (subtotal + tax) * quantity. Parentheses document the intended formula and protect it during later edits.
Compound assignment such as total += price updates the left operand. Increment is convenient for counters, but avoid putting several increments inside one expression. A separate statement makes evaluation and debugging easier to reason about.
Comparisons build Boolean facts
Comparison operators produce bool: equality, inequality, ordering, and range facts. Logical AND requires both facts, logical OR requires at least one, and logical NOT reverses a Boolean. Short-circuit evaluation means the right side of && is skipped when the left side is false, which can safely guard an operation.
| Expression | Meaning | Common mistake |
|---|---|---|
| value == expected | Equal values | Writing = and assigning instead |
| age >= 18 && age <= 65 | Inside an inclusive range | Using || for the inside range |
| denominator != 0 && total / denominator > 2 | Guarded division | Reversing the operands |
| !is_ready | Not ready | Hiding a more meaningful positive name |
Convert at an explicit boundary
Implicit conversions are convenient when no information is lost, but narrowing should be visible. static_cast<int>(measurement) tells the reader that discarding a fraction is intentional; it does not prove the value fits. Validate ranges before narrowing external or large values.
Prefer storing a value in the representation the domain needs. Cents as an integer avoid fractional cents for a simple exercise. Real financial systems still need defined currencies, rounding modes, limits, and decimal rules rather than a single primitive type.
Quick knowledge check
Answer before you reveal.
01What is the result of 5 / 2 when both operands are int?
The result is the integer 2; the fractional remainder is discarded.
02Why can static_cast<double>(total) improve a calculation?
It makes the intended floating-point boundary visible before division, preventing accidental integer division.
Exercise
Practice challenge
Calculate the average of three integer scores as a double, then print whether the average meets a passing threshold. Test scores whose integer average would hide a fraction.
Requirements
- The average preserves its fractional part
- The pass decision uses the calculated average
- Parentheses make the sum and division order obvious
Optional extension: Reject a score outside the range 0 through 100 in the next lesson.
Open in C++ compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Is floating-point arithmetic broken?
No. It represents a wide range efficiently but many decimal fractions are approximate, so comparisons and financial rules need deliberate design.
Should I memorize the complete precedence table?
Learn common groups, but use parentheses when a reader might reasonably hesitate. Clarity is more valuable than demonstrating precedence memory.
Does signed integer overflow wrap around?
Portable C++ does not guarantee wraparound for signed overflow; it is undefined behavior and must be prevented.