Fiveable

🧵Programming Languages and Techniques I Unit 3 Review

QR code for Programming Languages and Techniques I practice questions

3.2 Arithmetic and Assignment Operators

3.2 Arithmetic and Assignment Operators

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🧵Programming Languages and Techniques I
Unit & Topic Study Guides

Arithmetic operations form the backbone of programming, allowing us to manipulate numbers and perform calculations. From basic addition to complex compound assignments, these tools give us the power to create dynamic, responsive programs that can handle a wide range of mathematical tasks.

Operator precedence and variable modification techniques add nuance to our arithmetic toolkit. By understanding the order of operations and how to incrementally change values, we can write more efficient and precise code, avoiding common pitfalls and creating cleaner, more readable programs.

Arithmetic Operations and Assignment

Basic arithmetic operations

  • Addition operator (+) combines values (a+ba + b) summing numbers (5 + 3 = 8) or concatenating strings ("Hello" + "World" = "HelloWorld")
  • Subtraction operator (-) finds difference between values (aba - b) calculating decreases (10 - 7 = 3) or negative numbers (-5 - 3 = -8)
  • Multiplication operator (*) multiplies values (aba * b) scaling quantities (4 * 3 = 12) or repeating strings ("abc" * 3 = "abcabcabc")
  • Division operator (/) divides values (a/ba / b) returning floating-point result (7 / 2 = 3.5) handling fractional calculations (1 / 3 ≈ 0.3333)
  • Modulus operator (%) computes remainder of integer division (aa % b) useful for cyclic operations (17 % 5 = 2) or checking odd/even (num % 2 == 0)
  • Integer division operator (//) performs floor division (a//ba // b) discarding remainder (7 // 2 = 3) rounding down to nearest integer (-7 // 2 = -4)
Basic arithmetic operations, OpenAlgebra.com: Free Algebra Study Guide & Video Tutorials: Order of Operations

Compound assignment operators

  • Addition assignment (+=) adds and assigns (a+=ba += b) incrementing counters (count += 1) or appending to strings (str += "new")
  • Subtraction assignment (-=) subtracts and assigns (a=ba -= b) decreasing values (balance -= 50) or removing from collections (list_length -= 3)
  • Multiplication assignment (*=) multiplies and assigns (a=ba *= b) scaling variables (price *= 1.1) or repeating sequences (pattern *= 2)
  • Division assignment (/=) divides and assigns (a/=ba /= b) reducing quantities (total /= 2) or calculating percentages (score /= 100)
  • Modulus assignment (%=) computes remainder and assigns (aa %= b) wrapping values (angle %= 360) or alternating states (turn %= 2)
  • Integer division assignment (//=) performs floor division and assigns (a//=ba //= b) grouping items (items //= 10) or truncating decimals (num //= 1)
Basic arithmetic operations, OpenAlgebra.com: Free Algebra Study Guide & Video Tutorials: Order of Operations

Operator Precedence and Variable Modification

Operator precedence in expressions

  • Parentheses () override default precedence grouping operations ((2 + 3) * 4 = 20)
  • Exponentiation (**) raises to power (2 ** 3 = 8)
  • Unary plus and minus (+x, -x) affect sign (-(-5) = 5)
  • Multiplication, division, modulus (*, /, %, //) perform higher-priority calculations (2 + 3 * 4 = 14)
  • Addition and subtraction (+, -) execute last in expression (10 - 5 + 2 = 7)
  • Equal precedence operators evaluate left to right (8 / 4 * 2 = 4)
  • Use parentheses for clarity and control ((8 / 4) * 2 = 4 vs 8 / (4 * 2) = 1)

Increment and decrement operators

  • Pre-increment (++a) increases before use (a = 5; b = ++a; // a is 6, b is 6)
  • Post-increment (a++) increases after use (a = 5; b = a++; // a is 6, b is 5)
  • Pre-decrement (--a) decreases before use (a = 5; b = --a; // a is 4, b is 4)
  • Post-decrement (a--) decreases after use (a = 5; b = a--; // a is 4, b is 5)
  • Commonly used in loops (for (int i = 0; i < 10; i++))
  • Provide concise syntax for modifying variables (count++ instead of count = count + 1)
  • Can lead to unexpected behavior if used multiple times in same expression (a = b++ + b++)
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly → and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →