Flow-control structures: Difference between revisions

Content added Content deleted
m (→‎exceptions: added a new category. -- ~~~~)
(→‎{{header|REXX}}: changed case of text to match the header case, order sections in alphabetical order. -- ~~~~)
Line 1,621: Line 1,621:


=={{header|REXX}}==
=={{header|REXX}}==
===call===
The CALL statement immediately transfer control to a named subroutine, and the CALL statement may have any number (or none) parameters.

The named subroutine may or may not return a   RESULT   (which is similar to a return code, but REXX allows character strings as well).
<lang rexx>numeric digits 1000 /*prepare for some gihugeic numbers.*/
...
n=4
call factorial n
say n'!=' result
exit
/*──────────────────────────────────FACTORIAL subroutine────────────────*/
factorial: parse arg x
!=1
do j=2 to x
!=!*j
end /*j*/
return !</lang>

===exceptions===
===exceptions===
{See '''SIGNAL''' below.)
(See '''signal''' below.)


===exit===
===exit===
The EXIT statement terminates the running (REXX) program and passes control to the invoking program (it could be the shell/host/supervisor program).
The EXIT statement terminates the running (REXX) program and passes control to the invoking program (it could be the shell/host/supervisor program).

If an expression is coded, it normally is used to set the RESULT (if a REXX program) or return code (also called RetCode, RC, completion code, or other such names).
If an expression is coded, it normally is used to set the RESULT (if a REXX program) or return code (also called RetCode, RC, completion code, or other such names).


Some operating systems require the expression to be a whole number within a certain range (often with a no expression or a [null] expression is usually taken to mean a return code of '''0''').
When using the EXIT with an expressing to pass control to the operating system (i.e., exiting a REXX program), some operating systems require the expression to be a whole number within a certain range (often with a no expression or a [null] expression, which is usually taken to mean a return code of '''0''').


If the expression is a number, it is normalized to the current NUMERIC DIGITS.
If the expression is a number, it is normalized to the current NUMERIC DIGITS.
Line 1,634: Line 1,653:


exit expression</lang>
exit expression</lang>

===iterate===
The ITERATE statement immediately transfer control to the DO statement, that is, it iterates (increments or decrements) the named REXX variable that is specified on the DO statement. &nbsp; The ITERATE statement can also specify which DO loop is to be iterated.

(All indentations in REXX are merely cosmetic and are used for readability.}
<lang rexx>sum=0
do j=1 to 1000
if j//3==0 | j//7==0 then iterate
sum=sum+j
end /*j*/

/*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 /*m*/
end /*k*/
say 'prod=' prod</lang>

===leave===
The LEAVE statement transfer control to the next REXX statement following the END statment of the current DO loop. &nbsp; The LEAVE statement can also specify which DO loop is to be left if the DO loop has a named variable.
<lang rexx> do j=1 to 10
say 'j=' j
if j>5 then leave
say 'negative j=' -j
end /*j*/

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 /*m*/
end /*k*/
say 'sum=' sum</lang>


===return===
===return===
Line 1,646: Line 1,709:


return expression</lang>
return expression</lang>

===select===
The SELECT statement is used to conditionally test for cases to selectively execute REXX 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 /*select*/

say 'result for' a op b "=" r</lang>


===signal===
===signal===
The SIGNAL statement can be thought of as a GO TO statement, however, on issuance of a SIGNAL statement, all executing DO loops and SELECTs are terminated. Essentially, that means that there is no real way to re-enter a DO loop once a SIGNAL statement is used.
The SIGNAL statement can be thought of as a GO TO statement, however, on issuance of a SIGNAL statement, all executing DO loops and SELECTs are terminated. &nbsp; Essentially, that means that there is no real way to re-enter a DO loop (or a SELECT structure) once a SIGNAL statement is used.


Once a SIGNAL statement is executed, control passed to the first occurance of the label specified (more than one label with the same name isn't considered an error). The label can be any combination of letters, digits, periods, and some special symbols, the most common are '''$''', '''#''', '''@''', '''!''', '''?''', and '''_''' (underscore or underbar). Some versions of REXX (CMS, PC/REXX, Personal REXX, TSO, R4, ROO) also allow the cent sign ('''¢'''), some of those REXXes support the British pound (currency) symbol ('''£''').
Once a SIGNAL statement is executed, control passed to the first occurance of the label specified (more than one label with the same name isn't considered an error). &nbsp; The label can be any combination of letters, digits, periods, and some special symbols, the most common are '''$''', '''#''', '''@''', '''!''', '''?''', and '''_''' (underscore or underbar). &nbsp; Some versions of REXX (CMS, PC/REXX, Personal REXX, TSO, R4, ROO) also allow the cent sign ('''¢'''), some of those REXXes support the British pound (currency) symbol ('''£''').


The SIGNAL statement is also used to transfer control in case of some specific conditions:
The SIGNAL statement is also used to transfer control in case of some specific conditions:
Line 1,692: Line 1,779:
exit 13
exit 13
...</lang>
...</lang>

===leave===
The LEAVE statement transfer control to the next REXX statement following the END statment of the current DO loop. The LEAVE statement can also specify which DO loop is to be left if the DO loop has 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 (increments or decements) the named REXX variable that is specified on the DO statement. 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 statement may have any number (or none) parameters. 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 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}}==
=={{header|Ruby}}==