Aussie rules
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.
A | B | C | ||
---|---|---|---|---|
1 | ||||
2 | ||||
3 | ||||
4 |
Running Whether the CPU is running a program or not. Evaluator
(Empty) Where the CPU does calculations. What just happened
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
What do you notice?




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



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.



- '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


- '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

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
- 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
-
'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
- '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
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.