The Goto Command

goto.png

Command Summary

Jumps to a label somewhere else in the program.

Command Syntax

:Goto label-name

Menu Location

This command can't be found in any menu besides the command catalog.

Calculator Compatibility

This command works on all calculators.

Token Size

2 bytes

The Goto command jumps to a label (declared with the Lbl command) somewhere else in the program — either before or after the Goto, it doesn't matter. The label has to be in the same program as the Goto — you can't jump into another program. If there are several labels with the same name, the Goto command will only find the first.

Virtually everyone critiques the Goto command for being unnecessary and encouraging bad coding habits. The argument is that it makes code hard to read: if you see a 'Goto x' somewhere in the program, you have to search through the entire program to find out where to continue reading. And most of the things that Goto is used for can be done better using commands like If, While, Cycle, etc. TI seems to agree, because they didn't even bother putting Goto and Lbl in the program editor toolbar.

That being said, it is sometimes (very rarely) a good idea to use Goto. A good example is a situation when you need to exit several loops at once (this can't be done with the Exit command, which only exits one loop).

A note for TI-83 series programmers: the issue of memory leaks from improper use of the Goto command does not occur on 68k calculators. The 68k TI-Basic parser doesn't use a stack to keep track of loops and If blocks entered: instead, End statements have a link back to what it is they're matching. This means that nothing special happens if a Goto jumps out of a loop: the calculator doesn't even know it's inside a loop except at the end when it has to do the looping.

The 68k calculators have their own bit of unexpected Goto behavior: code like expr("Goto x") will not work. This is because the code that expr( runs is treated as though it were inside its own program, so you can't jump out of that program into the program that it's actually in.

Optimization

The Cycle and Exit commands perform tasks that you might otherwise use Goto for. By all means, use these command instead if you can: they work much faster, since they don't have to look through the entire program for the label (in fact, they don't have to look at all — they already know where to jump).

Error Conditions

500 - Invalid label happens when the label doesn't exist (in this program).

Related Commands

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License