|
The GOTO and GOSUB commands enables you to jump to certain positions in your program. Labels are used to specify what point in the program to continue execution. GOTO To use GOTO, place a label somewhere in your program, and then enter.
Run the following example program:
GOTO TheLabel PRINT "2" TheLabel: PRINT "3"
3
GOSUB The GOSUB command is the same as GOTO, except when it encounters a RETURN statement, the program "returns" back to the GOSUB command. In other words, RETURN continues program execution immediately after the previous GOSUB statement.
GOSUB TheLabel PRINT "2" END TheLabel: PRINT "3" RETURN Since the program returns to the GOSUB command, the number 2 is printed this time.
3 2 Line numbers "Line numbers" can be used as labels.
GOTO 10 PRINT "2" 10 PRINT "3" (Notice the line number) You can also write the program like this:
20 GOTO 40 30 PRINT "2" 40 PRINT "3" The line numbers don't even have to be in sequence.
2 GOTO 160 701 PRINT "2" 160 PRINT "3" Each of these programs output:
3 Guessing game The following is a simple guessing game:
start: PRINT "Guess a number between 1 and 10: "; INPUT num IF (num < 1 OR num > 10) THEN
GOTO start IF (num = 6) THEN
GOTO start
Try again Guess a number between 1 and 10: ? 7 Try again Guess a number between 1 and 10: ? 6 Correct!!!
Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc. http://www.geocities.com/progsharehouse/qbtutor Mirror: http://development.freeservers.com/qbtutor |