The difference between statements and expressions

ProgrammingJun 2021

The simplest way to understand the two is to focus on expressions and then understand statements in relationship to expressions.  In programming, an expression can be seen as anything that resolves itself into a single value; as opposed to a statement which is an instruction most often acts on that value.

In the example below, the number 25 resolves itself to 25.

20 + 5 also resolves itself into 25 by the time it's assigned.

Now, assuming that multiply returns the first number multiplied by the second, then the function multiply() is still an expression since it resolves itself to a value.

Even a more complex expression made of subexpressions is still an expression as it resolves itself to the value 25.

Therefore, even a function with millions of lines of code, many of which instructions, in the whole can still be seen as an expression as it will eventually resolve itself to the value it returns.

Everything around expressions, around values, are instructions, most often to manipulate and transform those values. Therefore, something as simple as telling the computer to assign a variable to a value is a statement.

As well as a conditional statement, a loop, et al.

In light of this, a program can be seen as nothing more than a series of instructions that can take value and work at it to transform it into a new value. Take a basket of apples, count the apples, and return how many apples are in the basket: 25. 😅

No items found.