Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!yale!mintaka!ogicse!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p0.f4.n494.z5.fidonet.org!cspw.quagga From: cspw.quagga@p0.f4.n494.z5.fidonet.org (cspw quagga) Newsgroups: comp.lang.modula2 Subject: Why are the loops so awkward? Message-ID: <5377.26731960@puddle.fidonet.org> Date: 9 Jun 90 21:04:56 GMT Sender: ufgate@puddle.fidonet.org (newsout1.26) Organization: FidoNet node 5:494/4.0 - Settler City Fido, Grahamstown RSA Lines: 48 I've been coerced into writing a large application in Modula2 and my confusions and biases are coming to the fore. Can someone please comment on the following examples and tell me whether I win the prize for the most abstruse interpretation of the rules, or whether I have really understood the language properly. Problem: do something to every element in an array (say a string). PROCEDURE P(VAR S : ARRAY OF CHAR); VAR i,j : INTEGER; k,n : CARDINAL; BEGIN n := Length(S); FOR k := 0 TO n-1 DO process(S[k]); END; (* Wrong: For empty strings, n-1 is not a cardinal, or may be very big *) FOR i := 0 TO n-1 DO process(S[i]); END; (* Wrong: Can't mix INTEGER and CARDINAL in the same loop *) j := INTEGER(n); (* Lets assume this is okay *) FOR i := 0 TO j-1 DO (* The loop now goes correctly in all cases *) process(S[i]); END; (* Wrong: The index type of an open array is CARDINAL, so using an integer as a subscript is not strictly permitted. *) k := 0; WHILE (S[k] <> 0C) DO process(S[k]); INC(K); END; (* Wrong: Strings do not always store the trailing NUL byte. *) END P; Am I in a grumpy mood without justifiable cause? Pete -- uucp: uunet!m2xenix!puddle!5!494!4.0!cspw.quagga Internet: cspw.quagga@p0.f4.n494.z5.fidonet.org