Expression-oriented programming languages
From Wikipedia, the free encyclopedia
An expression-oriented programming language is a programming language where (nearly) every construction is an expression and yields a value. Macro definitions, preprocessor commands, and declarations are often treated as statements in expression-oriented languages. Some expression-oriented languages introduce a void return type to be yielded by expressions that have only side-effects.
ALGOL 68 is an example of an expression-oriented language. Pascal is not. All functional programming languages are expression-oriented.
Expression-orientation can be confusing in imperative programming languages, as many commands used normally as statements are in fact expressions. For example, assignment in the C programming language is an expression, not a statement. This allows for the following confusion:
if (x = 1) { ... } else { /* this branch is never executed */ }
The condition of the if is the result of the expression x = 1
, which is 1. Thus the if will always execute the true branch. The above is often confused with:
if (x == 1) { ... /* executed if x is 1 */ } else { ... /* executed if x is not 1 */ }
For this reason, many programmers write the constant first in conditionals, as 1 = x
is a syntax error, and mistyping = for == will not result in a bug.