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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,da5197b9dca0ed40 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!f17g2000vbl.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Re: Processing array subsections, a newbie question. Date: Sat, 12 Jun 2010 18:20:26 -0700 (PDT) Organization: http://groups.google.com Message-ID: <9e1f3f35-6f49-468f-be89-46d4f51f8193@f17g2000vbl.googlegroups.com> References: <4c13db30$0$2391$4d3efbfe@news.sover.net> <8739wsottd.fsf@ludovic-brenta.org> NNTP-Posting-Host: 173.87.192.162 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1276392026 10483 127.0.0.1 (13 Jun 2010 01:20:26 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 13 Jun 2010 01:20:26 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: f17g2000vbl.googlegroups.com; posting-host=173.87.192.162; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:11668 Date: 2010-06-12T18:20:26-07:00 List-Id: On Jun 12, 4:54=A0pm, Ludovic Brenta wrote: > "Peter C. Chapin" writes: > > > Ok =A0 =A0 =A0 =A0: Boolean :=3D True; > > Index_Fst : Natural :=3D Buffer'First; > > Index_Lst : Natural; > > ... > > > while Ok and Index_Fst <=3D Buffer'Last loop > > =A0 Do_Something(Buffer, Index_Fst, Index_Lst, Ok); > > =A0 Index_Fst :=3D Index_Lst + 1; > > end loop; > > > The problem with this code is that the assignment to Index_Fst inside t= he loop > > might raise Constraint_Error if Index_Lst =3D Buffer'Last after Do_Some= thing > > finishes. I can work around this problem but my solutions tend to be ra= ther > > ungainly looking. Surely there must be an easy way to handle this. > > How about: > > Ok =A0 =A0 =A0 =A0: Boolean :=3D True; > Index_Fst : Natural :=3D Buffer'First; > Index_Lst : Natural; > ... > loop > =A0 =A0Do_Something(Buffer, Index_Fst, Index_Lst, Ok); > =A0 =A0exit when not Ok; > =A0 =A0exit when Index_Fst =3D Buffer'Last; > =A0 =A0Index_Fst :=3D Index_Lst + 1; > end loop; Yes! Or save a line with loop =A0 =A0Do_Something(Buffer, Index_Fst, Index_Lst, Ok); =A0 =A0exit when not Ok or Index_Fst =3D Buffer'Last; =A0 =A0Index_Fst :=3D Index_Lst + 1; end loop; Loop exit (with optional naming, which obviates the problems of 'break' in C and its successors) is one of the small beauties of Ada and an example of the way things should have been - with benefit of hindsight - in all ALGOL-like languages. Rather than while ... and do ... while ... , which are inherently limited in expressive power, every language ought to offer loop ... exit ... end; Lord knows it would make teaching beginning programming easier. My favorite example is loop Put(prompt to user); Get(user input); exit when user input is okay; Put(the input was not correct because...); end loop; rather than the more complex, ugly, and potentially less efficient declare Boolean Input_Okay =3D False; begin while not Input_Okay loop Put(prompt to user); Get(user input); Input_Okay :=3D test for user input is okay; if not Input_Okay then Put(the input was not correct because...); end if; end while; end; And my experience is that a language construct that is good for beginning programmers is always good for general practice.