langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,4 @@
loop xx = 1 to 10
if xx = 1 then leave -- loop terminated by leave
say 'unreachable'
end

View file

@ -0,0 +1,43 @@
loop xx = 1 to 10 -- xx is the control variable
...
loop yy = 1 to 10 -- yy is the control variable
...
if yy = 3 then leave xx -- xx loop terminated by leave
if yy = 4 then leave yy -- yy loop terminated by leave
...
end
...
end xx
loop label xlabel xx = 1 to 10 -- xx is still the control variable but LABEL takes precidence
...
loop yy = 1 to 10 -- yy is the control variable
...
if yy = 3 then leave xlabel -- xx loop terminated by leave
...
end yy
...
end xlabel
do label FINIS
say 'in do block'
if (1 == 1) then leave FINIS
say 'unreachable'
signal Exception("Will never happen")
catch ex = Exception
ex.printStackTrace()
finally
say 'out of do block'
end FINIS
loop vv over ['A', 'B']
select label selecting case vv
when 'A' then do; say 'A selected'; say '...'; end
when 'B' then do;
say 'B selected';
if (1 == 1) then leave selecting;
say '...';
end
otherwise do; say 'nl selection'; say '...'; end
end selecting
end vv

View file

@ -0,0 +1,9 @@
loop fff = 0 to 9
...
loop xx = 1 to 3
...
if fff > 2 then iterate fff
...
end
...
end fff

View file

@ -0,0 +1,10 @@
exception Found of int
let () =
(* search the first number in a list greater than 50 *)
try
let nums = [36; 23; 44; 51; 28; 63; 17] in
List.iter (fun v -> if v > 50 then raise(Found v)) nums;
print_endline "nothing found"
with Found res ->
Printf.printf "found %d\n" res

View file

@ -0,0 +1,5 @@
case {OS.rand} mod 3
of 0 then {Foo}
[] 1 then {Bar}
[] 2 then {Buzz}
end

View file

@ -0,0 +1,15 @@
declare
proc {Stupid X}
choice
X = 8
{System.showInfo "choosing 8"}
[] X = 9
{System.showInfo "choosing 9"}
[] X = 10
{System.showInfo "choosing 10"}
end
2 * X = 18
end
in
{Show {SearchOne Stupid}}

View file

@ -0,0 +1,28 @@
LEAVE
The LEAVE statement terminates execution of a loop.
Execution resumes at the next statement after the loop.
ITERATE
The ITERATE statement causes the next iteration of the loop to
commence. Any statements between ITERATE and the end of the loop
are not executed.
STOP
Terminates execution of either a task or the entire program.
SIGNAL FINISH
Terminates execution of a program in a nice way.
SIGNAL statement
SIGNAL <condition> raises the named condition. The condition may
be one of the hardware or software conditions such as OVERFLOW,
UNDERFLOW, ZERODIVIDE, SUBSCRIPTRANGE, STRINGRANGE, etc, or a
user-defined condition.
CALL
The CALL statement causes control to transfer to the named
subroutine.
SELECT
The SELECT statement permits the execution of just one of a
list of statements (or groups of statements).
It is sort of like a computed GOTO.
GO TO
The GO TO statement causes control to be transferred to the named
statement.
It can also be used to transfer control to any one of an array of
labelled statements. (This form is superseded by SELECT, above.)

View file

@ -0,0 +1,10 @@
label
jumpto;
begin
...
jumpto:
some statement;
...
goto jumpto;
...
end;

View file

@ -0,0 +1,5 @@
try
Z := DoDiv (X,Y);
except
on EDivException do Z := 0;
end;

View file

@ -0,0 +1 @@
procedure halt(errnum: Byte);

View file

@ -0,0 +1 @@
procedure exit(const X: TAnyType)

View file

@ -0,0 +1 @@
TOWN: goto TOWN;

View file

@ -0,0 +1,7 @@
while condition1 do
while condition2 do
if condition3 then
quitloop(2);
endif;
endwhile;
endwhile;

View file

@ -0,0 +1,8 @@
while condition1 do
while condition2 do
if condition3 then
goto l;
endif;
endwhile;
endwhile;
l:;

View file

@ -0,0 +1,16 @@
state1:
DO_SOMETHING();
if condition1 then
goto state1;
elseif condition2 then
goto state2;
....
else
goto stateN;
endif;
state2:
....
...
...
stateN:
....

View file

@ -0,0 +1,10 @@
define outer();
define inner(n);
if n = 0 then
goto final;
endif;
inner(n - 1);
enddefine;
inner(5);
final:;
enddefine;

View file

@ -0,0 +1 @@
go_on expression to lab1, lab2, ..., labN else elselab ;

View file

@ -0,0 +1 @@
return;

View file

@ -0,0 +1 @@
return(val1, val2, val3);

View file

@ -0,0 +1 @@
chain proc2(x1, x2, x3);

View file

@ -0,0 +1,12 @@
If OpenConsole()
top:
i = i + 1
PrintN("Hello world.")
If i < 10
Goto top
EndIf
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,8 @@
X=1: Y=2
Gosub Calc
;X will now equal 7
End
Calc:
X+3*Y
Return ; Returns to the point in the code where the Gosub jumped from

View file

@ -0,0 +1,12 @@
Gosub MySub
Lable2:
; The program will jump here, then 'end'
End
MySub:
If #PI>3
FakeReturn ; This will simulate the function of a normal "Return".
Goto Lable2
EndIf
Return

View file

@ -0,0 +1,11 @@
OnErrorGoto(?MyExitHandler)
X=1: Y=0
z= X/Y
; = a illegal division with zero
Debug "This line should never be reached"
End
MyExitHandler:
MessageRequester("Error", ErrorMessage())
End

View file

@ -0,0 +1,10 @@
Procedure MyErrorHandler()
;All open files etc can be closed here
MessageRequester("Error", ErrorMessage())
End
EndProcedure
OnErrorCall(MyErrorHandler())
X=1: Y=0
Z= X/Y
;This line should never be reached

View file

@ -0,0 +1,70 @@
REBOL [
Title: "Flow Control"
Author: oofoe
Date: 2009-12-05
URL: http://rosettacode.org/wiki/Flow_Control_Structures
]
; return -- Return early from function (normally, functions return
; result of last evaluation).
hatefive: func [
"Prints value unless it's the number 5."
value "Value to print."
][
if value = 5 [return "I hate five!"]
print value
]
print "Function hatefive, with various values:"
hatefive 99
hatefive 13
hatefive 5
hatefive 3
; break -- Break out of current loop.
print [crlf "Loop to 10, but break out at five:"]
repeat i 10 [
if i = 5 [break]
print i
]
; catch/throw -- throw breaks out of a code block to enclosing catch.
print [crlf "Start to print two lines, but throw out after the first:"]
catch [
print "First"
throw "I'm done!"
print "Second"
]
; Using named catch blocks, you can select which catcher you want when throwing.
print [crlf "Throw from inner code block, caught by outer:"]
catch/name [
print "Outer catch block."
catch/name [
print "Inner catch block."
throw/name "I'm done!" 'Johnson
print "We never get here."
] 'Clemens
print "We never get here, either."
] 'Johnson
; try
div: func [
"Divide first number by second."
a b
/local r "Result"
][
if error? try [r: a / b] [r: "Error!"]
r ; Functions return last value evaluated.
]
print [crlf "Report error on bad division:"]
print div 10 4
print div 10 2
print div 10 1
print div 10 0

View file

@ -0,0 +1,41 @@
/* GOTO: as in other languages
STOP: to stop current data step */
data _null_;
n=1;
p=1;
L1:
put n p;
n=n+1;
if n<=p then goto L1;
p=p+1;
n=1;
if p>10 then stop;
goto L1;
run;
/* LINK: equivalent of GOSUB in BASIC
RETURN: after a LINK, or to return to the beginning of data step */
data _null_;
input a b;
link gcd;
put a b gcd;
return;
gcd:
_a=a;
_b=b;
do while(_b>0);
_r=mod(_a,_b);
_a=_b;
_b=_r;
end;
gcd=_a;
return;
cards;
2 15
533 221
8 44
;
run;

View file

@ -0,0 +1,7 @@
Sub bar2()
Dim x = 0
GoTo label
x = 5
label:
Console.WriteLine(x)
End Sub

View file

@ -0,0 +1,7 @@
Sub foo()
On Error GoTo label
'do something dangerous
Exit Sub
label:
Console.WriteLine("Operation Failed")
End Sub

View file

@ -0,0 +1,7 @@
Sub foo2()
On Error Resume Next
Operation1()
Operation2()
Operation3()
Operation4()
End Sub

View file

@ -0,0 +1,9 @@
Sub Foo1()
If Not WorkNeeded() Then Exit Sub
DoWork()
End Sub
Sub Foo2()
If Not WorkNeeded() Then Return
DoWork()
End Sub

View file

@ -0,0 +1,11 @@
Function Foo3()
Foo3 = CalculateValue()
If Not MoreWorkNeeded() Then Exit Function
Foo3 = CalculateAnotherValue()
End Function
Function Foo4()
Dim result = CalculateValue()
If Not MoreWorkNeeded() Then Return result
Return CalculateAnotherValue()
End Function