← C++ from Scratch Lesson 9 of 62 · 10 XP

Operators

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

  • arithmetic: + - * / %;
  • compound assignment: += -= *= /= %=;
  • increment: x++ / ++x — plus one (x-- / --x — minus one);
  • comparisons: == != < > <= >= → bool;
  • logic: && (and), || (or), ! (not);
  • ternary: condition ? if_yes : if_no.
int coins = 10;
coins += 5;                 // 15
coins++;                    // 16
bool rich = coins >= 100;   // false
std::string s = rich ? "rich" : "poor";

Precedence: * and / come before +; comparisons before &&; && before ||. When in doubt, use parentheses.

Task. Read an integer n. Print n squared, then even or odd — using the ternary operator.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.