Here is a strict comparison between control flow constructs in Java and NetRexx.
The DO instruction is used to group togheter chuncks of code. You can exit from a DO group with the LEAVE instruction. You can also protect objects, when the enclosed code executes, with the protect keyword (synchronization), catch exceptions and label the end of the group with a name for easy reading. An empty block or a null instruction must be rendered with NOP.
Java{ //... list of instructions } |
NetRexxdo --... list of instructions end |
{} ; |
nop nop |
{ //... list of instructions } |
do label do_name --... list of instructions end do_name |
synchronized(object) { //... list of instructions } |
do protect object --... list of instructions end |
try { //... list of instructions } catch(Exception e) { //... catch instructions } finally { //... finally instructions } |
do --... list of instructions catch e=Exception --... catch instructions finally --... finally instructions end |
The IF instruction is used (as usual) to conditionally execute group of instructions.
Javaif (expression) instruction; |
NetRexxif expression then instruction |
if (expression) instruction_then; else instruction_else; |
if expression then instruction_then else instruction_else |
if (expression) { instructions_then; } else { instructions_else; } |
if expression then do instructions_then end else do instructions_else end |
The LOOP instruction is used to execute repeatedly a block of instructions. You can exit a loop with the LEAVE instruction and repeat it before end with ITERATE. A LOOP block may use all the keywords of DO, such as label, protect, catch and finally. You can build very complex loops with repetitors (to-by-for, over, for and forever) and conditions (while and until).
Javawhile(true) { if(something) break; if(something_else) continue; // other instructions } |
NetRexxloop forever if something then leave if something_else then iterate -- other instructions end |
for(int ct = 0; ct < 10; ct++) { // instructions with ct not used } |
loop for 10 -- instructions end |
for(int ct = 0; ct < 10; ct++) { // instructions } |
loop ct=0 for 10 -- instructions end |
for(float x=5.1; x<17.2; x+=2.5) { // instructions } |
loop x=5.1 to 17.2 by 2.5 -- instructions end |
int ct = 0; for(float x=20.5; x>2.5; x-=2.0) { if(ct++ == 4) break; // instructions } |
loop x=20.5 to 2.5 by -2.0 for 4 -- instructions end -- -- |
Hashtable hash = new Hashtable(); hash.put("0","zero"); hash.put("1","one"); hash.put("2","two"); for ( Enumeration e = hash.elements(); e.hasMoreElements(); ) { System.out.println(e.nextElement()); } |
hash=Hashtable -- hash ='' hash.put('0','zero') -- hash['0']='zero' hash.put('1','one') -- hash['1']='one' hash.put('2','two') -- hash['2']='two' loop word over hash say word end -- -- You can use the class Hastable -- or alternatively indexed strings |
while(expression) { // instructions } |
loop while expression -- instructions end |
do { // instructions } while(!expression); |
loop until expression -- instructions end |
for(float x=0.0; x<9.0; ct+=0.1) { if(x < minX) break; // instructions if(x > maxX) break; } |
loop x=0.0 to 9.0 by 0.1 while x>minX until x>maxX -- instructions end x -- |
synchronized(anObject) { try { while(expression) { // instructions } } catch(Exception e) { // instructions } } |
loop protect anObject while expression -- instructions catch e=Exception -- instructions end -- -- -- -- |
The SELECT instruction is used to select one of several execution alternatives. The options are listed with the when keyword. If none of the conditions is fulfilled, the code marked with otherwise is executed. You can exit a selection with the LEAVE instruction. A SELECT block may use all the keywords of DO, such as label, protect, catch and finally.
Javaswitch(expression) { case 0: break; case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; default: System.out.println("other"); break; } |
NetRexxselect when expression=0 then nop when expression=1 then say'one' when expression=2 then say'two' otherwise say'other' end -- -- -- |
if(condition_1) { // instructions_1 } else if(condition_2) // instruction_2 else { // instructions_else } // // // |
select when condition_1 then do -- instructions_1 end when condition_2 then -- instruction_2 otherwise do -- instructions_else end end |
Exceptions are thrown with the SIGNAL instruction.
Javatry { // instructions if(something) throw new Exception( "something happened" ) ; // instructions } catch(Exception e) { System.out.println(e.toString()); } |
NetRexxdo -- instructions if something then signal Exception( "something happened" ) -- instructions catch e=Exception say e.toString end -- |
To stop a program you need to use the EXIT instruction.
Javaif(exit_condition) System.exit(0) ; |
NetRexxif exit_condition then exit -- |
[Index] [Previous Chapter] [Next Chapter]