NetRexx at Once

Quick Guide for Java Developers


[Index] [Previous Chapter] [Next Chapter]

4. Control Flow

Here is a strict comparison between control flow constructs in Java and NetRexx.

4.1 DO Instruction

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
}
NetRexx
do
  --... 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

4.2 IF Instruction

The IF instruction is used (as usual) to conditionally execute group of instructions.

Java
if (expression)
  instruction;
NetRexx
if 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

4.3 LOOP Instruction

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).

Java
while(true) {
  if(something) break;
  if(something_else) continue;
  // other instructions
}
NetRexx
loop 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
--
--
--
--

4.4 SELECT Instruction

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.

Java
switch(expression) {
  case 0: break;
  case 1:
    System.out.println("one"); break;
  case 2:
    System.out.println("two"); break;
  default:
    System.out.println("other"); break;
}
NetRexx
select
  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

4.5 SIGNAL Instruction

Exceptions are thrown with the SIGNAL instruction.

Java
try {
  // instructions
  if(something)
    throw new Exception(
      "something happened"
    )
  ;
  // instructions
} catch(Exception e) {
  System.out.println(e.toString());
}
NetRexx
do
  -- instructions
  if something then
    signal Exception(
      "something happened"
    )
  -- instructions
catch e=Exception
  say e.toString
end
--

4.6 EXIT Instruction

To stop a program you need to use the EXIT instruction.

Java
if(exit_condition)
  System.exit(0)
;
NetRexx
if exit_condition then
  exit
--

[Index] [Previous Chapter] [Next Chapter]


TETRACTYS Freeware Main Page hosted by GeoCities Get your own Free Home Page
Hosted by www.Geocities.ws

1