comp.lang.ada
 help / color / mirror / Atom feed
From: "Norman H. Cohen" <ncohen@us.ibm.com>
Subject: Re: How to implement a continue statement in Ada?
Date: 1998/08/31
Date: 1998-08-31T00:00:00+00:00	[thread overview]
Message-ID: <6secvm$mj4$1@mdnews.btv.ibm.com> (raw)
In-Reply-To: 35EA8153.7BFC91E3@physics.purdue.edu


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

   <<End_Of_Iteration>> 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






       reply	other threads:[~1998-08-31  0:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <35EA8153.7BFC91E3@physics.purdue.edu>
1998-08-31  0:00 ` Norman H. Cohen [this message]
1998-08-31  0:00 ` How to implement a continue statement in Ada? Robert I. Eachus
1998-08-31  0:00   ` Robert T. Sagris
1998-09-01  0:00     ` Matthew Heaney
1998-09-01  0:00 ` alan walkington
1998-09-02  0:00   ` Dr Richard A. O'Keefe
1998-09-02  0:00     ` alan walkington
1998-09-02  0:00     ` Matthew Heaney
1998-09-03  0:00     ` dewarr
1998-08-31  0:00 Robert T. Sagris
1998-09-01  0:00 ` dewarr
1998-09-01  0:00 ` Dr Richard A. O'Keefe
1998-09-01  0:00   ` dewarr
1998-09-03  0:00     ` Dr Richard A. O'Keefe
1998-09-03  0:00       ` dennison
  -- strict thread matches above, loose matches on Subject: below --
2000-02-08  0:00 Oliver Kellogg
2000-02-08  0:00 ` David Starner
2000-02-08  0:00   ` Aidan Skinner
2000-02-09  0:00     ` David Starner
2000-02-10  0:00       ` Aidan Skinner
2000-02-09  0:00     ` Ted Dennison
2000-02-12  0:00       ` Jeff Carter
2000-02-14  0:00       ` Oliver Kellogg
2000-02-08  0:00 ` Ted Dennison
2000-02-09  0:00   ` Roger Barnett
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox