Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on What are statements and expressions?
Parent
What are statements and expressions?
When I have tried to read technical explanations of the syntax rules for programming languages, and when I am trying to decipher error messages, I often encounter the terms expression and statement. It comes across that these two are related to each other somehow.
I understand that these terms have something to do with the actual code written in a programming language - not, for example, special sorts of values calculated by the program when it runs - right? But what do they mean exactly? How can I use these concepts to improve my understanding of a programming language?
Post
Statements and expressions are two syntactic categories that are used by many programming languages. Since they are syntactic, they depend on the programming language's syntax. In a real sense, a statement is "anything that can be used as a statement" and an expression is "anything that can be used as an expression"
Let me explain what I mean by this.
Expressions
While in most programming languages, the idea that expressions evaluate to a value and statements are actions does make intuitive sense, what things evaluate to values -- or even what values even are -- can be quite complicated and language-specific.
For example, consider void
functions in C, aka. non-value returning functions. Functions that return void
, intuitively, do not return a value. However, this is only half-true. Take a void function void foo();
. When we write the statement foo();
, this is actually an expression-statement that contains the expression foo()
, despite this expression, semantically, being void
and having no real value that it evaluates to.
This is why when we talk about expressions or statements, the real answer is just "whatever can be used as an expression, according to the programming language's grammar". And of course, what exactly counts as an expression depends on the language. C defines function calling syntax as an expression, regardless of the return type.
This doesn't have to be the case; Free Pascal for instance distinguishes "functions" which return values from "procedures" which don't. In Free Pascal, functions calls are expressions, while procedure calls are statements.[1]
As a more well-known example, Python is different from most mainstream languages in the C family in that it has both assignment statements using =
and assignment expressions using :=
.
C
int a;
int b = (a = 1);
Python (SyntaxError
)
a = 0
b = (a = 1)
Python
a = 0
b = (a := 1)
Statements
Again, most people have an intuition for what a statement is, and languages do try to follow that intuition when naming their syntax. Generally speaking, statements do something rather than evaluate to a value, though this is not universal, and what counts as a statement varies considerably between languages, and at any rate languages might decide that statements evaluate to values anyway (more on that later).
As mentioned previously, in C, assignment is an expression rather than a statement, despite primarily being used for its side-effect. In contrast, in Python, assignment can be either a statement or an expression, with different syntax for each.
All of this is just to say that again, what a statement is is defined more by syntax than any fundamental aspect, and different languages do the same thing differently.
Languages where everything is an expression
(or almost everything)
Functional languages and those influenced by them tend to have an everything-is-an-expression system, where what would traditionally be statements are expressions as well. In these kinds of languages, statements might not even exist, and you might only have declarations and expressions (for instance, Haskell falls into this category).
For example, in Rust, unlike C, if
-else
is an expression that evaluates to the result of the taken branch, essentially being the traditional ternary operator.
Rust
let y = if x == 1 {
"one"
} else {
"not one"
};
Even while-loops are expressions in Rust, although they always return the unit type.
Languages without expressions
At the extreme opposite end, assembly/machine/byte code only contains statements. Evaluating "expressions" (in the mathematical sense) relies on using statements. A hypothetical addition of 1 + 2
might look like this (in pseudo-code):
load_const reg1, 1 # Set reg1 to 1
load_const reg2, 2 # Set reg2 to 2
add reg3, reg1, reg2 # Add reg1 and reg2 and store the result in reg3
No expression to be seen here, just pure state manipulation, unless you consider reg1
and such to be expressions, though at this level, the entire point of distinguishing such a thing becomes somewhat meaningless
since the syntactic categories are instead 'instructions', 'registers', and 'constants'.
-
Interestingly, in Free Pascal, using an expression as a statement is actually a feature that you can turn on and off. ↩︎
1 comment thread