Skip to content

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 example: 2 + 3 * 4
final int result = 2 + 3 * 4;  // result is 14

// With parentheses: (2 + 3) * 4
final int withParentheses = (2 + 3) * 4;  // withParentheses is 20
// Operator precedence example: 2 + 3 * 4
val result = 2 + 3 * 4  // result is 14

// With parentheses: (2 + 3) * 4
val withParentheses = (2 + 3) * 4  // withParentheses is 20
// Operator precedence example: 2 + 3 * 4
const result: number = 2 + 3 * 4;  // result is 14

// With parentheses: (2 + 3) * 4
const withParentheses: number = (2 + 3) * 4;  // withParentheses is 20
// Operator precedence example: 2 + 3 * 4
final int result = 2 + 3 * 4; // result is 14

// With parentheses: (2 + 3) * 4
final int withParentheses = (2 + 3) * 4; // withParentheses is 20
// Operator precedence example: 2 + 3 * 4
let result = 2 + 3 * 4  // result is 14

// With parentheses: (2 + 3) * 4
let withParentheses = (2 + 3) * 4  // withParentheses is 20
# Operator precedence example: 2 + 3 * 4
result = 2 + 3 * 4  # result is 14

# With parentheses: (2 + 3) * 4
with_parentheses = (2 + 3) * 4  # with_parentheses is 20
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:

int x = 5;
x = x + 3; // x is now 8
you can use a compound assignment:
int x = 5;
x += 3; // x is now 8

Below are examples in various languages:

int guitarStrings = 6;
guitarStrings -= 1; // guitarStrings is now 5
var guitarStrings = 6
guitarStrings -= 1 // guitarStrings is now 5
let guitarStrings: number = 6;
guitarStrings -= 1; // guitarStrings is now 5
int guitarStrings = 6;
guitarStrings -= 1; // guitarStrings is now 5
var guitarStrings = 6
guitarStrings -= 1 // guitarStrings is now 5
guitar_strings = 6
guitar_strings -= 1  # guitar_strings is now 5