NetRexx at Once

Guida rapida per programmatori Java


[Indice] [Capitolo precedente] [Capitolo successivo]

4. Controllo del flusso

Segue un confronto tra i costrutti per il controllo del flusso del programma in Java ed in NetRexx.

4.1 L'istruzione DO

L'istruzione DO è usata per raggruppare parti del codice. E' possibile uscire da un gruppo DO con l'istruzione LEAVE. Si può anche proteggere un oggetto, quando viene eseguito il codice racchiuso, mediante la parola chiave protect (sincronizzazione), gestire le eccezioni con catch ed etichettare con label la fine del gruppo, per facilitare la lettura del codice. Un blocco vuoto o un'instruzione nulla sono realizzate mediante l'istruzione 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 L'istruzione IF

L'istruzione IF è usata (come al solito) per eseguire gruppi di istruzioni a seconda di una determinata condizione.

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 L'istruzione LOOP

L'istruzione LOOP è usata per eseguire ripetutamente un blocco di codice. Si può uscire dal loop con l'istruzione LEAVE e ripeterlo prima della fine con ITERATE. Un blocco LOOP può usare tutte le parole chiave del DO, come label, protect, catch e finally. Si possono realizzare loop molto complessi combinando ripetizioni (to-by-for, over, for e forever) e condizioni (while e 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 to 9
  -- 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'
for 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 L'istruzione SELECT

L'istruzione SELECT è utilizzata per selezionare un solo blocco di codice tra diversi possibili. Le opzioni sono elencate per mezzo della parola chiave when. Se nessuna condizione è soddisfatta, allora viene eseguito il codice marcato con otherwise. E' possibile uscire da un blocco SELECT con l'istruzione LEAVE. Un blocco SELECT può utilizzare tutte le parole chiave del DO, come label, protect, catch e 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 L'istruzione SIGNAL

Le eccezioni sono lanciate con l'istruzione SIGNAL.

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 L'istruzione EXIT

Per interrompere l'esecuzione di un programma si può usare l'istruzione EXIT.

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

[Indice] [Capitolo precedente] [Capitolo successivo]


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

1