3) Programming Fundamentals (Step by Step)
This section is intentionally detailed. Read each concept, then run the example. Learning is faster when you test small pieces one by one.
Step 1: Understand Values and Types
A program works with values. A type tells LunarBasic what kind of value each variable can hold.
- Integer: whole numbers (
0,-5,42). - Float/Double: decimal numbers (
1.5,3.14159). - Boolean:
TrueorFalse. - String: text inside quotes (
"Hello").
Dim score As Integer
Dim speed As Float
Dim isAlive As Boolean
Dim playerName As String
score = 10
speed = 2.5
isAlive = True
playerName = "Luna"
Step 2: Learn Assignment
Assignment stores a value in a variable.
Dim health As Integer
health = 100
health = health - 25
After the second assignment, health becomes 75.
Step 3: Use Expressions and Math
Dim total As Integer
Dim base As Integer
Dim bonus As Integer
base = 50
bonus = 20
total = (base + bonus) * 2
Expressions can combine numbers, variables, and operators.
Step 4: Make Decisions with If
Dim score As Integer
score = 85
If score >= 100 Then
DebugPrint("Perfect")
ElseIf score >= 60 Then
DebugPrint("Pass")
Else
DebugPrint("Try again")
End If
Step 5: Repeat Work with Loops
For Loop
Dim i As Integer
For i = 1 To 5
DebugPrint("Count: " + Str(i))
Next
While Loop
Dim x As Integer
x = 0
While x < 3
DebugPrint("x=" + Str(x))
x = x + 1
Wend
Step 6: Organize Code with Subs and Functions
Sub performs work. Function performs work and returns a value.
Sub PrintGreeting(name As String)
DebugPrint("Hello, " + name)
End Sub
Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
Dim result As Integer
PrintGreeting("Luna")
result = Add(3, 4)
Step 7: Store Multiple Values with Arrays
Dim points(5) As Integer
points[0] = 10
points[1] = 20
points[2] = 30
DebugPrint("First point: " + Str(points[0]))
Step 8: Use Strings for User-Facing Text
Dim firstName As String
Dim message As String
firstName = "Ari"
message = "Welcome, " + firstName
DebugPrint(message)
Step 9: Build a Tiny Complete Program
SetAppTitle("Fundamentals Demo")
Graphics(800, 600, False)
SetClsColor(12, 18, 28)
Dim running As Boolean
Dim fontRef As Integer
Dim x As Integer
fontRef = LoadFont("", 20)
running = True
x = 50
Do
If KeyDown(KEY_RIGHT) Then x = x + 3
If KeyDown(KEY_LEFT) Then x = x - 3
Cls()
DrawText(fontRef, x, 40, "Move with LEFT/RIGHT. ESC exits.")
Flip()
If KeyHit(KEY_ESC) Then
running = False
End If
Loop Until Not running
FreeFont(fontRef)
Common Beginner Mistakes (and Fixes)
- Mistake: using a variable before assigning it. Fix: initialize variables early.
- Mistake: forgetting
End If,Next, orWend. Fix: write closing lines immediately. - Mistake: putting expensive setup inside the frame loop. Fix: load once before the loop, free after the loop.
- Mistake: changing many things before testing. Fix: test after each small change.
Deep Reference Pages
- Names and Values
- Variables, Constants, and Assignment
- Control Flow
- Functions, Subs, and Return
- Arrays
- Expressions
Navigation: Previous: Complete IDE Guide | Outline | Next: Game Development Concepts