Jump anywhere: Difference between revisions
Content deleted Content added
fix <code> issue |
PascalABC.NET |
||
(26 intermediate revisions by 15 users not shown) | |||
Line 23:
You may also defer to more specific tasks, like [[Exceptions]] or [[Generator]].
<br><br>
=={{header|360 Assembly}}==
The unconditionnal branch instruction <b>B</b> is the jump anywhere of the S/360 assembler.
<syntaxhighlight lang="360asm"> ...
B ANYWHERE branch
...
ANYWHERE EQU * label
...</syntaxhighlight>
=={{header|6502 Assembly}}==
Line 193 ⟶ 201:
Although <code>JMP</code> was referred to as "unconditional" before, it does ''not'' have access to the entire address space of the CPU, unlike the 6502, Z80, and 68000. The 8086 uses a segmented memory model, where it uses 20-bit addresses despite being a 16-bit CPU. The other 4 bits are the "segment," and a <code>JMP</code> or <code>CALL</code> cannot exit that segment.
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program julpanaywhere64.s */
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "loop indice : "
szMessage1: .asciz "Display to routine call by register\n"
szMessage2: .asciz "Equal to zero.\n"
szMessage3: .asciz "Not equal to zero.\n"
szMessage4: .asciz "loop start\n"
szMessage5: .asciz "No executed.\n"
szMessResult1: .asciz ","
szMessResult2: .asciz "]\n"
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess // branch and link to routine
// return here after routine execution
b label1 // branch unconditional to label
ldr x0,qAdrszMessage5 // this instruction is never executed
bl affichageMess // and this
label1:
ldr x0,qAdrszMessage4
bl affichageMess
mov x20,0
1:
mov x0,x20
ldr x1,qAdrsZoneConv
bl conversion10 // decimal conversion
strb wzr,[x1,x0]
mov x0,#3 // number string to display
ldr x1,qAdrszMessResult
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszCarriageReturn
bl displayStrings // display message
add x20,x20,1 // increment indice
cmp x20,5
blt 1b // branch for loop if lower
mov x0,0
cbz x0,2f // jump to label 2 if x0 = 0
2:
adr x1,affichageMess // load routine address in register
ldr x0,qAdrszMessage1
blr x1
mov x4,4
cmp x4,10
bgt 3f // branch if higter
mov x0,x4
3:
mov x0,0b100 // 1 -> bit 2
tbz x0,2,labzero // if bit 2 equal 0 jump to label
ldr x0,qAdrszMessage3 // bit 2 <> 0
bl affichageMess
b endtest // jump end if else
labzero: // display if bit equal to 0
ldr x0,qAdrszMessage2
bl affichageMess
endtest:
mov x0,0b000 // 0 -> bit 2
tbnz x0,2,4f // if bit 2 <> 0 jump to label
ldr x0,qAdrszMessage2 // bit 2 = 0
bl affichageMess
b 5f // jump end test
4: // display if bit equal to 1
ldr x0,qAdrszMessage3
bl affichageMess
5:
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszMessage1: .quad szMessage1
qAdrszMessage2: .quad szMessage2
qAdrszMessage3: .quad szMessage3
qAdrszMessage4: .quad szMessage4
qAdrszMessage5: .quad szMessage5
qAdrszMessStart: .quad szMessStart
/***************************************************/
/* display multi strings */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: // INFO: displayStrings
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
add fp,sp,#48 // save paraméters address (6 registers saved * 8 bytes)
mov x4,x0 // save strings number
cmp x4,#0 // 0 string -> end
ble 100f // branch to equal or smaller
mov x0,x1 // string 1
bl affichageMess
cmp x4,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x4,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x4,#3
ble 100f
mov x3,#3
sub x2,x4,#4
1: // loop extract address string on stack
ldr x0,[fp,x2,lsl #3]
bl affichageMess
subs x2,x2,#1
bge 1b
100:
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret // return to addtress stored in lr
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
loop start
loop indice : 0
loop indice : 1
loop indice : 2
loop indice : 3
loop indice : 4
Display to routine call by register
Not equal to zero.
Equal to zero.
</pre>
=={{header|Ada}}==
Line 353 ⟶ 533:
300 GOTO : REM NO LINE NUMBER, JUMPS TO LINE 0</syntaxhighlight>
<syntaxhighlight lang="applesoftbasic">CONT : REM CONTINUE, JUMP BACK TO WHERE THE PROGRAM STOPPED</syntaxhighlight>
==={{header|BASIC256}}===
BASIC256 supports both <code>goto</code> and <code>gosub</code>.
{{works with|QBasic}}
{{works with|Yabasic}}
<syntaxhighlight lang="freebasic">print "First line."
gosub sub1
print "Fifth line."
goto Ending
sub1:
print "Second line."
gosub sub2
print "Fourth line."
return
Ending:
print "We're just about done..."
goto Finished
sub2:
print "Third line."
return
Finished:
print "... with goto and gosub, thankfully."
end</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FutureBasic.</pre>
==={{header|Chipmunk Basic}}===
Chipmunk Basic supports both <code>goto</code> and <code>gosub</code>.
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 PRINT "First line."
120 GOSUB sub1
130 PRINT "Fifth line."
140 GOTO Ending
150 sub1:
160 PRINT "Second line."
170 GOSUB sub2
180 PRINT "Fourth line."
190 RETURN
200 Ending:
210 PRINT "We're just about done..."
220 GOTO Finished
230 sub2:
240 PRINT "Third line."
250 RETURN
260 Finished:
270 PRINT "... with goto and gosub, thankfully."
280 END</syntaxhighlight>
{{out}}
<pre>Same as FutureBasic entry.</pre>
==={{header|GW-BASIC}}===
GW-BASIC supports both <code>goto</code> and <code>gosub</code>.
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|Minimal BASIC}}
{{works with|MSX BASIC|any}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 PRINT "First line."
110 GOSUB 140
120 PRINT "Fifth line."
130 GOTO 190
140 REM sub1:
150 PRINT "Second line."
160 GOSUB 220
170 PRINT "Fourth line."
180 RETURN
190 REM Ending:
200 PRINT "We're just about done..."
210 GOTO 250
220 REM sub2:
230 PRINT "Third line."
240 RETURN
250 REM Finished:
260 PRINT "... with goto and gosub, thankfully."
270 END</syntaxhighlight>
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">10 GOTO 100 ! jump to a specific line
20 RUN 200 ! start the program running from a specific line</syntaxhighlight>
==={{header|Minimal BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|Quite BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|Run BASIC}}===
Line 380 ⟶ 653:
print "done"</syntaxhighlight>
==={{header|
<syntaxhighlight lang="qbasic">100 ! Jump anywhere
110 CLEAR
130 GOSUB 160
150 GOTO 210
160 ! sub1:
170 PRINT "Second line."
180 GOSUB 250
190 PRINT "Fourth line."
200 RETURN
210 ! Ending:
220 PRINT "We're just about done..."
230 GOTO 270
240 ! sub2:
250 PRINT "Third line."
260 RETURN
270 ! Finished:
280 PRINT "... with goto and gosub, thankfully."
290 END</syntaxhighlight>
{{out}}
<pre>Same as FutureBasic entry.</pre>
==={{Header|Tiny BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
=={{header|BQN}}==
Jumping anywhere (akin to APL's `→`) is not possible in BQN, and BQN does not have labels either.
The main forms of jumping are within blocks.
=== Block predicates ===
These constitute conditional jumps. If the value is 0, the section following it is skipped.
<syntaxhighlight lang="bqn"> { 0 ? "Will not execute"; "Will execute" }
"Will execute"</syntaxhighlight>
=== Function calls ===
You can call a function from a block, temporarily jumping over to the function's body, and coming back.
<syntaxhighlight lang="bqn"> F←{𝕩⊣•Out"In Second"}
{
•Out "First"
F@
•Out "Last"
}</syntaxhighlight>
<syntaxhighlight lang="bqn"> First
In Second
Last</syntaxhighlight>
=={{header|C}}==
Line 619 ⟶ 911:
=={{header|COBOL}}==
<syntaxhighlight lang="
PROGRAM-ID.
* Nobody writes like this, of course; but...
PROCEDURE DIVISION.
* You can jump anywhere you like.
start-paragraph.
GO TO
yet-another-paragraph.
ALTER start-paragraph TO PROCEED TO a-paragraph-somewhere.
* That's right, folks: we don't just have GO TOs, we have GO TOs whose
* destinations can be changed at will, from anywhere in the program,
* at run time.
GO TO
* But bear in mind: once you get there, the GO TO no longer goes to
* where it says it goes to.
a-paragraph-somewhere.
DISPLAY 'Never heard of him.'
STOP RUN.
some-other-paragraph.
* You think that's bad? You ain't seen nothing.
GO TO
an-arbitrary-paragraph.
DISPLAY 'Edsger who now?'
GO TO some-other-paragraph.
END PROGRAM jumps-program.</syntaxhighlight>
{{out}}
<pre>Edsger who now?
Line 649 ⟶ 949:
'''COBOL''' also supports computed go to phrases, given a list of labels (paragraph names) and an integer index into that list.
<syntaxhighlight lang="
WORKING-STORAGE SECTION.
01 province PICTURE IS 99 VALUE IS 2.
PROCEDURE DIVISION.
GO TO quebec, ontario, manitoba DEPENDING ON province.
* Jumps to section or paragraph named 'ontario'.</syntaxhighlight>
=={{header|Common Lisp}}==
Line 961 ⟶ 1,266:
end.
</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|EMal has no goto statement.
|The closes statements are break, continue, return, exit
|and exceptions management.
|^
fun sample = void by block
for int i = 1; i < 10; ++i
if i == 1 do continue end # jumps to next iteration when 'i' equals 1
writeLine("i = " + i)
if i > 4 do break end # exits the loop when 'i' exceeds 4
end
for int j = 1; j < 10; ++j
writeLine("j = " + j)
if j == 3 do return end # returns from the function when 'j' exceeds 3
end
end
sample()
type StateMachine
^|this code shows how to selectevely jump to specific code
|to simulate a state machine as decribed here:
|https://wiki.tcl-lang.org/page/A+tiny+state+machine
|Functions return the next state.
|^
int n = -1
Map stateMachine = int%fun[
0 => int by block
if Runtime.args.length == 1
n = when(n == -1, int!Runtime.args[0], 0)
else
n = ask(int, "hello - how often? ")
end
return when(n == 0, 2, 1)
end,
1 => int by block
if n == 0 do return 0 end
writeLine(n + " Hello")
n--
return 1
end,
2 => int by block
writeLine("Thank you, bye")
return -1
end]
int next = 0
for ever
next = stateMachine[next]()
if next == -1 do break end
end
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\JumpAnywhere.emal 2
i = 2
i = 3
i = 4
i = 5
j = 1
j = 2
j = 3
2 Hello
1 Hello
Thank you, bye
</pre>
=={{header|Erlang}}==
Jumps are limited to [[Exceptions]].
Line 1,298 ⟶ 1,669:
All that said, don't use spaghetti code -- use functions. You can still by spark plugs for a 1985 Yugo, but why?
<syntaxhighlight lang="futurebasic">
window 1
print "First line."
Line 1,317 ⟶ 1,688:
"Outa here"
print "... with goto and gosub, thankfully."
HandleEvents
</syntaxhighlight>
Output:
Line 1,575 ⟶ 1,947:
Now we are in G</syntaxhighlight>
J also supports jumps to labels within a definition (but makes them a bit tedious to express, which has been rather successful at discouraging their use):
<syntaxhighlight lang="j">H=: verb define
Line 2,058 ⟶ 2,430:
PARI inherits C's ability to <code>goto</code> or <code>longjmp</code>; the latter is used extensively in the library.
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
label finish;
begin
var a := MatrRandomInteger(3,4);
var found := False;
for var i:=0 to a.RowCount-1 do
for var j:=0 to a.ColCount-1 do
if a[i,j] = 10 then
begin
found := True;
goto finish;
end;
finish:
Print(found);
end.
</syntaxhighlight>
=={{header|Perl}}==
Line 2,834 ⟶ 3,225:
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.
=={{header|
Branches are the only kind of control structure in SNOBOL4. They come in three flavours (and one compound one):
* <code>:(LABEL_UNCONDITIONAL)</code>
* <code>:S(LABEL_ON_SUCCESS)</code>
* <code>:F(LABEL_ON_FAILURE)</code>
* <code>:S(LABEL_ON_SUCCESS)F(LABEL_ON_FAILURE)</code> or <code>:F(LABEL_ON_FAILURE)S(LABEL_ON_SUCCESS)</code>
<syntaxhighlight lang="snobol4">
* Demonstrate an unconditional branch.
OUTPUT = "Unconditional branch." :(SKIP1.START)
OUTPUT = "This will not display."
* Demonstrate a branch on success.
SKIP1.START v = 1
SKIP1.LOOP gt(v, 5) :S(SKIP2.START)
OUTPUT = "Iteration A" v
v = v + 1 :(SKIP1.LOOP)
* Demonstrate a branch on failure.
SKIP2.START v = 1
SKIP2.LOOP le(v, 5) :F(SKIP3.START)
OUTPUT = "Iteration B" v
v = v + 1 :(SKIP2.LOOP)
* Demonstrate a combined branch.
* Demonstrate also an indirect branch.
SKIP3.START v = 0
label = "SKIP3.LOOP"
SKIP3.LOOP v = v + 1
le(v, 5) :S(SKIP3.PRINT)F(EXIT)
SKIP3.PRINT OUTPUT = "Iteration C" v :($label)
EXIT OUTPUT = "Goodbye"
END
</syntaxhighlight>
{{Out}}
<pre>
Unconditional branch.
Iteration A1
Iteration A2
Iteration A3
Iteration A4
Iteration A5
Iteration B1
Iteration B2
Iteration B3
Iteration B4
Iteration B5
Iteration C1
Iteration C2
Iteration C3
Iteration C4
Iteration C5
Goodbye
</pre>
=={{header|SPL}}==
Line 2,872 ⟶ 3,321:
=={{header|Tcl}}==
Tcl has both [[Exceptions#Tcl|exceptions]] and (from 8.6 onwards) [[Generator#Tcl|generators/coroutines]] but no unstructured goto ability. However, the main case where it might be desired, coding a general state machine, can be handled through metaprogramming (as discussed at some length on [http://wiki.tcl.tk/8363 the Tcler's Wiki]) so the absence is not strongly felt in practice.
=={{header|TXR}}==
{{trans|Common Lisp}}
TXR Lisp has a <code>tagbody</code> similar to Common Lisp. Like the Common Lisp one, it establishes an area of the program with forms labeled by symbols or numbers. The forms can branch to these symbols or numbers using <code>go</code>.
When a form initiates a branch, it is gracefully abandoned, which means that unwinding takes place: <code>unwind-protect</code> clean-up forms are called. Once the form is abandoned, control then transfers to the target form.
A <code>go</code> transfer may be used to jump out of a lexical closure, if the <code>tagbody</code> is still active. If a closure is captured in a <code>tagbody</code> which then terminates, and that closure is invoked, and tries to use <code>go</code> to jump to adjacent forms in that terminated tagbody, it is an error. An example of this follows, from an interactive session:
<pre>1> (let (fun)
(tagbody
again
(set fun (lambda () (go again))))
[fun])
** expr-1:4: return*: no block named #:tb-dyn-id-0028 is visible
** during evaluation of form (return* #:tb-id-0024
0)
** ... an expansion of (go again)
** which is located at expr-1:4</pre>
The above error messages reveal that TXR Lisp's <code>tagbody</code> is implemented by macros, and relies on a dynamic block return. It is provided mainly for compatibility; Common Lisp users using TXR Lisp may find it handy.
If the <code>tagbody</code> is still active when the lambda tries to perform a <code>go</code>, it works:
<pre>2> (let (fun)
(tagbody
(set fun (lambda () (go out)))
[fun]
(put-line "this is skipped")
out
(put-line "going out")))
going out
nil</pre>
The translated Common Lisp example follows:
<syntaxhighlight lang="txrlisp">(tagbody
beginning
(put-line "I am in the beginning")
(usleep 1000000)
(go end)
middle
(put-line "I am in the middle")
(usleep 1000000)
(go beginning)
end
(put-line "I am in the end")
(usleep 1000000)
(go middle))</syntaxhighlight>
{{out}}
<pre>
I am in the beginning
I am in the end
I am in the middle
I am in the beginning
I am in the end
I am in the middle
I am in the beginning
...</pre>
=={{header|VBA}}==
Line 2,904 ⟶ 3,414:
=={{header|VBScript}}==
In VBScript, there is no <code>goto</code> statement. It is a good thing for structured programming.
=={{header|V (Vlang)}}==
V (Vlang) and 'goto':
1) 'Goto' used only with 'unsafe' statements.
2) 'Goto' only allowed unconditionally jumping to a label within the function it belongs to.
3) Labelled 'break' and 'continue' statements are among the preferred alternatives to 'unsafe goto' usage.
4) Labelled 'break' and 'continue' allow easy breaking out of a nested loop or continuing within a containing loop.
<syntaxhighlight lang="v (vlang)">
// Unsafe 'goto' pseudo example:
if x {
// ...
if y {
unsafe {
goto my_label
}
}
// ...
}
my_label:
// Labelled 'break' and 'continue' example:
outer:
for idx := 0; idx < 4; idx++ {
for jdx := 0; jdx < 4; jdx++ {
if idx + jdx == 4 {continue outer}
if idx + jdx == 5 {break outer}
println(idx + jdx)
}
}
</syntaxhighlight>
=={{header|VTL-2}}==
Line 2,949 ⟶ 3,495:
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.
<syntaxhighlight lang="
for (i in 1..10) {
if (i == 1) continue // jumps to next iteration when 'i' equals 1
Line 2,987 ⟶ 3,533:
resuming
aborting
[./
</pre>
Line 3,118 ⟶ 3,664:
{{omit from|Oberon-2}}
{{omit from|UNIX Shell|Does not have goto or line labels, but can break from nested loops}}
{{omit from|Insitux}}
|