From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ee01489f40ec6ec7 X-Google-Attributes: gid103376,public From: dewar@cs.nyu.edu (Robert Dewar) Subject: Re: Emulating Modula-3's FOR loop Date: 1996/07/07 Message-ID: #1/1 X-Deja-AN: 164198507 references: <4rocup$rvo@ia.mks.com> organization: Courant Institute of Mathematical Sciences newsgroups: comp.lang.ada Date: 1996-07-07T00:00:00+00:00 List-Id: David J. Fiander suggests for a loop jumping by N: begin while I <= Subscript'Last loop -- process I := I + N; end loop; exception when Constraint_Error => null; end; This is definitely bad code. Using Constraint_Error to catch normal flow of control situations as opposed to errors is a bad misuse of the feature. Not only is it conceptually incorrect, but in practice it can be disastrously inefficient (in many implementations, a constraint error signal will involve a kernel trap with huge overhead). A much better translation is loop -- process exit when I > Subscript'Last - N; I := I + N; end loop; this should generate perfectly efficient code, since the expression Subscript'Last - N is loop invariant. Note that this assumes you know the loop will execute at least once. if not, the entire loop needs to be protected by an appropriate test.