Goal: model a small score table with decisions, loops, arrays, routines, enums, and a custom object.
Prerequisite: complete Language Foundations.
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.
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.
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.
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.
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.
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.
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.
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.
End If, Next, Wend, End Function, or End Sub.0 to ArrayLen(array) - 1.New before accessing its fields.Write an Average function that accepts an Integer array and returns a Double. Then add a Case for each GameState.
Control Flow, Arrays, Functions and Subs, Types and Objects, and ArrayLen.
Navigation: Previous | Course outline | Next: Your First Interactive Program