Lesson 7 / 36

Operators

Combine values into expressions.

Arithmetic

Integer division truncates. Cast to double for a real quotient.

int a = 7, b = 2;
a / b;            // 3  (integer division)
a % b;            // 1  (remainder)
(double)a / b;    // 3.5

Compare & combine

Comparisons == != < > <= >= produce 0 or 1. Logical && (and), || (or), ! (not). Shortcuts: a += 3, a++.

Quick check: In C, what is `7 / 2`?

  • 3.5
  • 3
  • 4
Answer

3 — Both operands are int, so the result is integer-divided to 3.