comp.lang.ada
 help / color / mirror / Atom feed
* "continue/next" for "loop"
@ 2003-11-03 10:48 Lutz Donnerhacke
  2003-11-03 10:51 ` Preben Randhol
                   ` (3 more replies)
  0 siblings, 4 replies; 44+ messages in thread
From: Lutz Donnerhacke @ 2003-11-03 10:48 UTC (permalink / raw)


I might be out of coffee, but I do not find an elegant way to skip the rest
of a loop body for the current cycle other than using "goto".

with Ada.Text_IO;   use Ada.Text_IO;

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;




^ permalink raw reply	[flat|nested] 44+ messages in thread
* RE: "continue/next" for "loop"
@ 2003-11-03 12:55 amado.alves
  2003-11-03 13:06 ` Lutz Donnerhacke
  2003-11-03 13:07 ` Lutz Donnerhacke
  0 siblings, 2 replies; 44+ messages in thread
From: amado.alves @ 2003-11-03 12:55 UTC (permalink / raw)
  To: comp.lang.ada

>> You cannot do:
>> 
>>          if i mod j != 0 then
>
> No. I don't know how to invert a complex loop.

What on Earth are you talking about? The two constructs

(1)  if I mod J = 0 then
        goto next;
     end if;
     Put (I'Img);
     <<next>> null;

(2)  if I mod J /= 0 then
        Put (I'Img);
     end if;

are clearly equivalent!




^ permalink raw reply	[flat|nested] 44+ messages in thread
* Re: "continue/next" for "loop"
@ 2003-11-03 13:45 christoph.grein
  2003-11-03 14:23 ` Preben Randhol
  2003-11-03 15:00 ` Lutz Donnerhacke
  0 siblings, 2 replies; 44+ messages in thread
From: christoph.grein @ 2003-11-03 13:45 UTC (permalink / raw)
  To: comp.lang.ada

Really, Lutz, I do not see why you by any force would like a solution without 
goto. It's the most appropriate solution in this case!

The goto solution is better understandable than the boolean condition one. And 
it's simpler. Has anyone noticed that More_Statments are missing in the non-goto 
solution?

Why is the goto braindead? Your coding standards forbit it? I hate those silly 
standards which forbid the most natural solution.

> Lutz Donnerhacke <lutz@iks-jena.de> wrote in 
> news:slrnbqckoa.m1.lutz@taranis.iks-jena.de:
> 
> > But both are the result of an oversimplification!
> > 
> > outer: loop
> >   inner: loop
> >     Complex_Actions_1;
> >     if First_Condition then
> >       Some_Justify_Actions;
> >       goto next_outer;
> >     end if;
> >     Complex_Actions_2;
> >     if Second_Condition then
> >       Some_Other_Justify_Actions;
> >       goto next_outer;
> >     end if;
> >     Complex_Actions_3;
> >   end loop inner;
> >   Complex_End_Statments;
> >   exit outer when Other_Condition;
> >   More_Statments;
> > 
> >   <<next_outer>> null;
> > end loop outer;
> 
> declare
>    Outer_Continue : boolean;
> begin
> outer: loop
>    inner : loop
>       Complex_Actions_1;
>       if First_Condition then
>          Some_Justify_Actions;
>          Outer_Continue := false;
>          exit inner;
>       end if;
>       Complex_Actions_2;
>       if Second_Condition then
>          Some_Other_Justify_Actions;
>          Outer_Continue := false;
>          exit inner;
>       end if;
>       Outer_Continue := true;
>       Complex_Actions_3;
>    end loop inner;
>    if Outer_Continue then
>       Complex_End_Statements;
>       exit outer when Other_Condition;
>    end if;
> end loop outer;
> 
> 
> Jim Rogers



^ permalink raw reply	[flat|nested] 44+ messages in thread
* RE: "continue/next" for "loop"
@ 2003-11-03 13:54 amado.alves
  0 siblings, 0 replies; 44+ messages in thread
From: amado.alves @ 2003-11-03 13:54 UTC (permalink / raw)
  To: comp.lang.ada

Sometimes I use exceptions:

outer: loop
 DECLARE
  DONE : EXCEPTION;
 BEGIN
  inner: loop
    Complex_Actions_1;
    if First_Condition then
      Some_Justify_Actions;
      RAISE DONE; --goto next_outer;
    end if;
    Complex_Actions_2;
    if Second_Condition then
      Some_Other_Justify_Actions;
      RAISE DONE; --goto next_outer;
    end if;
    Complex_Actions_3;
  end loop inner;
  Complex_End_Statments;
  exit outer when Other_Condition;
  More_Statments;
  --<<next_outer>> null;
 EXCEPTION
  WHEN DONE => NULL;
 END;
end loop outer;





^ permalink raw reply	[flat|nested] 44+ messages in thread
* RE: "continue/next" for "loop"
@ 2003-11-03 15:44 amado.alves
  2003-11-03 22:12 ` Dmytry Lavrov
  2003-11-03 22:27 ` Gautier Write-only
  0 siblings, 2 replies; 44+ messages in thread
From: amado.alves @ 2003-11-03 15:44 UTC (permalink / raw)
  To: comp.lang.ada

"Sometimes I use exceptions..."

I remember some people are against using exceptions for control flow. I think this even appears in some coding standards.

So, as other have said, the goto might be appropriate.

Yet another way is to structure the code more, i.e. put the crucial sequences of statements inside (local) procedures, and then call the procedures at the appropriate points.

Of course you may have to parametrize the procedures to pass the values of the loop control variables.

Come to think of it, this is probably how I do it most of the times. I seem to remember having really long procedure names...




^ permalink raw reply	[flat|nested] 44+ messages in thread

end of thread, other threads:[~2003-11-07 17:34 UTC | newest]

Thread overview: 44+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-03 10:48 "continue/next" for "loop" 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
  -- strict thread matches above, loose matches on Subject: below --
2003-11-03 12:55 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
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
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 13:54 amado.alves
2003-11-03 15:44 amado.alves
2003-11-03 22:12 ` Dmytry Lavrov
2003-11-03 22:27 ` Gautier Write-only

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