Skip to content

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.

An expression provides a value to be used in an instruction.

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.

OperatorDescriptionExample
( )Parenthesis(1 + 1) * 2
% * /Moduloa, multiplication, and division1 / 2 * 5 % 3
+ -Addition and subtraction10 + 3 - 4

a The remainder after division. For example, 9 modulo 3 is 0, 10 modulo 3 is 1, 11 modulo 3 is 2, 12 modulo 3 is 0, and so on.

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 ExpressionValue
7373
2.12.1
”Hello World""Hello World"
"Fred""Fred”
3 * 26
1 + 3 * 27
(1 + 3) * 28
7 - 3 + 15
3 / 21b
3 / 2.01.5c
3.0 / 2.01.5
3 % 21
11 % 32
1 + (3 / 2.0) + 6 * 2 - 86.5

b C# does integer division when none of the values in an expression contain decimal places.
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?

  1. 2
  2. "Learning is fun"
  3. 5 + 3
  4. 16 * 2
  5. 7 / 2
  6. 7 / 2.0
  7. 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.