Pascal vs C, again (was: Pascals Origins)
b-davis at utah-cs.UUCP
b-davis at utah-cs.UUCP
Tue Jul 22 10:28:48 AEST 1986
>In article <3130 at utcsri.UUCP> greg at utcsri.UUCP (Gregory Smith) writes:
> i :=1 ;
> while i<1000 and x[i] <> 0 do
> i := i+1;
> if x[i] = 0 then writeln('zero at location', i )
> else writeln('not found');
I personally like:
for i := 1 to 1000 do
if x[i] = 0 then goto DONE;
i := 1001;
DONE:
if i <= 1000 then writeln('zero at location', i )
else writeln('not found');
If you don't like the 'goto' then flame someplace else. I think that
goto's can be used in a structured manner. The statement 'i := 1001;'
is needed since the value of 'i' is undefined after the 'for' statement.
What's nice is that a good compiler can look at the 'for' loop and
determine that bounds checking is not needed on the 'if' statement.
The 'for' loop can also be done as a single machine instruction (on
none RISC machines). A good optimizing compiler might even be able
to generate better code than a good optimizing C compiler.
I like Pascal for some reasons and I like C for other reasons. The
reasons usually conflict. I don't claim to be consistant.
--
Brad Davis {ihnp4, decvax, seismo}!utah-cs!b-davis
b-davis at utah-cs.ARPA
One drunk driver can ruin your whole day.
More information about the Comp.lang.c
mailing list