Expressions

The word “expression” is used a lot in programming. You should know what an expression is.

Expressions are what the CPU evaluator does

The CPU is the computer component that does most of the work, in real computers and in Eva. Eva’s CPU has an evaluator.

Eva CPU

As you saw, the evaluator does all of the computation.

An expression is anything in your code that the evaluator needs to work out. If the evaluator is involved, it’s an expression.

Here’s code you’ve seen, with the expressions highlighted.

Dim amount As Single
Dim tip As Single
Dim total As Single
amount = Cells(1, 2)
tip = amount * 0.15
total = amount + tip
Cells(2, 2) = tip
Cells(3, 2) = total

For each expression, the evaluator computes something, moves data around, or both. For example…

amount = Cells(1, 2)

… moves data around. But if we had written…

amount = Cells(1, 2) + 5

… the evaluator would do a computation as well as move data around.

Here’s the Eva for that program. If you watch, each time there’s an expression, the CPU moves a value into the evaluator.

Here’s another program you’ve seen.

‘Declare variables
Dim animal As String
Dim sound As String
‘Get data from spreadsheet
animal = Cells(1, 2)
‘Compute sound
If animal = “dog” Then
    sound = “woof”
ElseIf animal = “cat” Then
    sound = “meow”
Else
    sound = “Don’t know”
End If
‘Output to spreadsheet
Cells(2, 2) = sound

Each of the highlighted things is an expression, that is, a computation the CPU’s evaluator does. That includes the expressions in the If and ElseIf statements. The CPU uses the evaluator to compute whether the expression is true or false, so the CPU can work out what to do next.

Here’s the Eva, if you want to try it. Watch how the evaluator is involved in every expression.

Even this statement has an expression:

total = 0

Not much of one, but still, it counts.

Constants

When they’re in code, numbers like 0, 10, and -11 are called constants, because they never change. In…

aVariable = 17
anotherVar = 2.78
yetAnother = -42

… none of the things on the right of the =s will ever change. In…

cuteness = puppies * 10

… 10 is a constant. It will always be the same.

Expressions show up all over the place

You’ve seen statements that put a value into a variable or cell.

amount = Cells(1, 2)
goals = Cells(1, 2)
Cells(2, 2) = sound

These are “assignment statements,” because they assign the value on the right of the = to the thing on the left. If you looked in VBA’s tech specs (technical specifications) for an assignment statement you’d see something like:

[destination] = [expression]

So, assignment statements have expressions.

Here’s part of the tech spec for an If statement:

If [expression] Then

If statements have expressions, too. Later, you’ll see that subroutine calls, loops, input and output statements, file open statements, and others, all use expressions.

Anywhere you can put a constant, like 4, you can put an expression, like arms + legs.

The rules

Expressions have rules, that is, things you are allowed to do, and things you aren’t. What happens if you break the rules? If you’re lucky, your program will crash. If you’re not lucky, your program will give the user the wrong answer.

Jeremy
Jeremy
Isn’t crashing worse?

Klaus
Klaus
Yeah… Wait, maybe not. Say you’re writing a program that gives people prices for stuff on a Web site, like shoes. Your program has a bug, and tells someone that some shoes cost $79.95. Really, they cost $179.95. Oops.

Maybe it would be better if your program crashed, and the price came up as “Unknown.” Or something.

Tara
Tara
Good thinking, Klaus! Later, we’ll be talking about what to do when Bad Things happen.

Let’s talk about different kinds of expressions, and how you use them correctly.

Summary

An expression is anything the CPU’s evaluator computes. That includes moving data from one variable to another, since there could be a calculation. If the evaluator is involved, it’s an expression.