Blitz:Program Flow

From Amiga Coding
Jump to: navigation, search

Amiblitz / Blitz Basic supports the general program flow commands common to most other BASIC dialects.

End

This statement quits the program and cleans up any resources used automatically. You should always include this at the end of your code, even if it appears to work without it since it tells the Blitz compiler to free up or close any resources the program used. Not freeing resources can cause memory to be lost until the next reboot, or for resource to remain unavailable to other programs.

End d0

This is a special form of the End statement, which is only available in AmiBlitz. This form allows the program to return a value to the shell it was run from. This is frequently used to return error codes, typically 0 means no errors, 5 means a warning, 10 means an error and so on. The value returned is whatever number is in the d0 CPU register. This value can be set prior to the End statement using the PutD0 command:

PutD0 10 ; Program failed with error level 10
End d0

Goto label

Causes program flow to jump directly to the label specified. For example:

NPrint "Message 1"
Goto MyLabel
NPrint "Message 2"
.MyLabel
Nprint "Message 3"

This example will show messages 1 and 3 only since flow jumps straight to where the label MyLabel is and carries on from there. Note: Using Goto is generally considered poor practice as repeated use quickly makes code difficult to follow and debug. You should consider using alternative methods, such as procedures, conditional blocks or loops instead.

Gosub label

Program flow jumps to the position of the label specified until it encounters a Return statement, which returns program flow to the command immediately after the Gosub command. For example:

NPrint "Message 1"
Gosub MyLabel
NPrint "Message 2"
End

.MyLabel
Nprint "Message 3"
Return

This example will display the messages in the order 1, then 3, then 2. Note: While arguably better than Goto, using Gosub is also generally considered poor practice. You should consider using procedures instead to keep your code more modular and manageable.

On...Goto/Gosub

This is a way to carry out one of a number of Goto or Gosub branches based on an expression, and is similar to that functionality found in most other BASICs. A number of different program labels are provided, and the program flow will jump to which ever label is indicated by the expression. For example:

On player Gosub player1, player2, player3

; More code here

.player1
; Player 1's code
Return

.player2
; Player 2's code
Return

.player3
; Player 3's code
Return

In this example, if the player variable contains the value 1, program flow will Gosub branch to the player1 label. If it's 2, it will Gosub to the second label, and if it's 3 it will Gosub to the third label. If the number provided doesn't match up with any of the labels, i.e. it is 0 or higher than the number of labels provided, no jumps will be made and program flow will continue with the next statement after the On statement.

Again, this is generally considered poor practice carried over from the old 8-bit BASIC days, and can make your code difficult to follow and debug. You should consider using alternative methods, such as Case...Select...End Select blocks.