Tuesday, 20 September 2016

GOTO

Quite often you don’t want the program to run exactly in the order you put the lines, from the first to the last. Sometimes you want the program to jump to a particular line. 

For example, your program asks the user to guess a particular number:

******** ‘some of the program here
INPUT “Guess the number”; n
******** ‘some of the program there

The program then checks if the entered number is correct. But if the user gives the wrong answer, you may want to let him try again. So you use the command GOTO, which moves the program back to the line where the question is asked. But first, to show QBasic where to go, you must “label” that line with a number:

1 INPUT “Guess the number”; n         ‘this line is labelled with number 1

Then, when you want the program to return to that line, you type 

GOTO 1

You can use GOTO to jump not only back but also forward, to any line you want. Always remember to label that line. You can have more than one label, but in that case they should be different.

No comments:
Write comments