The Goto Command

[ LiB ]

Before writing our full-fledged game, I want to introduce you to the concept of Goto . Goto is a very simple command, but it can be misused very easily, so I recommend using Goto as sparingly as possible. Almost always, if something can be done by using Goto , it can be done in another way.

Goto works like this: you add a label somewhere in your code, and Goto jumps to that label. (See Figure 2.12.) The best illustration of this is a sample program.

Figure 2.12. Using Goto .


 ;demo02-10.bb  Demonstrates use of Goto .label Print "Hello" selection = Input("Enter 1 if you want me to repeat 'Hello' ==> ") If (selection = 1)       Goto label EndIf End 

The output is shown in Figure 2.13.

Figure 2.13. The demo02-10.bb program.


NOTE

Notice that I did not include WaitKey in this program. Because the program repeats and then ends with an End command, the WaitKey is not necessary.

As you can see in Figure 2.12, calling Goto starts the program back at the top. This is accomplished by putting .label at the top of the program. You can make Goto move anywhere by simply moving the line .label . Notice that when you define the label, you put a period (.) before it. When you call it from Goto , however, you discard the period.

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net