Pascal vs C, again (was: Pascals Ori
cjl at iuvax.UUCP
cjl at iuvax.UUCP
Thu Aug 14 01:18:00 AEST 1986
Let us see what Pascal would be if Dr. N. Wirth could redesign it again.
In his Modula-2, both conditional boolean operators and LOOP-EXIT-END are
introduced. So with Modula-2 (Pascal-2 ??), we would write loops as :
(1) i:= 1;
LOOP
(* invariant : (i <= 1001) and (0 not in x[1..i-1]) *)
IF i = 1001 THEN EXIT END;
(* assert (i <= 1000) and (0 not in x[1..i-1]) *)
IF x[i] = 0 THEN EXIT END;
INC(i);
END;
IF i = 1001 THEN WriteString("not found."); WriteLn;
ELSE WriteString("Zero at location "); WriteInt(i,1); WriteLn;
END;
(2) i := 1;
LOOP
(* invariant : (i <= 1001) and (0 not in x[1..i-1]) *)
IF (i =1001) OR (x[i] = 0) THEN EXIT END;
INC(i);
END;
..............
(3) i := 1;
WHILE (i <= 1000) AND (x[i] <> 0) DO
INC(i);
END;
................
(4) i:= 1;
LOOP
(* invariant : (i <= 1001) and (0 not in x[1..i-1]) *)
IF i = 1001 THEN
WriteString("not found."); WriteLn; EXIT ;
END;
(* assert (i <= 1000) and (0 not in x[1..i-1]) *)
IF x[i] = 0 THEN
WriteString("Zero at location "); WriteInt(i,1); WriteLn; EXIT;
END;
INC(i);
END;
Wirth likes (3) because it tries to group and document the loop exit
conditions AT one place(simpler). Personally I like (4) because it is
easier to reason. (Ref Gries : The Science of Programming)
Note there is no statment between the two exit conditions and post-loop
clean-up follows corresponding exit condition more closely. That simplies
the program reasoning.
Apparently Wirth doesn't like to use boolean variables for controlling
loop-exit today. (Any other opinion ?) Also arbitrary loop-break
without trying to group them together would make post-loop assertion
unnecessary complex. (Thus hard to read.)
C.J.Lo
Dept. of CIS, IUPUI
UUCP : ...!iuvax!cjl
ARPA : cjl at Indiana@CSNet-Relay
More information about the Comp.lang.c
mailing list