Variable declaration reset: Difference between revisions

→‎{{header|ALGOL 68}}: Better explanation and use UPB/LWB for more Algol 68-ness
(Added Java)
(→‎{{header|ALGOL 68}}: Better explanation and use UPB/LWB for more Algol 68-ness)
Line 11:
 
=={{header|ALGOL 68}}==
In Algol 68, things that aren't explicitely initialised are notionally initialised to SKIP - an indeterminate value, so there should be no output from the program. Each iteration of the loop will get a new curr and prev, with prev initialised to SKIP. The following is equivalent to the Phix program...
Algol 68 should produce no output as each iteration of the loop has a separate instance of the variables.
<br>
The Algol 68 equivalent of the Phix program is as below, whether this works as expected depends on how the implementation treats uninitialised variables...
<lang algol68>BEGIN
[]INT s = ( 1, 2, 2, 3, 4, 4, 5 );
FOR i TO ( UPB sFROM -LWB s )TO +UPB 1s DO
INT curr := s[ i ], prev;
IF IF i > 1LWB s THEN curr = prev ELSE FALSE FI THEN
print( ( i, newline ) )
FI;
Line 24 ⟶ 22:
OD
END</lang>
...however, one of the non-standard features of Algol 68G is that uninitialised variables cause a runtime error instead of silently being set to SKIP.
{{out}} with [[ALGOL_68_Genie|Algol 68G]]:
<pre>
5 IF IF i > 1LWB ANDs THEN curr = prev ELSE FALSE FI THEN
1
a68g-2.8.3: runtime error: 1: attempt to use an uninitialised INT value (detected in VOIDBOOL conditional-clause starting at "IF" in this line).</pre>
...whereas, with [[Rutgers_ALGOL_68|Rutgers Algol 68]]:<br>
</pre>
with [[Rutgers_ALGOL_68|Rutgers Algol 68]]:<br>
No output.
 
3,021

edits