Operators
Operators#
Operators are symbols that perform operations on values or variables. They enable mathematical calculations, comparisons, and data manipulation in programming.
Common operators include + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). These symbols work consistently across programming languages, making code intuitive and readable.
graph TD
A[Data Values] --> B[Operators]
B --> C[Arithmetic Operations]
B --> D[Compound Assignment]
C --> E[Basic Math: +, -, *, /, %]
D --> F[Shortcuts: +=, -=, *=, /=, %=]
style B fill:#e3f2fd
style E fill:#c8e6c9
style F fill:#c8e6c9
Arithmetic Operators#
Arithmetic operators perform basic math operations: + (add), - (subtract), * (multiply), / (divide), and % (remainder).
Operations follow standard math rules: multiplication and division happen before addition and subtraction.
Operator Precedence#
Precedence determines which operations happen first. 2 + 3 * 4 equals 14 (not 20) because multiplication comes before addition.
graph LR
A["Expression: 2 + 3 * 4"] --> B["Step 1: 3 * 4 = 12"]
B --> C["Step 2: 2 + 12 = 14"]
D["Precedence Order"] --> E["1. Parentheses ()"]
E --> F["2. Multiplication *, Division /"]
F --> G["3. Addition +, Subtraction -"]
style A fill:#ffcdd2
style C fill:#c8e6c9
style D fill:#e3f2fd
Compound Assignment Operators#
Compound operators combine math with assignment: +=, -=, *=, /=, %=.
Instead of writing:
you can use a compound assignment:Below are examples in various languages: