Pascal vs C, again (was: Pascals Origins)

Gregory Smith greg at utcsri.UUCP
Sat Jul 19 04:14:43 AEST 1986


In article <7014 at boring.mcvax.UUCP> jack at boring.uucp (Jack Jansen) writes:
>
>I'm getting sick and tired of all those people picking on pascal,
>and saying "C is better", without giving *any* reason for it.
>
:
>You can say a lot of things about pascal, but not that it wasn't
>well-designed. And, even though you have to do very wierd things
>to get some things to work, it *is* possible to write medium to

Challenge: given

var	x: array [1..1000] of integer;

write a code fragment to find the location of the first 0 in x. The
condition that no zeroes exist must be distinguished.

Further rules:
	- Standard Jensen & Wirth Pascal ( no break from loop )
	- *** x[i] must not be evaluated for i<1 or i>1000 ***
	- The search loop must be terminated as soon as a zero is found.
	- 'a or b', 'a and b' always evaluate both a and b ( I think
	   this is a rule in Jensen & Wirth )

This is the best I can find:
	var i: integer;
	...
	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');

weird,huh? Note that the condition 'x[i]=0' is evaluated twice ( once
in the negative sense ), which would be unacceptable if we were searching
an array of records and the test was expensive.

Nobody can call this a special case... and I don't think my rules are
unreasonable. The problem is the behaviour of 'and' plus the lack of 'break'.

-- 
"You'll need more than a Tylenol if you don't tell me where my father is!"
						- The Ice Pirates
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list