From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: "continue/next" for "loop"
Date: Mon, 03 Nov 2003 20:23:55 -0500
Date: 2003-11-03T20:23:55-05:00 [thread overview]
Message-ID: <T7Gdnc0ndrIxYjuiRVn-jA@comcast.com> (raw)
In-Reply-To: <%txpb.5381$qh2.2068@newsread4.news.pas.earthlink.net>
Jeffrey Carter wrote:
>> goto exit; -- exit innermost enclosing loop.
>> exit loop; -- alternative syntax. ;-)
>
>
> This already exists in Ada; it's syntax is
>
> exit;
>
> Without a loop name or when part, exit exits the innermost enclosing loop.
Correct. I often use "exit when." Actually for the type of cases
discussed here, I often write the loop as an inlined procedure or
function and use a return statement for premature loop termination. (As
an example, walking an AVL tree looking for a particular key. The
return takes care of all the complex nesting issues that can occur.)
The goto exit was proposed as "syntactic sugar," since I often see
programmers using gotos where exit would do the job. I thought "exit
loop" was rubbing the sarcasm in even with the smiley. I have too many
times been asked how can I exit a loop. So I thought it was obvious I
was being sarcastic. (Maybe I should have said:
exit [loop_name]; -- alternative syntax. ;-)
Incidently, I the problem which results in exit statements being
overlooked is classes that teach for loops, then while loops and maybe
eventually get around to:
loop
...
exit when Done;
...
end loop;
If you teach students about the general purpose loop first, then add for
and while loops as special cases, maybe students will remember. I have
no problem. In the first Ada compiler I worked on, while loops were
parsed exactly as if they said "loop exit when not <expression>;
<sequence of statements> end loop;" No reason to generate extra work
for semantic analysis when we could eliminate some constructs in the
parser by translating them into token streams with identical meanings.
As for the horrible example that started this thread, I have often had
to deal with this type of twisted loop in parsing. Often the best
solution is instead of using two for loops use one plain loop, and make
incrementing the loop variables explicit. Sometimes that gets too heavy
and you really do have to go to named states with gotos.
> procedure t is
> begin
> for j in 2 .. 8 loop
> for i in 2 .. 8 loop
> if i mod j = 0 then -- Assume complex statements here.
> goto next; -- In my real case, it's another loop,
> end if; -- which detects the end of current path inside.
> Put(i'Img);
> <<next>> null;
> end loop;
> New_Line;
> end loop;
> end t;
procedure t is
begin
for j in 2 .. 8 loop
for i in 2 .. 8 loop
if i mod j = 0 then -- Assume complex statements here.
exit;
else
Put(i'Img);
end if;
end loop;
New_Line;
end loop;
end t;
Which in part was why I thought this was another case of "Help! I'm
stuck inside a loop and can't get out."
--
Robert I. Eachus
100% Ada, no bugs--the only way to create software.
next prev parent reply other threads:[~2003-11-04 1:23 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-11-03 12:55 "continue/next" for "loop" amado.alves
2003-11-03 13:06 ` Lutz Donnerhacke
2003-11-03 13:07 ` Lutz Donnerhacke
2003-11-03 13:25 ` Peter Hermann
2003-11-03 16:49 ` Robert I. Eachus
2003-11-03 18:21 ` (see below)
2003-11-03 18:52 ` Jeffrey Carter
2003-11-03 20:11 ` Lutz Donnerhacke
2003-11-04 1:23 ` Robert I. Eachus [this message]
2003-11-03 13:33 ` James Rogers
2003-11-03 13:46 ` Lutz Donnerhacke
2003-11-03 13:39 ` Dmitry A. Kazakov
2003-11-03 13:54 ` Ole-Hjalmar Kristensen
2003-11-03 14:56 ` Lutz Donnerhacke
2003-11-03 15:08 ` Stefan Lucks
2003-11-03 15:40 ` Lutz Donnerhacke
2003-11-05 10:36 ` Charles Lindsey
2003-11-05 18:05 ` Lutz Donnerhacke
2003-11-06 9:48 ` Martin Dowie
2003-11-07 14:54 ` Charles Lindsey
2003-11-07 16:24 ` Martin Dowie
2003-11-07 17:34 ` Jeffrey Carter
2003-11-05 14:45 ` Jim Rogers
-- strict thread matches above, loose matches on Subject: below --
2003-11-03 15:44 amado.alves
2003-11-03 22:12 ` Dmytry Lavrov
2003-11-03 22:27 ` Gautier Write-only
2003-11-03 13:54 amado.alves
2003-11-03 13:45 christoph.grein
2003-11-03 14:23 ` Preben Randhol
2003-11-03 15:01 ` Lutz Donnerhacke
2003-11-03 15:19 ` Dmitry A. Kazakov
2003-11-03 18:33 ` Chad R. Meiners
2003-11-03 15:48 ` Preben Randhol
2003-11-03 18:50 ` Georg Bauhaus
2003-11-03 15:00 ` Lutz Donnerhacke
2003-11-03 10:48 Lutz Donnerhacke
2003-11-03 10:51 ` Preben Randhol
2003-11-03 10:55 ` Preben Randhol
2003-11-03 11:01 ` Lutz Donnerhacke
2003-11-04 3:11 ` Steve
2003-11-05 15:54 ` sk
2003-11-06 15:40 ` Stephen Leake
2003-11-06 18:27 ` sk
2003-11-06 15:39 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox