Expression
Some instructions need data, which can be calculated or provided as a literal value. The term expression is used in programming to describe any code that represents a value. Expressions are used wherever data must be supplied in code. When a program runs, each expression is evaluated to become a single value that can be used by an instruction.
The concepts related to expressions are shown below.
Expressions — when, why, and how
Whenever an instruction needs data, an expression is needed to provide it. This lets you put whatever data you want in that instruction. Data can be hard-coded as a literal, use variables, and contain mathematical operators to calculate a value.
When you code an expression, you need to think about the value you need and how you can get it. You could provide a fixed value using a literal, read a value stored in a variable, or combine values with standard mathematical operators. If you are unsure, draw out the details you have on paper and think through how you can calculate the value you need.
If you do not have the details you need, you may need to think about adding extra variables to capture these values when they are available. You can then use them to get the value you need.
In C#
An expression is used to define a value. If a literal value is given, the computer doesn’t need to do anything. However, if an expression uses mathematical operations, the computer will evaluate the expression to a single value.
The following table lists the operators that you can include in expressions in order of precedence. The operators you can use in any specific expression depend on the kind of data that you are using. For example, it does not make sense to use the division operator on text data, so C# will return an error if you try to do this in your code.
Operator | Description | Example |
---|---|---|
( ) | Parenthesis | (1 + 1) * 2 |
% * / | Moduloa, multiplication, and division | 1 / 2 * 5 % 3 |
+ - | Addition and subtraction | 10 + 3 - 4 |
Examples
The following table shows some example expressions and the value they evaluate to in C#. Some of these examples are literal values, and others use mathematical calculations. Have a look at the table, and note down anything that surprises you. If you are confused about the last row, remember that C# will follow the BOMDAS order of operations.
Example Expression | Value |
---|---|
73 | 73 |
2.1 | 2.1 |
”Hello World" | "Hello World" |
"Fred" | "Fred” |
3 * 2 | 6 |
1 + 3 * 2 | 7 |
(1 + 3) * 2 | 8 |
7 - 3 + 1 | 5 |
3 / 2 | 1b |
3 / 2.0 | 1.5c |
3.0 / 2.0 | 1.5 |
3 % 2 | 1 |
11 % 3 | 2 |
1 + (3 / 2.0) + 6 * 2 - 8 | 6.5 |
c If any of the values in an expression have decimal places, the value the expression evaluates to does as well.
Activities
What values do the following expressions evaluate to?
2
"Learning is fun"
5 + 3
16 * 2
7 / 2
7 / 2.0
10 - (1 + 2) * 3
Answers
- 1:
2
- 2:
"Learning is fun"
- 3:
8
- 4:
32
- 5:
3
. There are no numbers with decimal places in this expression, so C# will use integer division. - 6:
3.5
. C# won't use integer division here because one of the values in the expression does have a decimal place, even if that decimal value is 0! - 7:
1
. Order of operations means that C# will evaluate the brackets first, then the multiplication, then the subtraction.