Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to implement a continue statement in Ada?

543 views
Skip to first unread message

Robert T. Sagris

unread,
Aug 31, 1998, 3:00:00 AM8/31/98
to
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

Norman H. Cohen

unread,
Aug 31, 1998, 3:00:00 AM8/31/98
to

Robert T. Sagris wrote in message <35EA8153...@physics.purdue.edu>...

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

Robert I. Eachus

unread,
Aug 31, 1998, 3:00:00 AM8/31/98
to
In article <35EA8153...@physics.purdue.edu> "Robert T. Sagris" <ro...@physics.purdue.edu> writes:

> I was wondering if there is a general way of implementing
> the behavior of C's continue statement in Ada.

No, there are several techniques, but nothing which really covers
everything.

> If at all possible without using a goto statement.

Well, that works in all cases, but certainly is not the best in all
cases.

> 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++;
> }

Ouch! Assuming that "i" really is an iteration counter;

for I in 0 .. 9 loop
if A(I) /= 0
then A(I) := X / A(I);
end if;
end loop;

Seems like the natural translation, but I'm not sure that
answers your question.
--

Robert I. Eachus

with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

Robert T. Sagris

unread,
Aug 31, 1998, 3:00:00 AM8/31/98
to

It seems that people have mistaken my code example for the specific
instance were I needed a continue statement.

I only gave it so there was an example of a C use of the continue
statement.
What I was asking; how would implement the code given, which realy is
just a general example, in Ada. Specifically so you could use it any any
loop were you needed to skip an iteration.

Thanks for the answers so far.

Robbi Sagris

dew...@my-dejanews.com

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to
In article <35EA816A...@physics.purdue.edu>,

"Robert T. Sagris" <ro...@physics.purdue.edu> wrote:
> 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
>

The only general way of implementing a continue is to use a goto.
Use a stylized approach like

This_Loop : loop
...
if ... then
goto Continue_This_Loop;
end if;
...
...
<<Continue_This_Loop>> null;
end loop This_Loop;

This works fine, unless you have some inexplicable allergy to
typing G-O-T-O :-)

See the archives for a recent discussion of the use of gotos
for this purpose.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Dr Richard A. O'Keefe

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to
Robert T. Sagris wrote:
> 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.

Will you accept an 'exit'?

Loop statements can have names (LRM 5.5).
But so can blocks (LRM 5.6).

The general C form

for (init; cond; updt) stmt

can be converted to

begin
init;
Loop_All:
while cond loop
Loop_Body:
begin
stmt
end;
updt;
end loop
end;

'break' => exit Loop_All;
'continue' => exit Loop_Body;

Obvious simplifications apply to particular cases.

dew...@my-dejanews.com

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to
In article <35EB70...@atlas.otago.ac.nz>,
This is wrong. It is also a good reminder to PLEASE compile what
you suggest before you suggest it. I always do (I do not trust
my knowledge of Ada to be 100% reliable, especially in the rush
of replying to newsgroup articles).

The exit statement can ONLY be used to exit from a loop in Ada,
it cannot be used to exit from a block in the manner suggested
above. If you had compiled your suggestion you would have got
an error message similar to

k.adb:4:07: invalid loop name in exit statement

Note that this also serves as a reminder that you have to be
careful reading responses to CLA, do not assume that all posts
are technicaly correct.

It is possible to replace the block in Richard's example by a dummy
loop:

Loop_Body : loop
...
exit Loop_Body;
end Loop_Body;

but you really have to be quite allergic to gotos to prefer this to
the simple and straightforward use of a goto to achieve a continue.

Note also that the syntax in Richard's example is quite wrong (again
something that compiling it would have revealed). If a loop or block
is named, the name MUST be repeated on the end

Robert Dewar

Matthew Heaney

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to
"Robert T. Sagris" <ro...@physics.purdue.edu> writes:

> What I was asking; how would implement the code given, which really is


> just a general example, in Ada. Specifically so you could use it any
> any loop were you needed to skip an iteration.

As has been pointed out, THE way to implement continue in Ada is to use
a goto:

for I in X .. Y loop
<do some work>

if P then
goto Continue;
end if;

<do some more work>

<<Continue>> null;
end loop;


As Robert pointed out, if you label the loop, then you can include the
loop label in the name of the target of the goto (this is a stylistic
convention):

Paint_House:
for I in X .. Y loop
<do some work>

if P then
goto Continue_Paint_House;
end if;

<do some more work>

<<Continue_Paint_House>> null;
end loop Paint_House;


alan walkington

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to

Robert T. Sagris wrote in message <35EA8153...@physics.purdue.edu>...
>I was wondering if there is a general way of implementing
>the behavior of C's continue statement in Ada.


As you have seen from the responses, the answer is NO.

however, using psuedocode (so don't tell me it won't compile--:-)) ..

in C:

loop statement
if (conditional on iterator)
continue;
end if;

more code
end loop

you must INVERT the logic in ada:

loop statement
if NOT (conditional on iterator)
more code
end if
end loop

both these will skip 'more code' on the test of an iterator


Alan Walkington
Sr. Software Engineer
UDLP, San Jose
(remove the obvious from the e-mail address)
walkyA...@netmagic.net


Dr Richard A. O'Keefe

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
alan walkington wrote:
>
> Robert T. Sagris wrote in message <35EA8153...@physics.purdue.edu>...
> >I was wondering if there is a general way of implementing
> >the behavior of C's continue statement in Ada.
>
> As you have seen from the responses, the answer is NO.

Clearly I have missed some responses.
What exactly is wrong with
- Robert Dewar's suggestion of using 'goto', or
- my suggestion of using a nested named block and named exits?
Both suggestions "implement the behaviour", which is skipping the
rest of the body of the loop.
Is it the fact that you have to invent names,
which you do not in C?
Is it the fact that these translations are _too_ powerful,
handling multi-level exit, which the C version does not?

Matthew Heaney

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
"Dr Richard A. O'Keefe" <o...@atlas.otago.ac.nz> writes:

> What exactly is wrong with
> - Robert Dewar's suggestion of using 'goto', or
> - my suggestion of using a nested named block and named exits?

What do you mean by a "named block and named exit"? Are you suggesting
using an exit to terminate a block? That wouldn't be legal.

alan walkington

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to

Dr Richard A. O'Keefe wrote in message <35ECD5...@atlas.otago.ac.nz>...

>alan walkington wrote:
>>
>> Robert T. Sagris wrote in message
<35EA8153...@physics.purdue.edu>...
>> >I was wondering if there is a general way of implementing
>> >the behavior of C's continue statement in Ada.
>>
>> As you have seen from the responses, the answer is NO.
>
>Clearly I have missed some responses.

IMHO the original poster was looking for an atomic ada construct similar to
the 'continue' statement in C. We, including myself, responded with
'work-arounds' that achieved the same thing.

>What exactly is wrong with
> - Robert Dewar's suggestion of using 'goto', or

Some of us who write Ada for the US government are stuck with coding
standards which prohibit the use of 'goto' under any circumstance. Not good
CS, but we have to learn to live with it.

> - my suggestion of using a nested named block and named exits?

I don't know how to use the 'exit' statement to get out of a block.
Elucidate, dear doctor?

>Both suggestions "implement the behaviour", which is skipping the
>rest of the body of the loop.

Yes, that's also what my psuedo code does.

>Is it the fact that you have to invent names,
>which you do not in C?
>Is it the fact that these translations are _too_ powerful,
>handling multi-level exit, which the C version does not?

Please lets not get into a 'which language is better' mode. I've been using
Ada for almost 15 years now, and C for longer. Both Ada and C/C++ have
there uses.

Dr Richard A. O'Keefe

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to
dew...@my-dejanews.com wrote:
> Note also that the syntax in Richard's example is quite wrong (again
> something that compiling it would have revealed). If a loop or block
> is named, the name MUST be repeated on the end

I owe everyone an apology for that.
My only excuse is that there is no Ada compiler installed on either
of the machines I normally use (I do mean to fix that) so I wasn't
_able_ to test before posting.
(Yes, I do have the LRM on-line, but the HTML file names on the
disc are in upper case, and the references are in lower or mixed
case, which means none of the links work, so finding things was
a bit painful. That one I _have_ fixed now.)

I _was_ using Ada a fair bit last year, and readers will realise
from this that I had never actually _needed_ to emulate C's
'continue', or for that matter, multi-level loop exit.

dew...@my-dejanews.com

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to
In article <35ECD5...@atlas.otago.ac.nz>,

o...@atlas.otago.ac.nz wrote:
> alan walkington wrote:
> >
> > Robert T. Sagris wrote in message <35EA8153...@physics.purdue.edu>...
> > >I was wondering if there is a general way of implementing
> > >the behavior of C's continue statement in Ada.
> >
> > As you have seen from the responses, the answer is NO.
>
> Clearly I have missed some responses.
> What exactly is wrong with
> - Robert Dewar's suggestion of using 'goto', or
> - my suggestion of using a nested named block and named exits?
> Both suggestions "implement the behaviour", which is skipping the
> rest of the body of the loop.
> Is it the fact that you have to invent names,
> which you do not in C?
> Is it the fact that these translations are _too_ powerful,
> handling multi-level exit, which the C version does not?
>

Quite a bit is wrong with "my suggestion of using a nested named
block and named exit", namely neither Ada 95 nor Ada 83 permit
exiting from a named block!

denn...@telepath.com

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to
In article <35EDD4...@atlas.otago.ac.nz>,
o...@atlas.otago.ac.nz wrote:

> I _was_ using Ada a fair bit last year, and readers will realise
> from this that I had never actually _needed_ to emulate C's
> 'continue', or for that matter, multi-level loop exit.

I have used Ada for 9 years now, and have yet to tell myself, "gee, I really
wish I could do a continue". I think this is mostly an issue for C converts.

I' sure there are perfectly legitimate uses for it, but the two times I used
"continue" in C code, I probably shouldn't have.


--
T.E.D.

0 new messages