Segue un confronto tra i costrutti per il controllo del flusso del programma in Java ed in NetRexx.
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
}
|
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 |
L'istruzione IF è usata (come al solito) per eseguire gruppi di istruzioni a seconda di una determinata condizione.
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 |
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).
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 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 -- -- -- -- |
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.
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
|
Le eccezioni sono lanciate con l'istruzione SIGNAL.
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
--
|
Per interrompere l'esecuzione di un programma si può usare l'istruzione EXIT.
Javaif(exit_condition) System.exit(0) ; |
NetRexxif exit_condition then exit -- |
[Indice] [Capitolo precedente] [Capitolo successivo]
hosted by