Programs and variables

Multiple choice

Which of these is true?

Saving
A

Variables in programming are just like variables in math.

B

Variables in programming are entirely different from variables in math.

Lesson contents

Working out the tip in a restaurant

Say we want a program that will figure out the tip for a meal, in an American restaurant. It's usually 15%.

Adela, how do you compute a tip?

Adela
Adela

Well, I'd take the meal cost, whatever it was. What is it?

Uh, let's say $25.

Adela
Adela

OK. The tip is 25 × 0.15. That's 3.75.

Then I'd take the tip, 3.75, and add it to the meal cost, of 25.

That would give a total, 28.75

Great! Adela remembered how to work out a tip:

  • Multiply meal cost by 15%.
  • Then add to meal cost to get the total to pay.

That's a program, a set of instructions Adela used to do the task.

To run her program, she had to get some data: the meal cost. Then she could do her calculations. The last step of Adela's program was to tell us what the result was.

Adela runs a program

Adela's smart, so the program could be a bit sloppy, and she could work out what to do. Here's the program again:

  • Multiply meal cost by 15%.
  • Then add to meal cost to get the total to pay.

The program doesn't tell Adela to ask us for the meal cost. She figured out that she needed that, though, and asked. The program didn't say she should tell us the results, but she worked out that she should.

Writing the program for Adela to run is pretty easy.

Computers are stupid

We can't use Adela's program with a computer, because computers are really, really, really stupid. They need every step spelled out. Miss one, or get it wrong, and the program won't work.

Truth Goat

Truth Goat: You won't believe how stupid computers are.

Here's a program that a computer might run. (It's pseudocode, not in a real programming language.)

  • Ask a human for a number. Put that in [meal cost].
  • Multiply [meal cost] by 0.15. Put that in [tip amount].
  • Add [meal cost] and [tip amount]. Put that in [total].
  • Tell the human [tip amount] and [total].

It's much longer than Adela's program:

  • Multiply meal cost by 15%.
  • Then add to meal cost to get the total to pay.

Why is it longer? Because you need to tell the computer all the deets, or the program won't work.

That's what programmers do. Tell a stupid stupid stupid machine all the deets to work something out.

You can run the program with this button. Try it, with a number, like 50.

Now try it again, but this time type the word "fifty" instead of the number 50. Will the computer be smart enough to know what you wanted?

Nope. Stupid computer.

Variables

Computers are so stupid, they forget what they did, from one line to the next. Here are the first two lines of the program again.

  • Ask a human for a number. Put that in [meal cost].
  • Multiply [meal cost] by 0.15. Put that in [tip amount].

By the time the computer starts running the second line, it's forgotten about the first.

Seriously.

When you want a computer to remember something from one line to another, you have to tell it to remember. Check out the first two lines again.

  • Ask a human for a number. Put that in [meal cost].
  • Multiply [meal cost] by 0.15. Put that in [tip amount].

The first line says to get some input, and remember it as [meal cost]. Then the computer forgets that line.

The second line tells it to take something from [meal cost], multiply it by 0.15, and put the result in [tip amount].

[meal cost] and [tip amount] are variables. A variable a tiny chunk of computer memory, with a name.

[meal cost] links the two lines together. So, the first line puts something into [meal cost].

  • Ask a human for a number. Put that in [meal cost].

Then that line is done.

The second line is:

  • Multiply [meal cost] by 0.15. Put that in [tip amount].

Take something from [meal cost], and multiply it by 0.15.

The second line has no idea what the first line did, but it doesn't have to. A human programmer told the first line to put something in [meal cost]. Then the programmer told the second line to get something from [meal cost]. The programmer made sure the two lines work together.

It's the programmer that makes up for the computer's stupidity, by creating a variable, telling one line to put data in it, and the next line to get data from it.

Ray wonders how stupid computers are

Here's the program again.

  • Ask a human for a number. Put that in [meal cost].
  • Multiply [meal cost] by 0.15. Put that in [tip amount].
  • Add [meal cost] and [tip amount]. Put that in [total].
  • Tell the human [tip amount] and [total].
Ray
Ray

OK, so what if we took out part of the first line, the piece that put the human's data into a variable.

  • Ask a human for a number. (Something deleted.)
  • Multiply [meal cost] by 0.15. Put that in [tip amount].

Would the program work?

No. The second line would fail, since there'd be nothing in [meal cost].

Marcus
Marcus

But, it would still ask for a number?

Yes, because the program says to:

  • Ask a human for a number.
  • Multiply [meal cost] by 0.15. Put that in [tip amount].
Marcus
Marcus

What would it do with the number it got from a human?

Throw it away, because the code doesn't tell it to do anything with the number.

Ray
Ray

Wow, that's super-duper stupid.

Yes. Many bugs are from programmers forgetting how stupid computers are. They miss steps that are so obvious to humans that we don't even think about them. But they must be spelled out to computers.

So far...

  • A program is a set of steps that a computer runs.
  • Programmers use variables to link the steps together.
  • Computer are stupid.

One more thing. The 0.15 in the line:

  • Multiply [meal cost] by 0.15. Put that in [tip amount].

That's called a constant, since it's a value that never changes.

To Excel

Let's Excelize that. We want a worksheet that starts like this:

Before entering data

Start Excel, and set that up. You already learned about making buttons. Check that lesson if you need to. (Remember the right-click-new-tab trick).

The user types a number into cell B1, then clicks the "Compute tip" button.

Input

The computer works out the answers, and outputs them:

Output

Here's code for the tip program. Copy it into the code for your button, and try it out.

  • Dim sMealPrice As Single
  • Dim sTipAmount As Single
  • Dim sTotal As Single
  • ' Input meal price.
  • sMealPrice = Cells(1, 2)
  • ' Compute tip and total.
  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount
  • ' Output.
  • Cells(5, 2) = sTipAmount
  • Cells(6, 2) = sTotal

Note

Remember that Option Explicit should be the first line in your code.

Let's run through the code, starting with the first three lines.

  • Dim sMealPrice As Single
  • Dim sTipAmount As Single
  • Dim sTotal As Single
  • ' Input meal price.
  • sMealPrice = Cells(1, 2)
  • ' Compute tip and total.
  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount
  • ' Output.
  • Cells(5, 2) = sTipAmount
  • Cells(6, 2) = sTotal

The first three lines tell the computer that we need three variables, what the names of the variables are, and that we want to put numbers in them (that's what Single means; more later). This is called declaring the variables.

Georgina
Georgina

I'm guessing that the s in the variable names, like sTotal, has to do with them being Single?

Right. It's called Hungarian naming, but don't worry about that now. We'll get to it soon.

After these lines run, computer memory looks like this:

Variables declared

Variables in memory

When you declare a variable, VBA automatically sets the variable's value to 0. Not all languages do this. In those languages, you have to set the value to 0 yourself.

Input

Let's check out the next two lines.

  • Dim sMealPrice As Single
  • Dim sTipAmount As Single
  • Dim sTotal As Single
  • ' Input meal price.
  • sMealPrice = Cells(1, 2)
  • ' Compute tip and total.
  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount
  • ' Output.
  • Cells(5, 2) = sTipAmount
  • Cells(6, 2) = sTotal

The first one is a comment. It explains to a human what the code below it does. Comments begin with a single quote ('). The computer ignores everything after the quote.

The next line says "Take what is in the cell at row 1, column 2, and put it into the variable sMealPrice. If the cell at row 1, column 2 had 25 in it:

Assignment

Copy a value from a cell in a worksheet, into a variable in memory

Here's that line again:

  •     sMealPrice = Cells(1, 2)

Important! The = does not mean what it means in math. It's something else entirely in programming. It says: "Take the value of the thing on the right, and copy it into the thing on the left."

Adela
Adela

So the = is a bit confusing, then.

Yes, I agree. It would better if it looked like an arrow:

  •     sMealPrice <- Cells(1, 2)

That's clearer: take the value of the thing on the right, and copy it into the thing on the left.

That's not the way VBA is, though. You must use =.

This is called an assignment statement, since it assigns a value to a variable. So an assignment statement always has the form:

  •     to_here = from_here

Computation

Here's the code again:

  • Dim sMealPrice As Single
  • Dim sTipAmount As Single
  • Dim sTotal As Single
  • ' Input meal price.
  • sMealPrice = Cells(1, 2)
  • Up to here
  • ' Compute tip and total.
  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount
  • ' Output.
  • Cells(5, 2) = sTipAmount
  • Cells(6, 2) = sTotal

We talked about declaring variables, and getting the user's input into a variable.

Let's look at the next three lines:

  • Dim sMealPrice As Single
  • Dim sTipAmount As Single
  • Dim sTotal As Single
  • ' Input meal price.
  • sMealPrice = Cells(1, 2)
  • ' Compute tip and total.
  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount
  • ' Output.
  • Cells(5, 2) = sTipAmount
  • Cells(6, 2) = sTotal

A comment, then more assignment statements. Take the thing on the right, and copy it to the thing on the left.

The thing on the right is an expression. It's a computation of some kind.

Adela
Adela

So the * means multiply?

Yes, it does. We'll go over the deets later.

Adela
Adela

A question. The code is:

  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount

Do we need the variable sTipAmount? Could we do this instead?

  • sTotal = sMealPrice + sMealPrice * 0.15

Yes! Great thinking, Adela! We could do that.

We wouldn't have sTipAmount, though, which we need for the output.

Output

Let's look at the last bit of the code:

  • Dim sMealPrice As Single
  • Dim sTipAmount As Single
  • Dim sTotal As Single
  • ' Input meal price.
  • sMealPrice = Cells(1, 2)
  • ' Compute tip and total.
  • sTipAmount = sMealPrice * 0.15
  • sTotal = sMealPrice + sTipAmount
  • ' Output.
  • Cells(5, 2) = sTipAmount
  • Cells(6, 2) = sTotal

What's this code do?

Ray
Ray

Two assignment statements. Take what is on the right, and put it into what is on the left. The things on the left are cells on the worksheet.

So this code copies the variables sTipAmount and sTotal into the worksheet, so the user can see them.

Right! Those lines output the variables' values.

Once more with feeling

OK, let's go through the code again. Explain what the pieces of code do, in your own words. Then compare it with what the virtual students think.

Here's some code:

  •     Dim sMealPrice As Single
  •     Dim sTipAmount As Single
  •     Dim sTotal As Single
Reflect

What does the code do?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Ray
Ray

The computer makes three variables in its memory, with the names given. sMealPrice, sTipAmount, and sTotal.

Good. Next:

  •     sMealPrice = Cells(1, 2)
Reflect

What does this code do?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Georgina
Georgina

Take the value in the cell at row 1, column 2. Put it in the variable sMealPrice.

That's an assignment statement.

Nice. Next:

  •     sTipAmount = sMealPrice * 0.15
  •     sTotal = sMealPrice + sTipAmount
Reflect

What does this code do?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Marcus
Marcus

Take whatever is in the variable sMealPrice, and multiply it by 0.15. Put the result in sTipAmount.

Then take what is in sMealPrice, add sTipAmount, and put that in sTotal.

Excellent. Last bit:

  •     Cells(5, 2) = sTipAmount
  •     Cells(6, 2) = sTotal
Reflect

What does this code do?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Adela
Adela

Take sTipAmount, and put its value into the worksheet cell at row 5, column 2. Then put sTotal into row 6, column 2.

Now that the values are in the worksheet, the user can see them. This is the program's output.

Yes! Great job, everyone!

Exercise

Do this exercise. We talked about how to submit exercises, if you need a reminder.

Exercise

Custom tip amount

Change the tip program, so that the user can enter a custom tip amount. Instead of 0.15, the user can enter whatever s/he wants.

The worksheet could start like this:

Start

The user clicks the button and sees:

15% tip

If the user changes 0.15 to 0.2 and clicks the button:

20% tip

Hints:

  • Start by adding a row for tip rate on the worksheet.
  • Add a new variable to the code.

Upload your worksheet.