Lesson 208 +5 XP

C++ Operators Reference

C++ Operators Reference

Operators run all the real work in a program. This is the cheat-sheet lineup, grouped by what each does.

Arithmetic operators

OperatorDoes whatExample
+additiona + b
-subtractiona - b
*multiplicationa * b
/divisiona / b
%remainder (modulo)a % b

Integer division drops the fraction: 7 / 2 is 3, not 3.5.

Comparison operators

OperatorMeaningValue
==equal totrue/false
!=not equal totrue/false
>greater thantrue/false
<less thantrue/false
>=greater than or equaltrue/false
<=less than or equaltrue/false

Logical operators

OperatorPronunciationMeaning
&&andtrue only if both sides are true
``ortrue if at least one side is true
!notflips true to false and false to true

Assignment operators

OperatorShorthand for
=sets a value directly
+=a = a + b
-=a = a - b
*=a = a * b
/=a = a / b
%=a = a % b
&=bitwise and, then assign
`=`bitwise or, then assign
^=bitwise xor, then assign
<<=left-shift bits, then assign
>>=right-shift bits, then assign

Increment / decrement

OperatorMeaning
++xadd 1, then use the new value
x++use the current value, then add 1
--xsubtract 1, then use the new value
x--use the current value, then subtract 1

Bitwise operators

OperatorMeaning
&bitwise AND
``bitwise OR
^bitwise XOR
~bitwise NOT
<<shift bits left (also the stream insertion operator)
>>shift bits right (also the stream extraction operator)

Ternary (the only 3-part operator)

OperatorPattern
? :condition ? valueIfTrue : valueIfFalse

int max = (a > b) ? a : b; picks a or b based on the condition. One line, no if statement.

Scope and pointer operators

OperatorWhat it does
::scope resolution - reach a name in a class or namespace (std::cout)
.member access - access a field or method of an object (player.score)
->arrow - access a member through a pointer (ptr->score)
*pointer dereference - get the value the pointer points at
&address-of - get the memory address of a variable

Other common operators

OperatorMeaning
sizeofbyte size of a type or variable
,comma - run several expressions, the last one is the result
<< and >>stream out and stream in (overloaded for cout/cin)
& (in a class)references - alias to another variable (int& r = x)
::scope resolution - same as noted above

Precedence shortcut

Remember the loose ordering (highest to lowest work, roughly):

  1. () \[\] -> . - things, subscripts, members.
  2. ! ~ ++ -- - * & - unary prefixes.
  3. * / % - multiply and divide.
  4. + - - add and subtract.
  5. << >> - stream/shift.
  6. < > <= >= - comparisons.
  7. == != - equality.
  8. && - logical and.
  9. || - logical or.
  10. = += -= ... - assignment (right-to-left).
  11. , - lowest of all.

If you're ever unsure, add parentheses. Clear beats clever.

Notes

  • / on integers rounds toward zero - the fraction disappears.
  • % is meaningless for floats; use fmod from <cmath`` if needed.
  • == compares values; = assigns a value. Swapping them is a classic bug.
  • && and || stop early (short-circuit): if the left side decides it, the right side never runs.
  • The << symbol does double duty: bit shifting in math and "send to stream" for output.

TL;DR

  • Arithmetic: + - * / %.
  • Comparison: == != > < >= <=.
  • Logical: && || !.
  • Assignment shortcuts: += -= *= /= %=.
  • Ternary ? : picks one of two values in a single line.
  • :: resolves scope, -> reaches through pointers, & takes an address.
  • When unsure of precedence, use parentheses.