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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7596cfba54ad3207 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-07-03 11:59:28 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn1feed!wn2feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc01.POSTED!not-for-mail Message-ID: <3D234A17.2070508@attbi.com> From: "Robert I. Eachus" Organization: Eachus Associates User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4.1) Gecko/20020314 Netscape6/6.2.2 X-Accept-Language: en,pdf MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Trivial Ada question References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.61.239.24 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1025722767 24.61.239.24 (Wed, 03 Jul 2002 18:59:27 GMT) NNTP-Posting-Date: Wed, 03 Jul 2002 18:59:27 GMT Date: Wed, 03 Jul 2002 18:59:27 GMT Xref: archiver1.google.com comp.lang.ada:26849 Date: 2002-07-03T18:59:27+00:00 List-Id: "Reinert Korsnes" asked: >I would like to ask about a program construct: > >I want to test if some condition holds for all values >of I in A'range and in this case the variable >N should be incremented by one ( N := N + 1; ). >How can I do this most elegant and effective with Ada95 >(without "goto") ? >Assume I do not want to make a separate subroutine for this. > >This example is simple but not computationally optimal (two tests): > > for I in A'range loop > exit when not some condition(I); > if I = A'last then >-- Everything OK to the end: > N := N + 1; > end if; > end loop; This is fine. I don't know if your compiler will figure out how to merge the loop end and the assignment to N, but the necessary information is certainly there. However, that points out the reason you shouldn't even be asking the question. The code: for I in A'range loop if some_condition(I) then goto Skip; end if; end loop; N := N + 1; <> ... Is certainly short and sweet, and will compile to exactly what you want. Ah, but it uses that evil incarnate, a goto! Let's think about this for a minute. Why should you avoid gotos? Well because they can result in horrible things like loops with multiple exits. But that is precisely what we are trying to write here, a loop with two exits. And this is the problem with draconian no goto policies. The eliminate a symptom but don't address the underlying construct. That is fortunate, because sometimes you need those constructs. Notice that a compiler might generate identical code for these two examples. What makes one right and the other wrong?