Jump anywhere: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 29:
===Direct Jump===
This is strictly a one-way trip, just like GOTO in BASIC. The program counter will be set to the specified address or label. This technique is limited to constants. In other words, a direct jump cannot be altered at runtime (self-modifying code notwithstanding)
<langsyntaxhighlight lang="6502asm">JMP PrintChar ; jump to the label "PrintChar" where a routine to print a letter to the screen is located.</langsyntaxhighlight>
 
===Indirect Jump===
This method is a form of the "computed GOTO" and lets you jump to an address stored in a pair of memory addresses. The least effective way to do this is as follows:
<langsyntaxhighlight lang="6502asm">lda #<PrintChar ;load into A the low byte of the address that PrintChar references.
sta $00
lda #>PrintChar ;load into A the high byte of the address that PrintChar references.
sta $01 ;these need to be stored low then high because the 6502 is a little-endian cpu
JMP ($00) ;dereferences to JMP PrintChar</langsyntaxhighlight>
 
The above example is just the same as a direct jump but more complicated for no added benefit. To actually use an indirect jump correctly, the input needs to be variable. Here's one way to do that:
 
<langsyntaxhighlight lang="6502asm">JumpTable_Lo: db <PrintChar, <WaitChar, <ReadKeys, <MoveMouse ;each is the low byte of a memory address
JumpTable_Hi: db >PrintChar, >WaitChar, >ReadKeys, >MoveMouse ;each is the high byte of a memory address
 
Line 48:
lda JumpTable_Hi,x
sta $11
JMP ($0010)</langsyntaxhighlight>
 
Depending on the value of x, you will be taken to a different procedure. Once again, this is a one-way jump and you can't return.
Line 54:
Another way to do this is with a "jump block."
 
<langsyntaxhighlight lang="6502asm">.org $8000
JMP PrintChar
JMP WaitChar
JMP ReadKeys
JMP MoveMouse
</syntaxhighlight>
</lang>
 
Each jump instruction on the 6502 takes up 3 bytes: one for the JMP command itself, and two for the destination. Knowing this, we can use a variable that is a multiple of 3 to take us to the desired location. Let's pretend X is such a variable, and this routine lets us JMP to the "next" routine.
<langsyntaxhighlight lang="6502asm">lda $80
sta $21
txa
Line 68:
adc #$03
sta $20
JMP ($0020)</langsyntaxhighlight>
 
If X was 0, the JMP takes us to WaitChar.
Line 80:
The 65c02, which was used in the Apple II and the Atari Lynx, can perform an indirect jump offset by X. This can be used to perform the above technique with much less setup.
 
<langsyntaxhighlight lang="6502asm">JumpTable:
word PrintChar
word PrintString
Line 86:
 
ldx #$02
JMP (JumpTable,x) ;dereferences to JMP PrintString</langsyntaxhighlight>
 
Your index needs to be a multiple of 2 for this to work, most of the time you'll use <code>ASL A</code> then <code>TAX</code>to double your index prior to performing the indirect jump.
Line 94:
 
Unfortunately, you cannot simply replace "JMP" in the above example with "JSR" as that is not a valid command. What you have to do instead is a bit trickier. One way to accomplish this is with a "Return Table." Let's rewrite JumpTable_Lo and JumpTable_Hi in that format.
<langsyntaxhighlight lang="6502asm">ReturnTable: dw PrintChar-1,WaitChar-1,ReadKeys-1,MoveMouse-1</langsyntaxhighlight>
 
For this example, assume that all of those labels are subroutines that end in an RTS instruction. This method won't work otherwise. Once your return table is defined, you need some sort of variable that allows you to index into that table (i.e. select a subroutine to use).
We'll assume for the code below that the accumulator contains such a variable right now.
 
<langsyntaxhighlight lang="6502asm">
;execution is currently here
JSR UseReturnTable ;the address just after this label is pushed onto the stack.
Line 114:
RTS ;this "RTS" actually takes you to the desired subroutine.
;The top two bytes of the stack are popped, the low byte is incremented by 1,
;and this value becomes the new program counter.</langsyntaxhighlight>
 
Now all of that was a bit confusing wasn't it? Essentially this "abuses" the following concepts:
Line 139:
In addition, for nearby subroutines, <code>BSR</code> can be used in place of <code>JSR</code> to save data. There is also <code>BRA</code> (branch always) for unconditional local jumping, which the original 6502 didn't have.
 
<langsyntaxhighlight lang="68000devpac">bra foo
nop ;execution will never reach here
foo:
Line 146:
BGE D1_Is_Greater_Than_Or_Equal_To_D0
; your code for what happens when D1 is less than D0 goes here
D1_Is_Greater_Than_Or_Equal_To_D0:</langsyntaxhighlight>
 
===Absolute Jumps===
The 68000 has <code>JMP</code> and <code>JSR</code> for absolute jumping and function calls, respectively. When you <code>JSR</code>, the next <code>RTS</code> you encounter will return the program counter to just after the <code>JSR</code> statement you came from, provided that the return address is still on top of the stack when the <code>RTS</code> is executed. The CPU assumes that the top 4 bytes of the stack are the correct return address, and has no way of knowing if it's wrong. A sequence like this will likely result in a crash or otherwise undefined behavior:
<langsyntaxhighlight lang="68000devpac">JSR foo:
 
; this is somewhere far away from the JSR above
foo:
MOVE.L D0,(SP)+
RTS ;the CPU will "return" to the value that was in D0, not the actual return address.</langsyntaxhighlight>
 
This can be abused for our benefit, allowing the programmer to select a subroutine from a list of functions. This trick goes by many names, some of the more common ones are "RTS Trick" or "Ret-poline" (a portmanteau of x86's <code>RET</code> and trampoline. <code>RET</code> is called <code>RTS</code> on the 6502 and 68000)
 
<langsyntaxhighlight lang="68000devpac">;For this example, we want to execute "ReadJoystick"
MOVE.W D0,#1
JSR Retpoline
Line 175:
 
SubroutineTable:
DC.L MoveMouse, DC.L ReadJoystick, DC.L ReadKeyboard, DC.L PrintString</langsyntaxhighlight>
 
===Indirect Jumps===
Line 184:
 
<code>JCXZ</code> will jump only if the <code>CX</code> register equals 0. This is useful for skipping a loop if <code>CX</code> (the loop counter) is already zero.
<langsyntaxhighlight lang="asm">JCXZ bar
foo:
LOOP foo ;subtract 1 from CX, and if CX is still nonzero jump back to foo
bar:</langsyntaxhighlight>
 
<code>LOOP label</code> is the equivalent of <code>DEC CX JNZ label</code> and, as the name implies, is used for looping. You might have heard that <code>DEC CX JNZ label</code> should always be used over <code>LOOP label</code> for better performance. This is only true on later x86-based CPUs - the reasons for <code>LOOP</code>'s poor performance weren't present on the 8086, and as such it's better to use there.
Line 195:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">
procedure Goto_Test is
begin
Line 245:
end Goto_Test;
 
</syntaxhighlight>
</lang>
 
=={{header|ARM Assembly}}==
Line 264:
ARM is very unique in that the program counter can be directly loaded as well as influenced by branching. You need to be very careful when doing this, because loading a value into <code>PC</code> that isn't a multiple of 4 will crash the CPU.
 
<langsyntaxhighlight ARMlang="arm Assemblyassembly">MOV PC,R0 ;loads the program counter with the value in R0. (Any register can be used for this)
LDR PC,[R0] ;loads the program counter with the 32-bit value at the memory location specified by R0</langsyntaxhighlight>
 
This sequence of commands can store registers onto the stack, retrieve them, and return all at once. For this to work properly the only difference in the choice of registers can be that you push <code>LR</code> and pop <code>PC</code>.
<langsyntaxhighlight ARMlang="arm Assemblyassembly">PUSH {R0-R12,LR}
POP {R0-R12,PC}</langsyntaxhighlight>
 
If you don't have access to unified syntax, the above will only work in THUMB mode. For 32-bit ARM, you may have to use
<langsyntaxhighlight ARMlang="arm Assemblyassembly">STMFD sp!,{r0-r12,lr}
LDMFD sp!,{r0-r12,pc}</langsyntaxhighlight>
 
=={{header|Arturo}}==
Line 282:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="ahk">; Define a function.
function()
{
Line 318:
Suspended
 
*/</langsyntaxhighlight>
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 GOTO 100: REM jump to a specific line
20 RUN 200: REM start the program running from a specific line</langsyntaxhighlight>
 
Some versions of basic allow line labels to be used. Here we jump to a label:
{{works with|BaCon}}
{{works with|QuickBASIC}}
<syntaxhighlight lang ="qbasic">GOTO mylabel</langsyntaxhighlight>
==={{header|Applesoft BASIC}}===
caveat: http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html
<langsyntaxhighlight ApplesoftBASIClang="applesoftbasic"> 0 REM GOTO
100 GOTO 110 : REM JUMP TO A SPECIFIC LINE
110 RUN 120 : REM START THE PROGRAM RUNNING FROM A SPECIFIC LINE
Line 351:
280 STOP : REM BREAK THE PROGRAM
290 END : REM END THE PROGRAM
300 GOTO : REM NO LINE NUMBER, JUMPS TO LINE 0</langsyntaxhighlight>
<langsyntaxhighlight ApplesoftBASIClang="applesoftbasic">CONT : REM CONTINUE, JUMP BACK TO WHERE THE PROGRAM STOPPED</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">10 GOTO 100 ! jump to a specific line
20 RUN 200 ! start the program running from a specific line</langsyntaxhighlight>
 
==={{header|Run BASIC}}===
<langsyntaxhighlight lang="runbasic">for i = 1 to 10
if i = 5 then goto [label5]
next i
Line 378:
 
[finish]
print "done"</langsyntaxhighlight>
 
=={{header|BASIC256}}==
Line 384:
{{works with|QBasic}}
{{works with|Yabasic}}
<langsyntaxhighlight lang="freebasic">print "First line."
gosub sub1
print "Fifth line."
Line 405:
Finished:
print "... with goto and gosub, thankfully."
end</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FutureBasic.</pre>
Line 411:
=={{header|C}}==
C has <code>goto LABEL</code> keyword.
<langsyntaxhighlight lang="c"> if (x > 0) goto positive;
else goto negative;
 
Line 421:
 
both:
...</langsyntaxhighlight>
The label must be literal, not computed at run time. This won't work:
<langsyntaxhighlight lang="c">goto (x > 0 ? positive : negative);</langsyntaxhighlight>
<!-- Except if you're using GCC extensions, when you can do something that's very similar to that. Scary stuff! -->
You can <code>goto</code> ''almost'' anywhere inside the same function, but can't go across function boundaries. It's sometimes used to break out of nested loops:<langsyntaxhighlight lang="c">for (i = 0; ...) {
for (j = 0; ...) {
if (condition_met) goto finish;
}
}</langsyntaxhighlight>although you can (not that you ''should'') jump into a loop, too:<langsyntaxhighlight lang="c"> goto danger;
for (i = 0; i < 10; i++) {
danger: /* unless you jumped here with i set to a proper value */
printf("%d\n", i);
}</langsyntaxhighlight>
For unwrapping call stack and go back up to a caller, see [[Exceptions#C|longjmp example]]; more powerful, but more expensive and complicated, is POSIX [[Exceptions/Catch an exception thrown in a nested call#C|ucontext]].
The best application for goto is error handling, this simplifies the resource clean up of a large function.
Line 440:
 
 
<syntaxhighlight lang="c">
<lang c>
char *str;
int *array;
Line 466:
...// read in the csv file and convert to integers
 
</syntaxhighlight>
</lang>
 
 
<syntaxhighlight lang="c">
<lang c>
char *str;
int *array;
Line 495:
exit:
return;
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
Like C, C# also has a <code>goto LABEL</code> keyword. This section is partly copied from the section on C, since both languages share common syntax.
<langsyntaxhighlight lang="csharp">if (x > 0) goto positive;
else goto negative;
 
Line 509:
 
both:
...</langsyntaxhighlight>
The label must be literal, not computed at run time. This won't work:
<langsyntaxhighlight lang="csharp">goto (x > 0 ? positive : negative);</langsyntaxhighlight>
 
You can <code>goto</code> ''almost'' anywhere inside the same method, but can't go across method boundaries. It's sometimes used to break out of nested loops:
<langsyntaxhighlight lang="csharp">for (i = 0; ...) {
for (j = 0; ...) {
if (condition_met) goto finish;
}
}</langsyntaxhighlight>
The label must be in scope, so you cannot jump into a loop. This will not compile:
<langsyntaxhighlight lang="csharp">goto danger;
for (i = 0; i < 10; i++) {
danger:
Console.WriteLine(i);
}</langsyntaxhighlight>
 
In C#, you can also <code>goto</code> a label from within any section of a try .. catch block. However, it is not permitted to jump out of a finally block.
<langsyntaxhighlight lang="csharp">int i = 0;
tryAgain:
try {
Line 538:
finally {
//goto end; //Error
}</langsyntaxhighlight>
 
Another usage of <code>goto</code> is to jump between <code>case</code> labels inside a <code>switch</code> statement:
<langsyntaxhighlight lang="csharp">public int M(int n) {
int result = 0;
switch (n) {
Line 555:
}
return result;
}</langsyntaxhighlight>
 
=={{header|C++}}==
Like C, C++ also has a <code>goto LABEL</code> statement which can be used to jump within a function.
 
<langsyntaxhighlight Cpplang="cpp">#include <iostream>
#include <utility>
 
Line 605:
cout << "k = " << k << "\n"; // k was never initialized, accessing it is undefined behavior
}</langsyntaxhighlight>
 
{{out}}
Line 619:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
PROGRAM-ID. JUMPS-PROGRAM.
* Nobody writes like this, of course; but...
Line 642:
AN-ARBITRARY-PARAGRAPH.
DISPLAY 'Edsger who now?'
GO TO SOME-OTHER-PARAGRAPH.</langsyntaxhighlight>
{{out}}
<pre>Edsger who now?
Line 649:
'''COBOL''' also supports computed go to phrases, given a list of labels (paragraph names) and an integer index into that list.
 
<langsyntaxhighlight COBOLlang="cobol">01 province pic 99 value 2.
GO TO quebec, ontario, manitoba DEPENDING ON province</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
In Common Lisp you can jump anywhere inside a tagbody.
<langsyntaxhighlight lang="lisp">
(tagbody
beginning
Line 668:
(sleep 1)
(go middle))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 683:
=={{header|Computer/zero Assembly}}==
A <tt>JMP</tt> (jump) instruction can transfer control to any point in memory. Its target can be modified at run time, if required, by using instruction arithmetic:
<langsyntaxhighlight lang="czasm"> LDA goto
SUB somewhere
ADD somewhereElse
STA goto
goto: JMP somewhere</langsyntaxhighlight>
By the time execution reaches the instruction labelled <tt>goto</tt>, that instruction has become <tt>JMP somewhereElse</tt>. (This kind of coding does not, however, necessarily make the flow of control easier to follow.)
 
Line 694:
 
=={{header|DCL}}==
<langsyntaxhighlight DCLlang="dcl">$ return ! ignored since we haven't done a gosub yet
$
$ if p1 .eqs. "" then $ goto main
Line 764:
$ sub4: subroutine
$ exit
$ endsubroutine</langsyntaxhighlight>
{{out}}
<pre>$ @jump_anywhere
Line 865:
Déjà Vu supports continuations:
 
<langsyntaxhighlight lang="dejavu">example:
!print "First part"
yield
Line 872:
local :continue example
!print "Interrupted"
continue</langsyntaxhighlight>
{{out}}
<pre>First part
Line 883:
Every function/procedure must have your own labels, globals labels won't work in local functions.
The label must be literal, not computed at run time.
<syntaxhighlight lang="delphi">
<lang Delphi>
var
x: Integer = 5;
Line 905:
readln;
end.
</syntaxhighlight>
</lang>
Although you can (not that you should) jump into a loop, too. If you use a loop variable, it will start with its value.
If the loop variable has a value out range loop limits, then you cause a intinity loop.
<syntaxhighlight lang="delphi">
<lang Delphi>
label
inloop, outloop;
Line 927:
readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 942:
This won't work.
 
<syntaxhighlight lang="delphi">
<lang Delphi>
label
tryAgain, finished;
Line 960:
readln;
end.
</syntaxhighlight>
</lang>
=={{header|Erlang}}==
Jumps are limited to [[Exceptions]].
Line 966:
=={{header|ERRE}}==
ERRE has a GOTO statement in the form GOTO <label>. <label> is a numercic string and must be declared. ERRE GOTO is local only: you can jump only within the same procedure or within the main program. In the following example there are two errors:
<syntaxhighlight lang="text">
PROGRAM GOTO_ERR
 
Line 986:
IF J<>0 THEN GOTO 100 END IF
END PROGRAM
</syntaxhighlight>
</lang>
You can't jump from procedure P1 to procedure P2 or from main to procedure P2 (label 99). Jump to label 100 is allowed (within main program).
 
Line 996:
A continuation is conceptually simple in Factor. It is a tuple consisting of slots for the five stacks that make up the entire execution context: data stack, call stack, retain stack, name stack, and catch stack. To create a continuation from the current execution context, use <code>current-continuation</code>.
 
<langsyntaxhighlight lang="factor">USING: continuations prettyprint ;
 
current-continuation short.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,006:
Following is a simple example where we reify a continuation before attempting to divide 1 by 0. Note the current execution context (a <code>continuation</code> object) is placed on the top of the data stack inside the <code>callcc0</code> quotation which <code>continue</code> then reifies.
 
<langsyntaxhighlight lang="factor">USING: continuations math prettyprint ;
 
[ 10 2 / . continue 1 0 / . ] callcc0</langsyntaxhighlight>
{{out}}
<pre>
Line 1,015:
 
We can even reify a continuation while taking an object back with us.
<langsyntaxhighlight lang="factor">USING: continuations kernel math ;
 
[ 10 2 / swap continue-with 1 0 / ] callcc1</langsyntaxhighlight>
 
{{out}}
Line 1,053:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">\ this prints five lines containing elements from the two
\ words 'proc1' and 'proc2'. gotos are used here to jump
\ into and out of the two words at various points, as well
Line 1,081:
 
5 1 proc2
bye</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight lang="forth">line 1 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 2 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 3 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 4 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 5 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5</langsyntaxhighlight>
 
 
<langsyntaxhighlight lang="forth">\ this is congruent to the previous demonstration, employing
\ a data structure to store goto/jump addresses instead of
\ separate variables, and two additional words 'mark_goto' and
Line 1,120:
 
: go 5 1 6 goto ; go
bye</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight lang="forth">line 1 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 2 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 3 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 4 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 5 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5</langsyntaxhighlight>
 
 
<langsyntaxhighlight lang="forth">\ works with ficl, pfe, gforth, bigforth, and vfx.
\ swiftforth may crash, iforth does not function.
 
Line 1,167:
5 1 go
 
bye</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight lang="forth">iteration 1 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 2 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 3 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 4 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 5 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8</langsyntaxhighlight>
 
 
Line 1,180:
Although Forth was designed to be a structured programming language it is simple to add a generic jump anywhere.
The Forth dictionary of WORDs is essentially a linked list of labels. We can find the execution token for any label in the dictionary with the ['] operator and jump to it with the keyword EXECUTE.
<langsyntaxhighlight lang="forth">: GOTO ['] EXECUTE ;</langsyntaxhighlight>
 
TEST
<langsyntaxhighlight lang="forth">: WORLD ." World!" ;
: HELLO ." Hello" ;
 
GOTO CR GOTO HELLO GOTO SPACE GOTO WORLD
Hello World!</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 1,229:
The following program demonstrate the use of the various 'Goto' constructs in the -lang fb dialect only:
 
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' compiled with -lang fb (the default) and -e switches
Line 1,278:
Print
Print "Pres any key to quit"
Print</langsyntaxhighlight>
 
{{out}}
Line 1,297:
 
All that said, don't use spaghetti code -- use functions. You can still by spark plugs for a 1985 Yugo, but why?
<langsyntaxhighlight lang="futurebasic">
include "ConsoleWindow"
 
Line 1,318:
print "... with goto and gosub, thankfully."
end
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,336:
 
For example:
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,356:
k++
fmt.Println(k)
}</langsyntaxhighlight>
 
{{out}}
Line 1,372:
4
</pre>
::<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,406:
func init() {
fmt.Println("run first")
}</langsyntaxhighlight>
 
=={{header|Harbour}}==
Line 1,417:
 
First some boilerplate, where we define labels and goto "operator".
<langsyntaxhighlight Haskelllang="haskell">import Control.Monad.Cont
 
data LabelT r m = LabelT (ContT r m ())
Line 1,429:
 
runProgram :: Monad m => ContT r m r -> m r
runProgram program = runContT program return</langsyntaxhighlight>
 
Here is example of using labels in IO:
<langsyntaxhighlight Haskelllang="haskell">main = runProgram $
do
start <- label
Line 1,440:
then do lift $ putStrLn "Name can't be empty!"
goto start
else lift $ putStrLn ("Hello, " ++ name)</langsyntaxhighlight>
 
{{Out}}
Line 1,452:
 
We can build tiny EDSL to get rid of explicit lifting and to add refferences:
<langsyntaxhighlight Haskelllang="haskell">import Data.IORef
 
readInt = lift $ readLn >>= newIORef
get ref = lift $ readIORef ref
set ref expr = lift $ modifyIORef ref (const expr)
output expr = lift $ putStrLn expr</langsyntaxhighlight>
 
and implement famous Euclid's algorithm as an imperative program:
<langsyntaxhighlight Haskelllang="haskell">gcdProg = runProgram $ callCC $ \exit -> -- <--------+
do -- |
start <- label -- <-----+ |
Line 1,478:
when (n > m) $ set nr (n - m) -- |
when (m > n) $ set mr (m - n) -- |
goto loop -- ---+</langsyntaxhighlight>
 
{{Out}}
Line 1,492:
In native Haskell such flow jumps are done by function calls:
 
<langsyntaxhighlight Haskelllang="haskell">gcdFProg = start
where
start = do
Line 1,508:
| n == m = n
| n < m = loop n (m-n)
| n > m = loop (n-m) m</langsyntaxhighlight>
 
Or without explicit recursion and branching:
 
<langsyntaxhighlight Haskelllang="haskell">import Control.Applicative
import Control.Monad.Trans.Maybe
 
Line 1,523:
process = gcd <$> (lift readLn >>= exitOn 0) <*> lift readLn
 
exitOn n x = if x == n then empty else pure x</langsyntaxhighlight>
 
=={{header|i}}==
<langsyntaxhighlight lang="i">//'i' does not have goto statements, instead control flow is the only legal way to navigate the program.
concept there() {
print("Hello there")
Line 1,554:
//Move to the code contained in the 'there' function.
there()
}</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,560:
 
For example:
<langsyntaxhighlight lang="j">F=: verb define
smoutput 'Now we are in F'
G''
Line 1,573:
F''
Now we are in F
Now we are in G</langsyntaxhighlight>
 
J also supports jumps to labels within a definition:
 
<langsyntaxhighlight lang="j">H=: verb define
smoutput 'a'
label_b.
Line 1,594:
c
g
e</langsyntaxhighlight>
 
=={{header|Java}}==
The closest thing that Java has to a "goto" is labelled loops:
<langsyntaxhighlight lang="java">loop1: while (x != 0) {
loop2: for (int i = 0; i < 10; i++) {
loop3: do {
Line 1,615:
}
//loop1 calculations executed after loop2 is done or if the break is executed, skipped if the continue is executed
}</langsyntaxhighlight>
 
It's frowned upon to use exceptions for non-exceptional paths, so get ready to frown at this fizz-buzz player that makes use of <tt>try/catch/finally</tt> to jump to the right places for printing:
<langsyntaxhighlight lang="java">
public class FizzBuzzThrower {
public static void main( String [] args ) {
Line 1,636:
}
}
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
Line 1,644:
jq supports named labels ("label $NAME") and break statements ("break $NAME"). Usage
follows the pattern:
<langsyntaxhighlight lang="jq">label $out | ... break $out ...</langsyntaxhighlight>
 
When, during program execution, a "break" statement is encountered, the program
Line 1,655:
Break statements are used to interrupt a generator. For example, to emit
the least positive integer satisfying sin(1/N) == (1/N) using IEEE 64-bit arithmetic:
<langsyntaxhighlight lang="jq">label $out | 1e7 | while(true; .+1) | if (1/.) | . == sin then (., break $out) else empty end</langsyntaxhighlight>
 
Here, the "while" filter is an unbounded generator. The answer is 46530688.
Line 1,663:
Indeed, a better way to solve the type of problem mentioned above is by using one of the jq-provided control structures.
For example, the above problem can be solved more succinctly and transparently using until/2:
<langsyntaxhighlight lang="jq">1e7 | until(1/. | . == sin; .+1)</langsyntaxhighlight>
 
=={{header|Julia}}==
 
Julia provides the @goto and @label macros for goto within functions but these cannot be used at the global level or to jump from one function to another. The macros can however be used for a typical use of @goto -- jumping out to a specific level from nested loops within a function.
<langsyntaxhighlight lang="julia">
function example()
println("Hello ")
Line 1,676:
println("world")
end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
Line 1,684:
 
For example:
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun main(args: Array<String>) {
Line 1,699:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,723:
 
* Lingo supports "unwinding the call stack" via the abort command that exits the current call stack:
<langsyntaxhighlight lang="lingo">on foo
abort()
end
Line 1,730:
foo()
put "This will never be printed"
end</langsyntaxhighlight>
 
* Rarely used, but Lingo supports saving a continuation via the play ... play done construct:
<langsyntaxhighlight lang="lingo">on testPlayDone ()
 
-- Start some asynchronous process (like e.g. a HTTP request).
Line 1,744:
-- The following will be executed only after 'play done' was called in the asynchronous process code
put "Done. The asynchronous process returned the result:" && _global.result
end</langsyntaxhighlight>
 
=={{header|Logtalk}}==
Line 1,751:
=={{header|Lua}}==
Lua 5.2 introduced a <code>goto</code> statement along with labels. It was somewhat controversially implemented instead of a <code>continue</code> keyword, but it is more flexible and supports other types of jumps as well. <code>goto</code> only supports statically-defined labels.
<langsyntaxhighlight lang="lua">-- Forward jump
goto skip_print
print "won't print"
Line 1,788:
end
print "not found"
::exit::</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 1,797:
After Else and Then we can place number to jump (same as Goto number). Goto can't take variable. We can use On Goto to use an expression to choose label (we can place numbers or named labels)
===Using GOTO and GOSUB===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Alfa {
Line 1,831:
}
Checkit
</syntaxhighlight>
</lang>
 
===Simulate Run Basic Entry===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module LikeRunBasic {
for i = 1 to 10
Line 1,858:
}
LikeRunBasic
</syntaxhighlight>
</lang>
 
===Simulate Go Entry===
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module LikeGo {
\\ simulate Go for
Line 1,944:
LikeGo_No_Labels
 
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,953:
=={{header|MBS}}==
 
<syntaxhighlight lang MBS="mbs">goto mylabel;</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
 
<syntaxhighlight lang="text">БП XX</langsyntaxhighlight>
 
'''XX''' is any address.
Line 1,972:
Most of the time you won't be jumping to a specific address. You can place a label before any instruction, and a jump to that label is the same as a jump to the address of that instruction.
 
<langsyntaxhighlight lang="mips">
j GoHere ;the assembler will convert this label to a constant memory address for us.
 
Line 1,979:
 
GoHere:
addiu $t0,1 ;this instruction is the first one executed after jumping.</langsyntaxhighlight>
 
 
Line 1,989:
 
Some interpreters will allow you jump between blocks of the same depth. Others will let you leave, but not enter, a block.
<langsyntaxhighlight MUMPSlang="mumps"> ;Go to a label within the program file
Goto Label
;Go to a line below a label
Line 2,005:
. Goto Out
Out Quit
Goto NoNo+2</langsyntaxhighlight>
 
=={{header|Neko}}==
Line 2,011:
Neko supports colon terminated labels, and a builtin '''$goto(label)'''. This builtin is special in that the label argument is not dereferenced as a normal Neko expression, but specifically as a label.
 
<langsyntaxhighlight ActionScriptlang="actionscript">$print("start\n")
$goto(skip)
$print("Jumped over")
skip:
$print("end\n")</langsyntaxhighlight>
 
The NekoVM keeps exception and call stacks in sync when jumping across boundaries to code blocks, but may not include some initiating side effects. For instance, jumping past a '''try''' phrase into the middle of the code block (where the try is not evaluated) will not catch the '''catch''' of the try catch pair. There are other cases where '''$goto()''' can cross semantic boundaries; a practice best avoided.
Line 2,021:
=={{header|Nim}}==
Nim has exceptions and labelled breaks:
<langsyntaxhighlight lang="nim">
block outer:
for i in 0..1000:
for j in 0..1000:
if i + j == 3:
break outer</langsyntaxhighlight>
 
=={{header|Oforth}}==
Line 2,038:
 
Some portion of code can be restarted (as recursion) and skipped (as continuation).
<langsyntaxhighlight lang="scheme">
; recursion:
(let loop ((n 10))
Line 2,052:
 
(print "ok.")
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
Line 2,061:
=={{header|Perl}}==
Perl's <code>goto LABEL</code> and <code>goto EXPR</code> are a little too powerful to be safe. Use only under extreme duress (actually, not even then). <code>goto &SUB</code> is esoteric but much more innocuous and can occasionally be handy.
<langsyntaxhighlight lang="perl">sub outer {
print "In outer, calling inner:\n";
inner();
Line 2,095:
 
print "\nCalling inner:\n";
inner(); # will die</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,101:
Using this 'baroque' syntax is viewed as an effective means of dissuading novices from adopting goto as a weapon of choice.
Note that pwa/p2js does not support #ilASM{} at all. <!--(notonline)-->
<langsyntaxhighlight Phixlang="phix">#ilASM{ jmp :%somelabel }
...
#ilASM{ :%somelabel }</langsyntaxhighlight>
The above shows a global label, which can only be declared in top-level code, not inside a routine, and can be referenced from anywhere.
 
Local labels are declared with a double colon (::) and referenced with a single colon
<langsyntaxhighlight Phixlang="phix">#ilASM{ jmp :local
...
::local }</langsyntaxhighlight>
They can only be referenced at the top level from within the same #ilASM construct, or anywhere within the routine where they are defined.
Phix also supports anonymous local labels
<langsyntaxhighlight Phixlang="phix">#ilASM{ jmp @f
...
@@:
...
jle @b }</langsyntaxhighlight>
There are also optional (global) labels:
<langsyntaxhighlight Phixlang="phix">#ilASM{ call :!optlbl
[32]
pop eax
Line 2,131:
push qword[rsp]
[]
ret }</langsyntaxhighlight>
These are obviously more useful when the reference and declaration are in separate files: if the file containing the declaration is not included, the reference quietly resolves to 0.
The above code shows how to duplicate the return address and discard on return, so that execution carries on at the next instruction if the definition is not present.
Line 2,139:
 
Lastly (for completeness) there are init labels, defined using
<syntaxhighlight lang Phix="phix">#ilASM{ :>init }</langsyntaxhighlight>
These are used by the VM (eg builtins\VM\pStack.e declares :>initStack) to specify initialisation code which must be run at startup. They can also be called like a normal global label.
 
Line 2,150:
 
There is no 'go' or 'goto' function in PicoLisp, but it can be emulated with normal list processing functions. This allows "jumps" to arbitrary locations within (the same or other) functions. The following example implements a "loop":
<langsyntaxhighlight PicoLisplang="picolisp">(de foo (N)
(prinl "This is 'foo'")
(printsp N)
(or (=0 (dec 'N)) (run (cddr foo))) )</langsyntaxhighlight>
Test:
<pre>: (foo 7)
Line 2,168:
<br>In structured programming, the only place where a <code>goto</code> can be is inside <code>on</code> units to handle exceptions without recursion.
 
<syntaxhighlight lang ="pli">on conversion goto restart;</langsyntaxhighlight>
 
=={{header|PL/SQL}}==
PL/SQL supports both GOTOs and structured exception handling:
<langsyntaxhighlight PLlang="pl/SQLsql">DECLARE
i number := 5;
divide_by_zero EXCEPTION;
Line 2,197:
DBMS_OUTPUT.put_line( 'Finally' );
END;
/</langsyntaxhighlight>
{{out}}
<pre>startLoop
Line 2,213:
current loop. The syntax for a label is as follows (this example shows a
label in a While loop):
<syntaxhighlight lang="powershell">
<lang PowerShell>
:myLabel while (<condition>) { <statement list>}
</syntaxhighlight>
</lang>
The label is a colon followed by a name that you assign. The label must be
the first token in a statement, and it must be followed by the looping
Line 2,229:
This schematic example has a While statement with a For statement:
<syntaxhighlight lang="powershell">
<lang PowerShell>
:myLabel while (<condition 1>)
{
Line 2,239:
}
$a = $c # A statement after the labeled While-loop
</syntaxhighlight>
</lang>
 
If condition 2 evaluates to True, the execution of the script skips down
Line 2,247:
You can nest many labeled loops, as shown in the following schematic
example.
<syntaxhighlight lang="powershell">
<lang PowerShell>
:red while (<condition1>)
{
Line 2,263:
}
# After "red" loop
</syntaxhighlight>
</lang>
If the $b variable evaluates to True, execution of the script resumes
after the loop that is labeled "red". If the $c variable evaluates to
Line 2,276:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">OnErrorGoto(?ErrorHandler)
OpenConsole()
Gosub label4
Line 2,306:
 
ErrorHandler:
PrintN(ErrorMessage()) : Goto label4</langsyntaxhighlight>
{{out}}
<pre>eins zwei eins zwei eins zwei - drei -</pre>
Line 2,314:
 
The "goto" module was an April Fool's joke, published on 1st April 2004. Yes, it works, but it's a joke nevertheless. Please don't use it in real code! For those who like computer languages with a sense of humour it can be downloded [http://entrian.com/goto/goto-1.0.tar.gz here]. It is well documented and comes with many examples. My favorite:
<syntaxhighlight lang="python">
<lang Python>
# Example 2: Restarting a loop:
from goto import goto, label
Line 2,328:
goto .start
print output, "\n"
</syntaxhighlight>
</lang>
It goes the extra mile and adds a <code>comefrom</code> keyword. This should be used only if you are evil and proud of it. It is reported to have caused maintenance programmers to have so strongly believed themselves insane that it became so. They are now under strong medication in padded cells. Basically whenever the code passes a label it jumps to the comefrom point. For best results I advise writing the comefrom code as far as possible from the label and using no comments near the label.
 
Line 2,336:
{{works with|BASIC256}}
{{works with|Yabasic}}
<langsyntaxhighlight lang="qbasic">
PRINT "First line."
GOSUB sub1
Line 2,358:
Finished:
PRINT "... with goto and gosub, thankfully."
END</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FutureBasic.</pre>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
<lang QB64>
' Jumps in QB64 are inherited by Qbasic/ QuickBasic/ GWBasic
' here a GOTO demo of loops FOR-NEXT and WHILE-WEND
Line 2,406:
Q = Q + 1
GoTo 2
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
Line 2,432:
To achieve this, <code>]'[</code> advances the offset on top of the return stack, like <code>]else[</code>, but also puts a copy of the item skipped over on the stack. (i.e. the nest following <code>jump-into</code> in the example) It puts this, with an offset of 0, onto the call stack (with <code>]do[</code>. Then it uses <code>' ]else[ swap of</code> to build a nest of the specified number of instances of <code>]else[</code>, and puts that on the call stack, so that it will be evaluated after exiting <code>jump-to</code>, and increment the offset of the top of the call stack (i.e. the nest following <code>jump-to</code>) that number of times.
 
<langsyntaxhighlight Quackerylang="quackery"> [ ]'[ ]do[ ' ]else[ swap of ]do[ ] is jump-into ( n --> )
 
5 jump-into
Line 2,438:
( 0 1 2 3 4 offsets of each item )
( 5 6 7 8 9 from start of nest )
2 echo 3 echo again ]</langsyntaxhighlight>
 
{{out}}
Line 2,448:
 
As a little example:
<langsyntaxhighlight lang="racket">#lang racket
(define (never-divides-by-zero return)
(displayln "I'm here")
Line 2,460:
; I'm here
; "Leaving" (because that's what the function returns)
</syntaxhighlight>
</lang>
 
Here, return is the program continuation being passed to the function, when it is called, the string "Leaving" i the result of the function and the following code is never executed.
Line 2,467:
Where we generate elements of a list one at a time:
 
<langsyntaxhighlight lang="racket">#lang racket
;; [LISTOF X] -> ( -> X u 'you-fell-off-the-end-off-the-list)
(define (generate-one-element-at-a-time a-list)
Line 2,488:
;; time to return the generator
generator)
</syntaxhighlight>
</lang>
 
=={{header|Relation}}==
Line 2,497:
Retro allows the user to jump to any address.
 
<langsyntaxhighlight Retrolang="retro">:foo #19 &n:inc \ju...... #21 ;</langsyntaxhighlight>
 
When executed, the stack will contain 20; control does not return to the '''foo''' function.
Line 2,505:
 
===Label-based jumps===
<syntaxhighlight lang="raku" perl6line> outer-loop: loop {
inner-loop: loop {
# NYI # goto inner-loop if rand > 0.5; # Hard goto
Line 2,517:
LEAVE { say "Leaving outer loop block" }
LAST { say "Ending outer loop" }
}</langsyntaxhighlight>
Produces random output, but here's a representative run:
 
Line 2,533:
 
Continuations in Raku are currently limited to use in generators via the gather/take model:
<syntaxhighlight lang="raku" perl6line> my @list = lazy gather for ^100 -> $i {
if $i.is-prime {
say "Taking prime $i";
Line 2,540:
}
say @list[5];</langsyntaxhighlight>
This outputs:
 
Line 2,556:
 
Exceptions are fairly typical in Raku:
<syntaxhighlight lang="raku" perl6line> die "This is a generic, untyped exception";</langsyntaxhighlight>
Will walk up the stack until either some `CATCH` block intercepts the specific exception type or we exit the program.
 
But if a failure should be recoverable (e.g. execution might reasonably continue along another path) a failure is often the right choice. The fail operator is like "return", but the returned value will only be valid in boolean context or for testing definedness. Any other operation will produce the original exception with the original exception's execution context (e.g. traceback) along with the current context.
<syntaxhighlight lang="raku" perl6line> sub foo() { fail "oops" }
my $failure = foo;
say "Called foo";
say "foo not true" unless $failure;
say "foo not defined" unless $failure.defined;
say "incremented foo" if $failure++; # exception</langsyntaxhighlight>
Produces:
 
Line 2,579:
 
However, an exception can `.resume` in order to jump back to the failure point (this is why the stack is not unwound until after exception handling).
<syntaxhighlight lang="raku" perl6line> sub foo($i) {
if $i == 0 {
die "Are you sure you want /0?";
Line 2,593:
CATCH {
when ~$_ ~~ m:s/Are you sure/ { .resume; #`(yes, I'm sure) }
}</langsyntaxhighlight>
This code raises an exception on a zero input, but then resumes execution, divides be zero and then raises a divide by zero exception which is not caught:
 
Line 2,610:
<br>as long as the '''END''' or the '''DO''' loop isn't executed.
<br>The following used PC/REXX to illustrate this example.
<langsyntaxhighlight lang="rexx">/*REXX pgm demonstrates various jumps (GOTOs). In REXX, it's a SIGNAL. */
say 'starting...'
signal aJump
Line 2,628:
cJump: say 'and here we are at cJump.'
exit
end /*k*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,655:
As mentioned above some implementations may have restrictions on that.
This Signal jumps into a Do loop inside a Procedure:
<langsyntaxhighlight lang="rexx">i=13
signal label
say 'This is never executed'
Line 2,665:
End
Return
real_start:</langsyntaxhighlight>
Without the 'Signal real_start' which leads us out of the control structure the program would end with a syntax error when encountering the End correponding to the Do.
 
I recommend to use Signal only for condition handling and 'global' jumps to labels that are not within some structured constructs such as Do...End
An example:
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 12.12.2012 Walter Pachl
**********************************************************************/
Line 2,686:
Say 'argument must be a or b or omitted'
Exit
eoj: say 'here we could print statistics'</langsyntaxhighlight>
This can be useful when the different parts of the program span a few pages.
Also a Signal eoj in order to Exit from any point in the program to some final activities can be useful.
Line 2,698:
====Simple====
A simple example of labels and goto:
<langsyntaxhighlight lang="robotic">
. "The label 'touch' is used to let the player touch the robot"
. "to execute the following"
Line 2,712:
* "Label B was reached"
end
</syntaxhighlight>
</lang>
 
It prints "Label B was reached", skipping "label_a" entirely.
Line 2,718:
====Conditional====
This will jump to a given label, depending on the condition of "local1":
<langsyntaxhighlight lang="robotic">
set "local1" to 2
end
Line 2,733:
* "Label B was reached"
end
</syntaxhighlight>
</lang>
 
Since "local1" equals 2, it prints "Label B was reached".
Line 2,740:
When you goto a label, it chooses the top-most label name in the code.
However, with the use of "zap" and "restore", we can use the same label name multiple times in our code:
<langsyntaxhighlight lang="robotic">
end
: "touch"
Line 2,754:
restore "label_a" 1
end
</syntaxhighlight>
</lang>
 
When the first label is reached, it zaps "label_a" once. This allows us to reach the second label below. Conversely, restoring "label_a" allows us to go back to the first label once more.
Line 2,761:
With subroutines, we can jump to a given label and come back to where we left off after we called the inital "goto" statement.
To use these, the labels and the string supplied in the "goto" statements must have a number sign (#):
<langsyntaxhighlight lang="robotic">
. "The 'wait' statements are used to demonstrate what is happening"
set "local1" to 1
Line 2,791:
* "Label D was reached"
goto "#top"
</syntaxhighlight>
</lang>
 
The following is printed out in order:
<syntaxhighlight lang="text">
Label A was reached
Done with Label A
Line 2,800:
Label D was reached
Skipped finishing Label B and C
</syntaxhighlight>
</lang>
 
Using "#return", we go back up to the last "goto" statement called. However, using "#top" will ignore all the other subroutines called previously and go straight back to the first "goto" statement that started it all (in this case, goto "#label_b").
Line 2,809:
The next example abuses a continuation to solve [[FizzBuzz#Ruby]]. It is slower and more confusing than an ordinary loop.
{{libheader|continuation}}
<langsyntaxhighlight lang="ruby">require 'continuation' unless defined? Continuation
 
if a = callcc { |c| [c, 1] }
Line 2,830:
puts
c[c, i + 1]
end</langsyntaxhighlight>
 
This code uses the Continuation object <code>c</code> to jump to the top of the loop. For the first iteration, <code>callcc</code> creates <code>c</code> and returns <code>[c, 1]</code>. For later iterations, <code>callcc</code> returns <code>[c, i + 1]</code>. For the last iteration, <code>callcc</code> returns <code>nil</code> to break the loop.
Line 2,840:
In SPL jumps can be non-conditional and conditional.
This is an example of non-conditional jump to label "myLabel":
<langsyntaxhighlight lang="spl">myLabel ->
...
:myLabel</langsyntaxhighlight>
This is an example of conditional jump to label "4", which is done if a=0:
<langsyntaxhighlight lang="spl">4 -> a=0
...
:4</langsyntaxhighlight>
In SPL it is possible not only jump, but also visit a label. "Visiting" a label means that program execution can be returned back to the place from where label was visited.
This is an example of visiting label "label 55":
<langsyntaxhighlight lang="spl">label 55 <->
#.output("1")
:label 55
#.output("2")
<-</langsyntaxhighlight>
and output is:
{{out}}
Line 2,864:
=={{header|SSEM}}==
The SSEM provides two jump instructions: the absolute jump <tt>000 <operand> to CI</tt> and the relative jump <tt>100 Add <operand> to CI</tt>. The operand in both cases is a memory address, whose contents are to be either loaded into the CI (Current Instruction) register or else added to it. Since CI is incremented <i>after</i> an instruction is executed, not before, the value stored at the operand address must be one less than the value we actually want. For example, this code accomplishes a jump to absolute address 20:
<langsyntaxhighlight lang="ssem">10000000000000000000000000000000 0. 1 to CI
11001000000000000000000000000000 1. 19</langsyntaxhighlight>
and this accomplishes a relative jump forward by five words:
<langsyntaxhighlight lang="ssem">10000000000001000000000000000000 0. Add 1 to CI
00100000000000000000000000000000 1. 4</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 2,874:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub jump()
Debug.Print "VBA only allows"
GoTo 1
Line 2,892:
two:
Debug.Print "On <n> GoSub let you jump to the n-th label": Return
End Sub</langsyntaxhighlight>{{out}}
<pre>VBA only allows
jumps in procedures with GoTo
Line 2,913:
If the expression does not evaluate to a line number present in the program, the program continues with the next highest line number or stops if there is no line with a number higher that the expression.<br>
The following program demonstrates this - prompiting the user for a label to goto. (In an expression, <code>?</code> indicates a value should be read from the console, assigning to <code>?</code> prints the assigned value)<br>
<langsyntaxhighlight VTL2lang="vtl2">1010 ?="@1010: Where to goto? ";
1020 #=?
1030 ?="@1030"
Line 2,920:
1060 #=1010
2000 ?="@2000"
2010 ?="Exiting..."</langsyntaxhighlight>
{{out}}
<pre>
Line 2,949:
 
The Fiber class also has a ''try'' method for catching errors (which won't be described here) and a static ''abort'' method which (unless caught) exits the script altogether if an error occurs.
<langsyntaxhighlight lang="ecmascript">var func = Fn.new {
for (i in 1..10) {
if (i == 1) continue // jumps to next iteration when 'i' equals 1
Line 2,972:
System.print("yielding")
fiber.call() // resumes the fiber
return // would exit the module (and script) without error but won't be reached</langsyntaxhighlight>
 
{{out}}
Line 3,015:
{{works with|QBasic}}
{{works with|BASIC256}}
<langsyntaxhighlight lang="freebasic">print "First line."
gosub sub1
print "Fifth line."
Line 3,036:
Finished:
print "... with goto and gosub, thankfully."
end</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FutureBasic.</pre>
Line 3,050:
<code>JR</code> adds a signed displacement byte to the program counter. This takes one less byte to encode than a <code>JP</code> but cannot jump more than 127 bytes forward or 128 bytes backward. The programmer can either specify a constant value as the operand or a labeled section of code. If a label is used, the assembler will calculate the number of bytes between the <code>JR</code> instruction and that label, and will refuse to assemble the code if the label is too far away.
 
<langsyntaxhighlight lang="z80">and &01 ;returns 0 if the accumulator is even and 1 if odd. Also sets the zero flag accordingly.
jr z,SkipOddCode
;whatever you want to do when the accumulator is odd goes here, but it must be 127 bytes or fewer.
SkipOddCode:
;rest of program</langsyntaxhighlight>
 
 
A similar command called <code>DJNZ</code> is often used for looping. It decrements the B register and jumps if and only if B is nonzero. If B is zero, the <code>DJNZ</code> will do nothing and execution will move past it. Like all other jumping commands discussed so far, <code>DJNZ</code> will not alter the processor flags in any way. The same distance restriction of <code>JR</code> also applies to <code>DJNZ</code>. This is the equivalent of the <code>LOOP</code> instruction in [[x86 Assembly]].
 
<langsyntaxhighlight lang="z80">ld b,25
loop:
djnz loop ;loop 25 times.</langsyntaxhighlight>
 
 
Line 3,069:
<code>JR</code>,<code>JP</code>,<code>CALL</code>, and <code>RET</code> can be made conditional based on which flags are set. Essentially these are like <code>if</code> statements in a high-level language. If the condition specified by the command is not met, the command will have no effect; otherwise it will result in a jump.
 
<langsyntaxhighlight lang="z80">ret po ;return from subroutine if overflow has not occurred or the bit parity has an odd number of 1s.
call c, foo ;call "foo" if and only if the carry is set.
jp m,&ABCD ;jump to address &ABCD if the last operation resulted in a negative value.
jr z,25 ;jump 25 bytes forward if the last operation resulted in a zero. (Some assemblers such as VASM make you type "$+25")
;(Unlike the others, JR cannot jump based on the sign flag or parity/overflow flag.)</langsyntaxhighlight>
 
The Game Boy is more limited than the Z80 in this regard, as it has no sign or overflow flags! It can only use the zero and carry flags for jumps.
Line 3,082:
<code>JP (HL)</code> can be used to create a "trampoline," which lets you indirectly call a subroutine, provided that subroutine doesn't take <code>HL</code> as an argument. (Otherwise you'll get some unwanted results.) To do this, you need to know the address of the subroutine you wish to call, load it into HL, then <code>CALL</code> any address that contains either <code>JP (HL)</code> or the byte 0xE9. (<code>JP (HL)</code>'s bytecode is 0xE9). Once you do this, execution will effectively "call" the desired routine. For this to work, the desired routine must end in a <code>RET</code>, and balance the stack properly. That way, when it does <code>RET</code>, it will <code>RET</code> to where you were just after the <code>CALL</code>.
 
<langsyntaxhighlight lang="z80">
ld hl,foo ;rather than directly specifying "foo", you would normally retrieve it by indexing a table.
;This was done only to keep the example as simple as possible.
Line 3,097:
foo:
ld a,0
ret</langsyntaxhighlight>
 
''NB: If your hardware allows you to define the <code>RST</code> calls to whatever you like, you can speed this up even more by setting one of them to <code>JP (HL)</code>, and invoking that RST as your trampoline rather than the call. The only downside is that you can't conditionally execute an <code>RST</code>, but if you were using a trampoline it likely wasn't going to be conditional anyway.''
10,327

edits