## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Expressions and Statements

An expression is a piece of code that produces a value of some kind. We’ve actually seen several examples already, the most basic being the numbers we’re assigning into the variables. So in our example, a number such as:

`5.141`

is an expression. Expressions where we just tell C# what value we want are called literal expressions. More interestingly, expressions can perform calculations. For example, we could calculate the distance traveled per kilogram of fuel consumed with the expression in Example 2-6.

Example 2-6. Dividing one variable by another

`kmTravelled / fuelKilosConsumed`

The `/` symbol denotes division. Multiplication, addition, and subtraction are done with `*`, `+`, and `-`, respectively.

You can combine expressions together too. The `/` operator requires two inputs—the dividend and the divisor—and each input is itself an expression. We were able to use variable names such as `kmTravelled` because a variable name is valid as an expression—the resultant value is just whatever that variable’s value is. But we could use literals, as Example 2-7 shows. (A trap awaits the unwary here; see the sidebar on the next page.)

Example 2-7. Dividing one literal by another

`60 / 10`

Or we could use a mixture of literals and variable names to calculate the elapsed time in minutes:

`elapsedSeconds / 60`

or a multiplication expression as one of the inputs to a division expression to calculate the elapsed time in hours:

`elapsedSeconds / (60 * 60)`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required