Qbasicnews.com

Full Version: What does GOTO do?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Some of my friends have said that GOTO is another one of those useful commands that my teacher never botherd to tell us about. So I'm thinking of using it in my final project, but I don't know what it is. :-?

Can somebody explain what GOTO does?
It jumps directly to the label or line number that you specify...

Code:
LoopDaLoop:
i=i+1
print i
Goto LoopDaLoop
Quote:GOTO - a control flow statement that branches unconditionally to the
specified line
Syntax
GOTO {linelabel | linenumber}
þ linelabel or linenumber is the label of the line to execute next.
This line must be in the same procedure or subroutine as the GOTO
statement


Goto works just as Dr_D explained. I was always taught though that goto wasn't very good programming practice as it would make it easy to turn your code into spaghetti code with your program jumping all over the place. I'd say find other ways to accomplish what you would with GOTO, but that's only because that's the way I was taught.
in case that wasn't enuf,

use GOTO like this:
Code:
1
GOTO 1
or
Code:
linename:
GOTO linename

GOTO's aren’t really fun to follow especially with complicated code.

also using GOTO's as loops are a very bad idea too.
just use DO...LOOP or FOR...NEXT or WHILE...WEND


ok i'll admit, i just felt like posting...
GOTO, tells your computer to grow legs and do whatever you ask it, example:
Code:
GOTO Store.And.Buy.Me.Some.Candy

Very useful..
GOTO bank..withdrawl..$20
GOTO store..buy..a..pack..of..smokes
CALL dealer(buy, a, gram)
Quote:Some of my friends have said that GOTO is another one of those useful commands that my teacher never botherd to tell us about. So I'm thinking of using it in my final project, but I don't know what it is. :-?

Can somebody explain what GOTO does?
BLADER,

In order to answer your question, I'm going to give you a simple explanation of how a computer executes instructions.

When a computer program is loaded into computer memory, it is assigned a starting address. This address is the first instruction to be executed.
It will perform the first instruction, and continue to the next instruction, unless the instructon is a GOTO, which in machine language is called a JUMP, TRANSFER, or BRANCH.

This means that the computer will continue executing consecutive instructions until it encounters the equivalent of a GOTO.

When a GOTO is encountered, it does not execute the next instruction, but begins executing the instruction at the address contained in the GOTO instruction.

In QB, the GOTO statement or instruction is always unconditional, that is, GOTO right now, no matter what. In machine language it can be conditional, like JUMP IF ZERO TO SAME.

We can do the same thing in QB by using an IF and a GOTO:
IF A = B THEN GOTO SAME

Actually, in assembler, there are also two parts to the above JUMP IF ZERO example. First, you would subtract B from A, and then do the JUMP IF ZERO.

In QB, there are other statements besides GOTO which allow you to change the sequence of instruction execution. These are "loop control" statements, which basically will continue to execute a set of instructions in a loop until a given condition is met. Some of these "loop control" statments are:
FOR ... NEXT
DO WHILE... LOOP
WHILE ... WEND

Another form of statments which change the sequence of instruction execution are the SELECT CASE ... CASE ... END SELECT.

If your program did not have the capability to do the equivalent of a GOTO, it would start at the beginning, execute all the instructions once, and exit after the last instruction. This is not what you normally want. You need to control the flow and execution of the instructions in your program. Any form of a GOTO will give you this ability.
*****
I think goto isnt very usefull. In most cases you can just use a CALL statement or FOR or LOOP. So if your lookin for good marks on your final i wouldnt use it. Instead just woo your teacher with lots of remarks and some graphics.
GOTO can save you a bit of programming time, for example you don't have to think about the order of blocks of code as much, a text battle engine i'm den00bafying (i'm working on and off at it) has to be completely re-ordered to remove the GOTO command and keep it running correctly,

I also have to trap it at the very end in a while:wend or do:loop loop. Using GOTO was so much simpler (and i made it when i was just starting programming, hence the goto's)...
When I want to use GOTOs, I try to assign line numbers and remarks that are significant, so that I can easily see where I am going to and where I came from, such as:

Code:
100  GOTO 240
code
.....................
.....................
.....................
240 'from 100, 320
some more code
.....................
.....................
.....................
320 GOTO 240
.....................
.....................
.....................
For instance, one way that I use to test my programs with test data is by entering something as shown below, with the "TEXT$ = "Y" " very near the beginning of the program, where it is handy to get at:

Code:
'Program description, name, revision, date, author, etc. goes here
....................
'for testing, delete the ' in second line
TEXT$ = "N"
'TEST$ = "Y"
code
....................
....................

'test data goes here:
a=10:b=30:c=25:d=123.45:e=55

'next line allows testing without keyboard entries:
220 IF TEST$ = "Y" THEN GOTO 230

'input data code goes here, overriding the test data during
'normal running of the program
LINE INPUT "Enter a ";a
LINE INPUT "Enter b ";b
LINE INPUT "Enter c ";c
LINE INPUT "Enter d ";d
LINE INPUT "Enter e ";e

230 'from 220  
more code here
........................
This allows me to run the program without having to bother entering data from the keyboard, especially when one has to input more than just a few lines of data. Once the program runs smoothly and properly with the test data, it's a simple thing to simply remark out the "TEXT$ = "Y" again.
Pages: 1 2 3 4 5