Programming Basics

Expression vs. Statement

Programming Concepts

Senthil Nayagan
Senthil Nayagan Publication

--

Photo by Kevin Ku from Pexels.

Expression and statement are not the same

Expression and statement are two different terms that are commonly misunderstood. Let’s see how one is different from the other.

In programming, an expression is a combination of values, variables, constants, operators, and functions that are combined and interpreted by the compiler to create a new value, as opposed to a statement, which is just a standalone unit of execution and doesn’t return anything.

Most imperative programming languages make a distinction between expressions and statements and have rules about where each kind can be used. Pure functional languages, on the other hand, cannot support statements at all because a truly pure functional language would have no side-effects.

Statements lead to side-effects

A function or expression is said to have a side effect:

  • If it modifies some states, for example, changing values.
  • It has an observable interaction with calling functions or the outside world.

Having said that, “side effects” means changing something somewhere. The general purpose of an expression is to create a value with or without some possible side effects, while the sole purpose of a statement is to have side effects.

Benefits of expression over statement

  • Unlike statements, smaller expressions can be combined (aka “composed”) into larger expressions.
  • A series of statements always implies a specific order of evaluation, which means that a statement cannot be understood without looking at prior statements. However, with pure expressions, the sub-expressions do not have any implied order of execution or dependencies.

--

--

Senthil Nayagan
Senthil Nayagan Publication

I am a Data Engineer by profession, a Rustacean by interest, and an avid Content Creator.