Expressions distilled

A main building block of programs

Elementary school math

2 + 3 = ?

🚫 Not what we’re doing 🚫

Algebra

x + 3 = 5, find x

🚫 Not what we’re doing 🚫

Computer programming

x + 3 = ?

Let the computer do the work.

👍 What we’re here for 👍

Values

The simplest expression is a single value.

For instance

10

Operators

Operators allow us to combine expressions to get new expressions.

An example

2 + 3

2 is a value (and thus an expression)

3 is a value (and thus an expression)

+ is an operator.

2 + 3 is an expression.

It evaluates to 5.

The operators on numbers we care about

+ - addition

- - subtraction

* - multiplication

/ - division

% - remainder

** - exponentiation

Expressions can get arbitrarily complex

(2 + 3) / 10 ** 6

Computer evaluates sub-expressions until it gets to a value

(2 + 3) / 10 ** 6

5 / 10 ** 6

5 / 1000000

0.000005

Names can also be used as expressions

We assign values to names:

apples = 29

kids = 3

This is an expression

apples

It evaluates to whatever value was assigned to apples.

This is also an expression

apples / kids

It expresses how many apples we should give each kid to divide them evenly.

We don’t have to know the values that have been assigned to apples or kids to understand what this expression means.

The computer can evaluate it (assuming it does know the appropriate values).

A lot of programming comes down to

Translating what we mean into expressions

Understanding the meaning of expressions in code