🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #817

Re: Why are the loops so awkward?

#817
From: Peter.M..Perchansky Peter M. Per...
Date: Tue, 12 Jun 1990 21:17
40 lines
1721 bytes
Hello Peter:

    Why not do the following:

    PROCEDURE P (VAR s: ARRAY OF CHAR);
    VAR
       i, len : CARDINAL;
    BEGIN
        len := Length (s);
        FOR i := 0 TO len DO
            Process (s[i])
        END;
    END P;

    1)  I am not sure why you subtracted one from the length in your example since Length should return the given values given the following examples:

        s := ''; (* empty string *)
        (* len will equal 0 *)
        s := 'Peter';
        (* len will equal 5 *)

     2)  If you must subtract one, it is better to use the following:

         len := Length (s) - 1;

         instead of subtracting 1 from len in the FOR loop (some compilers might not optimize, and the expression will have to be evaluated every time.

      3)  To catch 0 - 1 errors when using CARDINAL, you can test it first.

      IF len # 0 THEN
         DEC (len) (* or len = len - 1 *)
      END;

   If you tell me what the procedure Process does, and what you are trying to accomplish, I (or some one else) might be better able to answer your questions.



--  
uucp: uunet!m2xenix!puddle!273!101!Peter.M..Perchansky
Internet: Peter.M..Perchansky@f101.n273.z1.fidonet.org
Technical Details
Message-ID: <5632.267655B5@puddle.fidonet.org>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!f101.n273.z1.fidonet.org!Peter.M..Perchansky

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!f101.n273.z1.fidonet.org!Peter.M..Perchansky
From: Peter.M..Perchansky@f101.n273.z1.fidonet.org (Peter M. Perchansky)
Newsgroups: comp.lang.modula2
Subject: Re: Why are the loops so awkward?
Message-ID: <5632.267655B5@puddle.fidonet.org>
Date: 12 Jun 90 21:17:02 GMT
Sender: ufgate@puddle.fidonet.org (newsout1.26)
Organization: FidoNet node 1:273/101 - Schizophrenia, Fleetwood PA
Lines: 40