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.
Post History
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 s...
Answer
#2: Post edited
- 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, 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, and you could easily have a programming language that distinguishes void functions *syntactically* from expressions, and I'm sure that such a language already exists (please comment if you know of one).As a more well-known example, Python is different from most mainstream languages in the C family in that assignment is *not* an expression.- <section class='notice is-success'>
- ### C
- ```c
- int a;
- int b = (a = 1);
- ```
- </section>
- <section class='notice is-danger'>
- ### Python (`SyntaxError`)
- ```py
- a = 0
- b = (a = 1)
- ```
- </section>
- ## 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 is a statement, and cannot be used where expressions are expected.- 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 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.
- <section class='notice is-success'>
- ### Rust
- ```rs
- let y = if x == 1 {
- "one"
- } else {
- "not one"
- };
- ```
- </section>
- 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):
- ```py
- 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'.
- 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]
- [^1]: Interestingly, in Free Pascal, using an expression as a statement is actually [a feature that you can turn on and off](https://www.freepascal.org/docs-html/current/prog/progsu123.html).
- 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 `:=`.
- <section class='notice is-success'>
- ### C
- ```c
- int a;
- int b = (a = 1);
- ```
- </section>
- <section class='notice is-danger'>
- ### Python (`SyntaxError`)
- ```py
- a = 0
- b = (a = 1)
- ```
- </section>
- <section class='notice is-success'>
- ### Python
- ```py
- a = 0
- b = (a := 1)
- ```
- </section>
- ## 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.
- <section class='notice is-success'>
- ### Rust
- ```rs
- let y = if x == 1 {
- "one"
- } else {
- "not one"
- };
- ```
- </section>
- 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):
- ```py
- 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'.
#1: Initial revision
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, 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, and you could easily have a programming language that distinguishes void functions *syntactically* from expressions, and I'm sure that such a language already exists (please comment if you know of one). As a more well-known example, Python is different from most mainstream languages in the C family in that assignment is *not* an expression. <section class='notice is-success'> ### C ```c int a; int b = (a = 1); ``` </section> <section class='notice is-danger'> ### Python (`SyntaxError`) ```py a = 0 b = (a = 1) ``` </section> ## 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 is a statement, and cannot be used where expressions are expected. 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 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. <section class='notice is-success'> ### Rust ```rs let y = if x == 1 { "one" } else { "not one" }; ``` </section> 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): ```py 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'.