1. Introduction
This tutorial will review two commonly used terms in Computer Science, namely expressions and statements, along with the differences between them.
2. Expression
An expression usually refers to a piece of code that can be evaluated to a value, and is composed of variables, operators, function calls, and literals. In most programming languages, common examples of expressions are method calls and mathematical operations.
Expressions are generally formulated differently in various programming languages. The basic idea, however, is that an expression will always yield an output; it evaluates to something. Let’s look at some simple examples of expressions:
5>3
7*2 + 4
In the example above, the first expression, , will yield the value True when evaluated, while the second expression, , will yield the value 18.
In most programming languages, there are typically three different types of expressions: arithmetic, character, and logical. Arithmetic expressions usually evaluate to a numeric value, character expressions will yield a character value, and logical expressions will yield a logical value.
Several different types of expressions can be combined to produce a single expression; however, this is dependent on the programming language in use. Other types of expressions include:
- Floating expressions, which yield a floating-point value
- Pointer expressions, which yield address values
- Bitwise expression, used for testing and shifting bits
Due to the different types of expressions, an expression will always have a type, either an integer, character or boolean value.
To evaluate expressions, we need to consider the precedence rules, or order of operations, of the language in which it is defined. Order of operations rules specifies how expressions are evaluated based on the operators used in the expression. Consequently, different operators have different priorities associated with them.
For example, it’s commonly known that the order of operations for most programming languages is: parentheses, exponents, multiplication or division, addition or subtraction. In the second example expression given above, is evaluated first, and then the results are added to . This is because multiplication has a higher priority than addition.
Another interesting characteristic of expressions is that the output can be printed either to a screen or a file.
3. Statement
Conversely, a statement refers to a piece of code that executes a specific instruction or tells the computer to complete a task. A statement can take the form of assignments, control statements, import statements, loop statements, jump statements, or method calls.
Let’s consider this code sample:
x=20
import library
In the example above, the first statement performs the action of assigning the variable with a value of 20. In the second statement, the instruction is given to import a library. Neither statement yields a value but instead performs a certain action.
Due to their property of not yielding a value, statements are usually of the type void. This means that they don’t have a data type as they don’t yield any value. Thus, the results of a statement evaluation cannot be printed.
To evaluate a statement, we take into consideration the order and context in which the statements are defined. Unlike expressions, statements are not evaluated based on the order of operations. Therefore, there are no priority rules associated with the elements of the statements. Let’s consider the statements below:
import library
if x = 20:
then exit
The statement will be executed before the statement because it was defined first.
4. Similarities and Differences
The main similarity between expressions and statements is that they’re both executed in computer programs.
The differences are as follows:
Expressions
Statements
Expressions evaluate to a value
Statements perform action
An expression can be made of several expressions
A statement cannot be made up of another statement
Results from the evaluation can be printed
Results from the evaluation cannot be printed
Expressions usually have a type in most programming languages
In most programming languages, statements are of the type void
Expressions are mostly defined by the user
Most statements are already predefined in the programming language
Evaluated based on the rules of order of operations of the programming language
Evaluated based on order/sequence
5. Conclusion
In this article, we defined expressions and statements. Then we reviewed some examples of each to see how they are evaluated. Finally, we discussed some basic similarities and differences between these two constructs.