Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!cbosgd!ihnp4!inuxc!iuvax!cjl From: cjl@iuvax.UUCP Newsgroups: net.lang.c Subject: Re: Pascal vs C, again (was: Pascals Ori Message-ID: <9500032@iuvax.UUCP> Date: Wed, 13-Aug-86 11:18:00 EDT Article-I.D.: iuvax.9500032 Posted: Wed Aug 13 11:18:00 1986 Date-Received: Sat, 16-Aug-86 05:50:41 EDT References: <7014@boring.UUCP> Lines: 61 Nf-ID: #R:boring:-701400:iuvax:9500032:000:2051 Nf-From: iuvax!cjl Aug 13 10:18:00 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@Indiana@CSNet-Relay