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:

Rendered by QuickLaTeX.com
In the example above, the first expression, Rendered by QuickLaTeX.com, will yield the value True when evaluated, while the second expression, Rendered by QuickLaTeX.com, 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 specify 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, 7*2 is evaluated first, and then the results are added to 4. 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:

Rendered by QuickLaTeX.com

In the example above, the first statement performs the action of assigning the variable x 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:

Rendered by QuickLaTeX.com

The import statement will be executed before the if 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:

Rendered by QuickLaTeX.com

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.

guest
0 Comments
Inline Feedbacks
View all comments