# ON...GOTO

{PBASIC 2.5}

ON...GOTO Example

Syntax: ON Offset GOTO Address1, Address2, ...AddressN

## Function

Go to the address specified by offset (if in range).

- Offset is a variable/constant/expression (0 - 255) that specifies the index of the address, in the list, to branch to (0 - N).
- Addresses are labels that specify where to go. ON...GOTOwill ignore any list entries beyond offset 255.

## Quick Facts

| BS2 Family
Limit of Address entries | 256
Related Commands | BRANCH, GOSUB, ON...GOSUB, GOTO

## Explanation

The ON...GOTO instruction is useful when you want to write something like this:

      IF idx = 0 THEN Case_0                ' idx = 0: jump to label "Case_0"
      IF idx = 1 THEN Case_1                ' idx = 1: jump to label "Case_1"
      IF idx = 2 THEN Case_2                ' idx = 2: jump to label "Case_2"

You can use ON...GOTO to organize this into a single statement:

      ON idx GOTO Case_0, Case_1, Case_2

This works exactly the same as the previous IF…THEN example. If the value isn't in range (in this case if value is greater than 2), ON...GOSUB does nothing and the program continues with the next instruction after ON.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012