Australian rules is a type of football. You score points by getting the ball between posts at either end of the field:
If the ball goes between the center two posts, that's a goal, and is worth six points. If the ball goes between one of the center and outside posts, that's a behind, and is worth one point.
Here's an Eva program for working that out.
Worksheet
You can change any of the cells.
CPU
The CPU runs the program, and does all the calculations.
Run status
Whether the CPU is running a program or not.
Next line
Next line of the program to be run.
You can change it.
Evaluator
Where the CPU does calculations.
What just happened
Program
The code someone typed.
Memory
Where variables are created. You can change their values.
Give it a try.
What do you notice?
Simon
It's almost the same as the compute tip program.
Tara
But the variables are different, and the programs do different things.
Simon
Well, yes, but the programs are… organized like each other.
Tara
Good! You're right.
Program structure
Here's the code for the Aussie rules program.
Dim goals As Integer
Dim behinds As Integer
Dim score As Integer
goals = Cells(1, 2)
behinds = Cells(2, 2)
score = goals * 6 + behinds
Cells(3, 2) = score
Tara
Hey, Jeremy! What one sentence would describe the first three lines?
Jeremy
Let me think… How about "Make some variables?"
Tara
Good! Geek speak for "make variables" is "declare variables." Means the same thing.
Let's add a comment to the code. A new first line.
'Declare variables
Dim goals As Integer
Dim behinds As Integer
Dim score As Integer
goals = Cells(1, 2)
behinds = Cells(2, 2)
score = goals * 6 + behinds
Cells(3, 2) = score
Comments begin with that single quote. The CPU ignores the text after the '. You use comments to remind yourself how the code works.
Tara
Kurt, how would you describe the next two lines in one sentence? That's lines 5 and 6.
Klaus
Umm…. how about "Get data from the worksheet"?
Tara
Sounds good!
'Declare variables
Dim goals As Integer
Dim behinds As Integer
Dim score As Integer
'Get data from the worksheet
goals = Cells(1, 2)
behinds = Cells(2, 2)
score = goals * 6 + behinds
Cells(3, 2) = score
Tara
Good! Let's keep going. Can you add comments to the rest of the program, to show how it's structured.
Simon
OK… How about this?
'Declare variables
Dim goals As Integer
Dim behinds As Integer
Dim score As Integer
'Get data from the worksheet
goals = Cells(1, 2)
behinds = Cells(2, 2)
'Compute results
score = goals * 6 + behinds
'Put results into worksheet
Cells(3, 2) = score
Tara
Great! There's a "Compute results" section, and a "Put results into worksheet" section.
Let's look at the first program we did. Here's the compute tip code:
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
It has exactly the same sections:
Declare variables
Get data from the worksheet
Compute results
Put results into worksheet
'Declare variables
Dim amount As Single
Dim tip As Single
Dim total As Single
'Get data from the worksheet
amount = Cells(1, 2)
'Compute results
tip = amount * 0.15
total = amount + tip
'Put results into worksheet
Cells(2, 2) = tip
Cells(3, 2) = total
Here are the programs, side by side.
'Declare variables
Dim amount As Single
Dim tip As Single
Dim total As Single
'Get data from the worksheet
amount = Cells(1, 2)
'Compute results
tip = amount * 0.15
total = amount + tip
'Put results into worksheet
Cells(2, 2) = tip
Cells(3, 2) = total
'Declare variables
Dim goals As Integer
Dim behinds As Integer
Dim score As Integer
'Get data from the worksheet
goals = Cells(1, 2)
behinds = Cells(2, 2)
'Compute results
score = goals * 6 + behinds
'Put results into worksheet
Cells(3, 2) = score
If we just look at the comments, they're exactly the same:
'Declare variables
'Get data from the worksheet
'Compute results
'Put results into worksheet
'Declare variables
'Get data from the worksheet
'Compute results
'Put results into worksheet
Two programs, doing different things, but they have the same structure.
Levels
You can look at every program on multiple levels. The structure level only has comments. Each comment describes a piece of the program. It says what will happen, without the details of how.
'Declare variables
'Get data from the worksheet
'Compute results
'Put results into worksheet
The code level shows the structure comments and the code.
'Declare variables
Dim goals As Integer
Dim behinds As Integer
Dim score As Integer
'Get data from the worksheet
goals = Cells(1, 2)
behinds = Cells(2, 2)
'Compute results
score = goals * 6 + behinds
'Put results into worksheet
Cells(3, 2) = score
When programmers start writing code, they start with the structure. They fill in the details later.
We'll come back to this idea of program structure throughout the course. Programs are much easier to write if you start with the structure first.
Summary
We looked at two programs that have the same structure. They do different things (tips vs. Aussie rules scores), use different variables, and different calculations. They have the same structure, however.