# ON...GOSUB Conditional Subroutine Execution

> Source: https://manual.pvxplus.com/directives/on-gosub

## Format

**ON** *num **GOSUB** stmtref,stmtref,...*  
  
***Where***:

|  |  |
| --- | --- |
| *num* | Value determines the location to which to transfer. Numeric expression. Integer range -32768 to +32767. |
| *stmtref* | Program line number or statement label to which to transfer control. |

## Description

Use the **ON ... GOSUB** directive to transfer control to a subroutine at one of the statement references listed, based on the numeric value supplied:

|  |  |  |
| --- | --- | --- |
|  | *num* \<=0 | Control is transferred to the first statement specified. |
|  | *num* =1 | Control is passed to the second statement number specified. |
|  | *num* =2 | Control is passed to the third. |
|  | *num* =3… *nnn* | Control is passed sequentially. |

If *num* is greater than the number of *stmtref* supplied, the last *stmtref* is assumed.

## See Also

[GOSUB.. Execute Subroutine](https://manual.pvxplus.com/directives/gosub)  
[RETURN Return From a Subroutine](https://manual.pvxplus.com/directives/return)

## Example

```
0020 on X gosub 0100,0200,0300,0400,0500
```

|  |  |  |
| --- | --- | --- |
|  | **ON...** | **GOSUB** |
|  | X \<= 0 | Transfers to 0100 |
|  | X = 1 | Transfers to 0200 |
|  | X = 2 | Transfers to 0300 |
|  | X = 3 | Transfers to 0400 |
|  | X >= 4 | Transfers to 0500 |
