Flow-control structures: Difference between revisions

m
m (Structure)
Line 1,106:
print div 10 1
print div 10 0</lang>
 
 
=={{header|REXX}}==
===exit===
The EXIT statement terminates the running (REXX) program and passes control to
<br> the invoking program (it could be the shell/host/supervisor program).
<br> If the invoking program is a REXX program, it also is terminated.
<br>
<br> If an expression is coded, it normally is used to set the return code (also called
<br> return code, completion code, or other such names).
<br>
<br> Some operating systems require the expression to be a whole number (often with a
<br> No expression or a [null] expression is usually taken to mean a return code of 0 (zero).
<lang rexx>
exit
 
exit expression
</lang>
===return===
the RETURN statement terminates the running (REXX) program and passes control to
<br> the invoking program (it could be the shell/host/supervisor program).
<br> If the invoking program isn't a REXX program, the RETURN statement acts like an EXIT.
<br>
<br> If the invoker is a REXX program and the RETURN statement has no EXPRESSION coded, and
<br> the invoker expects a RESULT, a SYNTAX condition (error) is raised --- which usually
<br> results in an abnormal termination of the invoking REXX program (this condition can be
<br> trapped, however).
<lang rexx>
return
 
return expression
</lang>
===SIGNAL===
The SIGNAL statement can be thought of as a GO TO statement, however, on issuance of
<br> a SIGNAL statement, all executing DO loops and SELECTs are terminated.
<br> Essentially, that means that there is no real way to re-enter a DO loop once a SIGNAL
<br> statement is used.
<br>
<br> Once a SIGNAL statement is executed, control passed to the first occurance of the label
<br> specified (more than one label with the same name isn't considered an error).
<br> The label can be any combination of letters, digits, periods, and some special symbols,
<br> the most common are $, #, @, !, ?, and _ (underscore or underbar).
<br> Some versions of REXX (CMS, TSO, R4) also allow the cent sign (¢).
<br>
<br> The SIGNAL statement is also used to transfer control in case of some specific conditions:
<br>
<br> ∙ when an I/O stream (could be a file) isn't ready.
<br> ∙ when the REXX program used a variable that isn't defined.
<br> ∙ when a REXX syntax error occurs.
<br> ∙ when the program is HALTed (usually via a Ctrl─Alt─Del ──or── PA1 ──or── somesuch.
<br> ∙ when there is a loss of digits.
<br> ∙ when a command executed returns an error return code [other than 0 (zero)].
<br> ∙ when a command executed indicates a failure.
<lang rexx>
signal on error
signal on failure
signal on halt
signal on lostdigits
signal on notready
signal on novalue
signal on syntax
 
 
signal off error
signal off failure
signal off halt
signal off lostdigits
signal off notready
signal off novalue
signal off syntax
 
signal on novalue
x=oopsay+1
novalue: say
say '*** error! ***'
say
say 'undefined REXX variable' condition("D")
say
say 'in line' sigl
say
say 'REXX source statement is:'
say sourceline(sigl)
say
exit 13
</lang>
===leave===
The LEAVE statement transfer control to the next REXX statement following the END
<br> statment of the current DO loop.
<br> The LEAVE statement can also specify which DO loop is to be left if the DO loop has
<br> a named variable.
<lang rexx>
do j=1 to 10
say 'j=' j
if j>5 then leave
say 'negative j=' -j
end
say 'end of the DO loop for j.'
ouch=60
sum=0
 
do k=0 to 100 by 3
say 'k=' k
 
do m=1 to k
if m=ouch then leave k
sum=sum+m
end
 
end
say 'sum=' sum
</lang>
===iterate===
The ITERATE statement immediately transfer control to the DO statement, that is, it iterates
<br> (increments or decements) the named REXX variable that is specified on the DO statement.
<br> The ITERATE statement can also specify which DO loop is to be iterated.
<lang rexx>
sum=0
 
do j=1 to 1000
if j//3==0 | j//7==0 then iterate
sum=sum+j
end
 
/*shows sum of 1k numbers except those divisible by 3 or 7.*/
say 'sum='sum
numeric digits 5000
prod=0
 
do k=1 to 2000
 
do m=1 to k
if m>99 then iterate k
prod=prod*m
end
 
end
say 'prod=' prod
</lang>
===call===
The CALL statement immediately transfer control to a named subroutine, and the CALL
<br> statement may have any number (or none) parameters.
<br> The named subroutine may or may not return a RESULT (which is similar to a return code).
<lang rexx>
numeric digits 1000 /*prepare for some gihugeic numbers.*/
n=4
call factorial n
say n'!=' result
exit
 
 
factorial: parse arg x
!=1
 
do j=2 to x
!=!*j
end
 
return !
</lang>
===select===
The SELECT statement is used to conditionaly test for cases to selectively execute REXX
<br> statement(s).
<lang rexx>
prod=1
a=7 /*or somesuch.*/
b=3 /* likewise. */
 
op='**' /*or whatever.*/
 
select
when op=='+' then r=a+b /*add. */
when op=='-' then r=a-b /*subtract. */
when op=='*' then do; r=a*b; prod=prod*r; end /*multiply.*/
when op=='*' then r=a*b /*multiply. */
when op=='/' & b\=0 then r=a/b /*divide. */
when op=='%' & b\=0 then r=a/b /*interger divide. */
when op=='//' & b\=0 then r=a/b /*modulus (remainder). */
when op=='||' then r=a||b /*concatenation. */
when op=='caw' then r=xyz(a,b) /*call the XYZ subroutine*/
otherwise r='[n/a]' /*signify not applicable.*/
end
 
say 'result for' a op b "=" r
</lang>
 
=={{header|Ruby}}==