Variables

3. Examples

3.2. Characteristics

1. Precedence: Determines the order of operations (e.g., * before +). Use parentheses to override (e.g., (x + y) * z).

2. Associativity: Defines the order for operators of the same precedence (e.g., left-to-right for +, right-to-left for =).

3. Type Sensitivity: Operators expect compatible types (e.g., + for strings concatenates, for numbers adds).

4. Side Effects: Some operators (e.g., ++, +=) modify operands.

Use Cases

1. Arithmetic: Calculating totals, averages, or measurements.

2. Relational/Logical: Controlling program flow with conditions (e.g., if statements).

3. Assignment: Updating variable values dynamically.

4. Increment/Decrement: Managing loop counters or indices.

Examples

Java:

int x = 10; // Assignment

x += 5; // Compound assignment (x = 15)

boolean isEqual = x == 15; // Relational (true)

int result = x * 2 + 3; // Arithmetic (33)

boolean condition = x > 10 && x < 20; // Logical (true)

x++; // Increment (x = 16)

Python:

x = 10

x += 5

is_equal = x == 15

result = x * 2 + 3

condition = x > 10 and x < 20

x += 1

C++:

int x = 10;

x += 5;

bool isEqual = x == 15;

int result = x * 2 + 3;

bool condition = x > 10 && x < 20;

x++;