upgrade
upgrade

🐛Intro to Computer Programming

Common Programming Operators

Study smarter with Fiveable

Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.

Get Started

Why This Matters

Operators are the verbs of programming—they're how you tell the computer to do something with your data. Every calculation, every comparison, every decision your program makes relies on operators working behind the scenes. You're being tested not just on recognizing operator symbols, but on understanding operator precedence, type compatibility, and how operators combine to create complex expressions. These concepts show up constantly in code tracing questions and debugging scenarios.

Think of operators as falling into distinct categories based on what they accomplish: some operators move data around, others transform it mathematically, and still others help your program make decisions. Don't just memorize symbols—know which category each operator belongs to and when you'd reach for one over another. Master the relationships between operators, and you'll write cleaner code and ace those tricky multiple-choice questions about expression evaluation.


Operators That Store and Move Data

These operators handle the fundamental task of getting values into variables. Assignment is the foundation of state management in programming—without it, your program can't remember anything.

Assignment Operator (=)

  • Stores the right-side value into the left-side variable—the variable acts as a container that holds whatever you assign to it
  • Works with any data type including integers, floats, strings, and objects—the variable takes on the type of the assigned value
  • Evaluates right to left, meaning x=y=5x = y = 5 assigns 5 to both variables in a single statement

Compound Assignment Operators (+=, -=, *=, /=)

  • Combines arithmetic and assignment in one stepx+=5x += 5 is shorthand for x=x+5x = x + 5
  • Reduces repetition and potential errors by referencing the variable only once instead of twice
  • Exists for all arithmetic operators including modulus (%=\%=), making iterative calculations cleaner

Compare: Assignment (==) vs. Compound Assignment (+=+=)—both store values, but compound operators modify existing values while simple assignment overwrites completely. If a question asks about updating a running total, compound assignment is your answer.


Operators That Calculate

Arithmetic operators transform numerical data through mathematical operations. These follow standard mathematical precedence rules (PEMDAS/BODMAS), which is a frequent source of exam questions.

Arithmetic Operators (+, -, *, /, %)

  • Perform the five core math operations—addition, subtraction, multiplication, division, and modulus (remainder)
  • Modulus (%) returns the remainder after division, making it essential for determining even/odd numbers or cycling through ranges
  • Division behavior varies by type—integer division truncates decimals (7/2=37 / 2 = 3), while float division preserves them (7.0/2=3.57.0 / 2 = 3.5)

Increment and Decrement Operators (++, --)

  • Adds or subtracts exactly one from a variable—x++x++ is equivalent to x=x+1x = x + 1
  • Prefix vs. postfix matters for expression evaluation++x++x increments before use, while x++x++ increments after use
  • Essential for loop control where you need to step through values one at a time

Compare: Arithmetic (++) vs. Increment (++++)—both can add to values, but increment is specialized for adding exactly one and modifying the variable in place. Watch for exam questions about prefix vs. postfix behavior in complex expressions.


Operators That Compare

Comparison operators evaluate relationships between values and return boolean results (true or false). These are the building blocks of conditional logic and control flow.

Comparison Operators (==, !=, <, >, <=, >=)

  • Equality (==) tests if values match—distinct from assignment (=), a common source of bugs
  • Relational operators establish ordering—less than, greater than, and their inclusive variants (<=<= and >=>=)
  • Always return a boolean that can be used directly in conditionals or stored in boolean variables

Compare: Assignment (==) vs. Equality (====)—this is the classic gotcha. Assignment changes a value; equality tests a value. Using == when you meant ==== compiles but creates a logic bug. Exam questions love testing this distinction.


Operators That Make Decisions

Logical operators combine boolean values to create complex conditions. These implement the fundamental logic gates (AND, OR, NOT) that underlie all digital computing.

Logical Operators (&&, ||, !)

  • AND (&&) requires both conditions true—useful for checking multiple requirements simultaneously
  • OR (||) requires at least one condition true—useful for accepting any of several valid options
  • NOT (!) inverts the boolean value—turns true to false and vice versa, enabling negative conditions

Ternary Operator (?:)

  • Compact if-else in a single expression—syntax: condition?valueIfTrue:valueIfFalsecondition ? valueIfTrue : valueIfFalse
  • Returns a value rather than executing statements—can be used inside assignments or function calls
  • Best for simple binary choices—complex conditions should use full if-else for readability

Compare: Logical AND (&&\&\&) vs. Logical OR (||)—AND narrows possibilities (both must be true), OR expands them (either can be true). FRQ tip: when asked to validate input, you'll typically chain conditions with AND; when checking for multiple valid options, use OR.


Operators That Manipulate Bits

Bitwise operators work directly on the binary representation of integers. These operate on individual bits, enabling low-level data manipulation and efficient flag management.

Bitwise Operators (&, |, ^, ~, <<, >>)

  • AND (&), OR (|), XOR (^) compare bits position by position—useful for masking, setting, or toggling specific bits
  • NOT (~) flips all bits—turns every 0 to 1 and every 1 to 0 in the binary representation
  • Shift operators (<<, >>) move bits left or right—left shift multiplies by powers of 2, right shift divides

Compare: Logical AND (&&\&\&) vs. Bitwise AND (&\&)—logical works on boolean values and short-circuits; bitwise works on every bit of integer operands. Confusing these is a common error, so watch the symbol count carefully.


Quick Reference Table

ConceptBest Examples
Storing values==, +=+=, =-=, =*=, /=/=
Mathematical calculation++, -, *, //, %\%
Incrementing/decrementing++++, --
Equality testing====, !=!=
Relational comparison<<, >>, <=<=, >=>=
Combining conditions&&\&\&, \|\|, !!
Conditional expressions?:?: (ternary)
Bit manipulation&\&, \|, ^\hat{}, \sim, <<<<, >>>>

Self-Check Questions

  1. What is the difference between == and ====, and what bug results from confusing them?

  2. If x=5x = 5, what are the values of yy and xx after executing y=x++y = x++ versus y=++xy = ++x?

  3. Which two operator categories both use the symbols &\& and |, and how do their behaviors differ?

  4. Compare and contrast: When would you use the ternary operator (?:?:) versus a full if-else statement?

  5. Given the expression 10+5210 + 5 * 2, what value results, and which concept explains why it's not 30?