Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!dsinc!netnews.upenn.edu!dsl.cis.upenn.edu!touch From: touch@dsl.cis.upenn.edu (Joe Touch) Newsgroups: comp.lang.modula2 Subject: Re: Why are the loops so awkward? Message-ID: <26032@netnews.upenn.edu> Date: 14 Jun 90 19:00:52 GMT References: <5377.26731960@puddle.fidonet.org> <728.26767558@waikato.ac.nz> <22213@boulder.Colorado.EDU> <747.2677cd57@waikato.ac.nz> <22259@boulder.Colorado.EDU> Sender: news@netnews.upenn.edu Reply-To: touch@dsl.cis.upenn.edu (Joe Touch) Organization: University of Pennsylvania Lines: 34 In article <22259@boulder.Colorado.EDU> wolniewi@tramp.Colorado.EDU (WOLNIEWICZ RICHARD H.) writes: > >I would probably write > > stringsSame := TRUE; > WHILE stringsSame AND (index <= stringLength) DO > stringsSame := (String1[index] = String2[index]); > INC(index); > END; (* WHILE *) > I'd prefer WHILE (index <= stringLength) AND (String1[index] = String2[index] DO INC(index); END; (* WHILE *) Unless my book is wrong, M2 is defined as having short-circuit evaluation of conjunctions and disjunctions. So if the index is out of bounds, the string element comparison never occurs, and the AND is safe. This omits the use of a redundant boolean (logical) variable, and not only is ir correct, but it will run faster (you don't really need to store the value of the comparison, do you?) I'd like to add that loops aren't awkward, but some PROGRAMMERS implementation of them IS. ;-) Joe Touch PS - A good text for a M2 course is "Modula-2" by Beidler and Jackowitz, PWS Publishers, 1986 (approx). It was developed by my advisor at the University of Scranton when I was there, where M2 has been used as a primary teaching language since 1984.