Program structure

Aussie rules

Australian rules is a type of football. You score points by getting the ball between posts at either end of the field:

Aussie rules

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.

Give it a try.

What do you notice?

Simon
Simon
It's almost the same as the compute tip program.
Tara
Tara
But the variables are different, and the programs do different things.
Simon
Simon
Well, yes, but the programs are… organized like each other.
Tara
Tara
Good! You're right.

Program structure

Here's the code for the Aussie rules program.

  1. Dim goals As Integer
  2. Dim behinds As Integer
  3. Dim score As Integer
  4. goals = Cells(1, 2)
  5. behinds = Cells(2, 2)
  6. score = goals * 6 + behinds
  7. Cells(3, 2) = score

Tara
Tara
Hey, Jeremy! What one sentence would describe the first three lines?
Jeremy
Jeremy
Let me think… How about "Make some variables?"
Tara
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.

  1. 'Declare variables
  2. Dim goals As Integer
  3. Dim behinds As Integer
  4. Dim score As Integer
  5. goals = Cells(1, 2)
  6. behinds = Cells(2, 2)
  7. score = goals * 6 + behinds
  8. 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
Tara
Kurt, how would you describe the next two lines in one sentence? That's lines 5 and 6.
Klaus
Klaus
Umm…. how about "Get data from the worksheet"?
Tara
Tara
Sounds good!

  1. 'Declare variables
  2. Dim goals As Integer
  3. Dim behinds As Integer
  4. Dim score As Integer
  5. 'Get data from the worksheet
  6. goals = Cells(1, 2)
  7. behinds = Cells(2, 2)
  8. score = goals * 6 + behinds
  9. Cells(3, 2) = score

Tara
Tara
Good! Let's keep going. Can you add comments to the rest of the program, to show how it's structured.
Simon
Simon
OK… How about this?

  1. 'Declare variables
  2. Dim goals As Integer
  3. Dim behinds As Integer
  4. Dim score As Integer
  5. 'Get data from the worksheet
  6. goals = Cells(1, 2)
  7. behinds = Cells(2, 2)
  8. 'Compute results
  9. score = goals * 6 + behinds
  10. 'Put results into worksheet
  11. Cells(3, 2) = score

Tara
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:

  1. Dim amount As Single
  2. Dim tip As Single
  3. Dim total As Single
  4. amount = Cells(1, 2)
  5. tip = amount * 0.15
  6. total = amount + tip
  7. Cells(2, 2) = tip
  8. Cells(3, 2) = total
It has exactly the same sections:

  • Declare variables
  • Get data from the worksheet
  • Compute results
  • Put results into worksheet

  1. 'Declare variables
  2. Dim amount As Single
  3. Dim tip As Single
  4. Dim total As Single
  5. 'Get data from the worksheet
  6. amount = Cells(1, 2)
  7. 'Compute results
  8. tip = amount * 0.15
  9. total = amount + tip
  10. 'Put results into worksheet
  11. Cells(2, 2) = tip
  12. 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.

  1. 'Declare variables
  2. 'Get data from the worksheet
  3. 'Compute results
  4. 'Put results into worksheet
The code level shows the structure comments and the code.

  1. 'Declare variables
  2. Dim goals As Integer
  3. Dim behinds As Integer
  4. Dim score As Integer
  5. 'Get data from the worksheet
  6. goals = Cells(1, 2)
  7. behinds = Cells(2, 2)
  8. 'Compute results
  9. score = goals * 6 + behinds
  10. 'Put results into worksheet
  11. 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.