← Documentation Home

2) Decisions, Data, and Routines

Back to Tutorials

Goal: model a small score table with decisions, loops, arrays, routines, enums, and a custom object.

Prerequisite: complete Language Foundations.


Step 1: Make a Decision with If

Dim score As Integer
score = 850

If score >= 1000 Then
    DebugPrint("Gold rank")
ElseIf score >= 500 Then
    DebugPrint("Silver rank")
Else
    DebugPrint("Bronze rank")
End If

The condition must be Boolean. Use comparisons such as =, <>, <, and >=, then combine conditions with And, Or, and Not.

Step 2: Repeat with For and While

Dim i As Integer

For i = 1 To 3
    DebugPrint("Round " + Str(i))
Next i

Dim lives As Integer
lives = 3
While lives > 0
    DebugPrint("Lives: " + Str(lives))
    lives = lives - 1
Wend

Use For when the number of repetitions is known and While when repetition depends on changing state.

Step 3: Store a List in an Array

Dim scores(4) As Integer
Dim total As Integer

scores[0] = 120
scores[1] = 450
scores[2] = 300
scores[3] = 610

For i = 0 To ArrayLen(scores) - 1
    total = total + scores[i]
Next i

DebugPrint("Total: " + Str(total))

Declare an array with parentheses, access elements with square brackets, and use ArrayLen instead of repeating its size throughout your program.

Step 4: Pass an Array to a Function

Function Highest(values() As Integer) As Integer
    Dim best As Integer
    Dim index As Integer

    best = values[0]
    For index = 1 To ArrayLen(values) - 1
        If values[index] > best Then
            best = values[index]
        End If
    Next index
    Return best
End Function

The empty parentheses in values() declare a one-dimensional array parameter. The function receives the array reference, so it does not copy every element.

Step 5: Group Commands in a Sub

Sub PrintScore(label As String, value As Integer)
    DebugPrint(label + ": " + Str(value))
End Sub

PrintScore("High score", Highest(scores))

A Sub performs work without returning a value. Parentheses are required in its declaration; calls may use the parenthesized form shown above.

Step 6: Give States Meaningful Names

Enum GameState
    Menu
    Playing
    Paused
    GameOver
End Enum

Dim state As GameState
state = GameState.Menu

Select state
Case GameState.Menu
    DebugPrint("Show menu")
Case GameState.Playing
    DebugPrint("Update game")
Case Else
    DebugPrint("Game is not active")
End Select

An enum replaces unexplained numbers with named integer values. Select is often clearer than a long chain of state comparisons.

Step 7: Group Related Fields in a Type

Type Player
    Field Name As String
    Field Score As Integer
    Field Lives As Integer
End Type

Dim hero As Player
hero = New Player
hero.Name = "Luna"
hero.Score = 850
hero.Lives = 3

DebugPrint(hero.Name + " has " + Str(hero.Lives) + " lives")
Free(hero)

Dim declares the object variable; New allocates the object. Match every owned object with Free when its lifetime ends.

Checkpoint: Score Analyzer

Function Highest(values() As Integer) As Integer
    Dim best As Integer
    Dim index As Integer

    best = values[0]
    For index = 1 To ArrayLen(values) - 1
        If values[index] > best Then
            best = values[index]
        End If
    Next index
    Return best
End Function

Function RankName(score As Integer) As String
    If score >= 1000 Then
        Return "Gold"
    ElseIf score >= 500 Then
        Return "Silver"
    End If
    Return "Bronze"
End Function

Dim scores(5) As Integer
Dim i As Integer

scores[0] = 250
scores[1] = 725
scores[2] = 400
scores[3] = 1100
scores[4] = 875

For i = 0 To ArrayLen(scores) - 1
    DebugPrint("Score " + Str(i + 1) + ": " + _
        Str(scores[i]) + " (" + RankName(scores[i]) + ")")
Next i

DebugPrint("Highest: " + Str(Highest(scores)))

The final line should report 1100. Change a value and run again to confirm the function searches the array rather than relying on a fixed answer.

Common Mistakes

  • Close each block with its matching terminator: End If, Next, Wend, End Function, or End Sub.
  • Array indexes begin at zero. Loop through 0 to ArrayLen(array) - 1.
  • Allocate a custom object with New before accessing its fields.
  • Ensure a function returns a value compatible with its declared return type.

Try It Yourself

Write an Average function that accepts an Integer array and returns a Double. Then add a Case for each GameState.

References Used

Control Flow, Arrays, Functions and Subs, Types and Objects, and ArrayLen.


Navigation: Previous | Course outline | Next: Your First Interactive Program