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,2cb6c27047140e0 X-Google-Attributes: gid103376,public From: "Norman H. Cohen" Subject: Re: How to implement a continue statement in Ada? Date: 1998/08/31 Message-ID: <6secvm$mj4$1@mdnews.btv.ibm.com>#1/1 X-Deja-AN: 386444107 References: <35EA8153.7BFC91E3@physics.purdue.edu> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Organization: IBM Microelectronics Division Newsgroups: comp.lang.ada Date: 1998-08-31T00:00:00+00:00 List-Id: Robert T. Sagris wrote in message <35EA8153.7BFC91E3@physics.purdue.edu>... >I was wondering if there is a general way of implementing >the behavior of C's continue statement in Ada. > >If at all possible without using a goto statement. > >Basically what I am asking: > >Is there a way to skip the current iteration of a loop >but continue with the normal progression of the loop. > >In C, I could write: > > i = 0; > while (i < 10) { > > if (a[i] == 0) { > i++; > continue; > } > > a[i] = x / a[i]; > i++; > } > >Thanks for any information you can give me. > >Robbi Sagris First of all, I find that most uses of "continue" reflect confusion about the control structure. For example, the loop above can be transformed into the MUCH clearer for (i=0; i < 10; i++) { if ( a[i] != 0 ) { a[i] = x/a[i]; } } Occasionally there is a legitimate use for something like "continue", when the decision to proceed to the next iteration is nested inside several levels of conditional statements. There are all sorts of tricks to achieve this effect without using a "goto" -- a local block that both declares and handles a local End_Of_Iteration exception so that "goto" can be spelled "raise" instead, moving the contents of the loop body into a function so that "goto" can be spelled "return" instead, surrounding the loop body with an inner loop that will be executed for only one iteration so that "goto" can be spelled "exit" instead -- but I find that the most straightforward, most readily understood and easily maintained, and most efficient solution in this situation is to spell "goto" as "goto". That is, add the labeled statement <> null; to the end of the loop to be continued, and write goto End_Of_Iteration; where a C programmer would write "continue". -- Norman Cohen