Jumpin' around
 

Before we begin, a word of caution. The methods presented here for jumping around your program are very simple to understand, and therefore it may be tempting to use them all the time. However, you should use them only when appropriate - because of their simplicity, they can actually cause you more problems when your programs start getting more complex, or can create an unweildy mess of code.

Both of the following methods use labels to specify where the program should continue executing from. A label can only consist of letters, underscores and numbers, although they cannot start with a number - each label name must be unique. A label can be specified in your code by simply writing the label name - but make sure that the label name is by itself, not part of an expression (or the compiler will think it is a variable) and is not the same as any existing command, procedure (described in next topic) or variable name.

Normally you can put code on the same line after the label if you put a colon after the label. AmiBlitz also allows you to not need the colon, if you turn on the new code syntax using the optimize command (see the AmiBlitz docs).

Goto
The first method which can be used to jump around your program is to use the Goto command. This will simply make the program start executing commands at the next command after the specified label. If you run the following code, you will see that the first NPrint is never executed, because the Goto jumps to a label after it.

Code Goto skipme
NPrint "Hello, world!"
skipme
NPrint "End of program"
MouseWait
End

Following on from the previous page, you can use the Goto command along with the Pop command to exit loops early. This is probably the most useful use of this command.

Code For i.w=0 To 10
NPrint "Counting... ",i

; This is where we check to see if we should exit the loop
If i=5

; And this is where we actually exit the loop
; First we pop the innermost loop we MUST pop (i.e. the For loop - If's do not count)
Pop For

; Then we must jump past the end of loop command
Goto exitloop

EndIf
Next

exitloop:
MouseWait
End

Gosub...Return
The second type of command that is used to change the execution point of your code is the Gosub command. The name comes from the words "Goto Subroutine", where a subroutine is a section of code that you jump to and run, and then return to the command after where you called it from.

Local labels
An extra feature you can use is local labels. After you use a normal label (described above) you can use local labels. These follow the same rules as other labels, but they MUST start with the ' (apostrophe) character. Local labels are only accessable between the normal label they are defined after and the next normal label. That means you can repeat label names only if they are after other normal labels. The benefit of local labels is the ability to repeat the names, so your code doesn't end up a mess of unreadable labels :).

For example, this is OK:

label1:
NPrint "Starting..."
Goto 'local1
NPrint "Whatcha think ya doin mutha sucka!"
'local1:
NPrint "Exit"
MouseWait
End

But this is not:

label1:
NPrint "Starting..."
Goto 'local1
label2:
NPrint "Whatcha think ya doin mutha sucka!"
'local1: ; This local label is not in the same range as the Goto command trying to jump here
NPrint "Exit"
MouseWait
End

The next topic in this section of the site will deal with more structured and cleaner methods for jumping around and repeating large sections of code in your programs.


Previous: Exiting loops early Programming contents Next


News | Introduction | Search | List/Forum/IRC | Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to programming main
Page last modified: 17th March 2002