Learning path

Full curriculum

Full curriculum

Unit content

Expressions and operators

An expression is a piece of a program that produces a value.

Examples include literals such as

42
"hello"
true

and combinations of variables and operators such as

price * quantity
age >= 18

Operators

Arithmetic operators combine numerical values, comparison operators produce Boolean values, and logical operators combine Boolean conditions.

An operator's meaning depends on the types of its operands.

Precedence and grouping

Languages define precedence rules that determine how an expression such as

2 + 3 * 4

is grouped. Parentheses make the intended order explicit:

(2 + 3) * 4

Expressions and statements

An expression computes a value. A statement may instead change state or control execution, for example by assigning a value or choosing a branch.

Some languages blur this distinction by allowing assignments or function calls inside larger expressions, but separating the two ideas remains useful.

Evaluation

To evaluate an expression, the program obtains the current values of its parts and applies the required operations according to the language's rules.

Expressions are therefore the basic way programs derive new values from existing data.