Operators, Division Traps, and Conversion
Arithmetic, comparison and logical operators; why 7/2 is 3; overflow in one sentence; and explicit casts.
Arithmetic
+ - * / % work as expected on integers and doubles โ with one famous trap:
std::cout << 7 / 2; // 3 โ integer / integer = integer!
std::cout << 7.0 / 2; // 3.5 โ one double makes the whole expression double
std::cout << 7 % 2; // 1 โ remainder (modulo)
Integer division truncates. If a calculation should be fractional, at least one operand must be a double. % is for integers only and is everywhere in real code: even/odd checks, ring buffers, pagination.
Comparison and logic
Comparisons produce bool: == != < <= > >=. Combine them with && (and), || (or), ! (not). Precedence trips beginners, so when in doubt, parenthesize:
if ((age >= 18) && (has_ticket)) { /* ... */ }
(Real bug to avoid: = assigns, == compares. if (x = 5) compiles โ and is almost never what you meant.)
Conversion: implicit and explicit
int small = 3.9; // implicit: 3, silently (why braces are better)
double avg = static_cast<double>(total) / count; // explicit, honest
static_cast<double>(x) says out loud "I want a real-numbered division here". This course uses static_cast โ never C-style (double)x casts, which are hard to search for and can silently succeed where they should fail.
Overflow in one sentence
Every type has limits (int is typically ยฑ2.1 billion). Computing past them wraps around silently โ there is no exception. The beginner defense is simply: pick a sensible type (long long for big counts) and know that overflow exists. Deep handling is Intermediate material.
Practice focus
The exercises here are pure computation โ unit conversions, bill splits, grade averages โ chosen because each one famously bites beginners with the division trap at least once.