getJibba - Tutorial

6.Loops

'nested loop for rows & columns
For row = 1 To 3
  Y = row * 30
  For column = 1 To 10
    i = i + 1
    X = column * 25
    char = Text.GetCharacter(i + 64)
    btn[i] = Controls.AddButton(char, X, Y)
  EndFor
EndFor

A nested loop is a loop inside a loop.

Game Loop

'left and right Arrow keys move shape
mySprite = Shapes.AddRectangle(60, 60)

X = GraphicsWindow.Width /2 - 30
Y = GraphicsWindow.Height - 60

GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp

While 0=0
  If key = "Left" Then
    X = X - 3
  EndIf
  If key = "Right" Then
    X = X + 3
  EndIf

  Shapes.Move(mySprite, X, Y)
  Program.Delay(15)
EndWhile

Sub OnKeyDown
  key = GraphicsWindow.LastKey
EndSub

Sub OnKeyUp
  key = ""
EndSub

This sample shows a very simple game loop. By pressing the left and right arrow keys the shape will move.

For a fairly easy example of a game loop check out Space Invaders.

Infinite Loop

An infinite loop is a loop that keeps looping, infinitely. The expression 0=0 is true therefore the loop keeps looping.

Breaking a loop

Sometimes you need to break out of a loop before its finished. In the above sample if you set row = 3 that would break the loop early.

Another way is to use the GoTo command. In general using GoTo is broadly discouraged as its misuse can lead to spaghetti code.