comp.lang.ada
 help / color / mirror / Atom feed
From: Ole-Hjalmar Kristensen <ole-hjalmar.kristensen@substitute_employer_here.com>
Subject: Re: "continue/next" for "loop"
Date: 03 Nov 2003 14:54:17 +0100
Date: 2003-11-03T13:54:18+00:00	[thread overview]
Message-ID: <wvbr3cd5woae.fsf@sun.com> (raw)
In-Reply-To: slrnbqckoa.m1.lutz@taranis.iks-jena.de

>>>>> "LD" == Lutz Donnerhacke <lutz@iks-jena.de> writes:

    LD> * amado.alves wrote:
    >>> 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!

    LD> But both are the result of an oversimplification!

    LD> outer: loop
    LD>   inner: loop
    LD>     Complex_Actions_1;
    LD>     if First_Condition then
    LD>       Some_Justify_Actions;
    LD>       goto next_outer;
    LD>     end if;
    LD>     Complex_Actions_2;
    LD>     if Second_Condition then
    LD>       Some_Other_Justify_Actions;
    LD>       goto next_outer;
    LD>     end if;
    LD>     Complex_Actions_3;
    LD>   end loop inner;
    LD>   Complex_End_Statments;
    LD>   exit outer when Other_Condition;
    LD>   More_Statments;

    LD>   <<next_outer>> null;
    LD> end loop outer;

    LD> I'd like to find a more amazing version of this braindead goto.
    LD> Please do not assume, that I'm too stupid to invert a condition in a
    LD> simplified example. IT'S NOT HOMEWORK!

It seems you are looking for a multi-level "continue" statement :-)
You need to somehow store the results of your tests if you want to
avoid the goto, because this information is used outside the scope of
the inner loop. A boolean should suffice, but is the code any more
readable?

   outer: loop
     declare 
        execute_outer_statements : boolean := false;
     begin
       inner: loop
         Complex_Actions_1;
         if First_Condition then
           Some_Justify_Actions;
         else
           Complex_Actions_2;
           if Second_Condition then
             Some_Other_Justify_Actions;
           else        
             Complex_Actions_3;
             execute_statements := true;
           end if;
         end if;
       end loop inner;
       if execute_outer_statements then
         Complex_End_Statments;
         exit outer when Other_Condition;
         More_Statments;
       end if;
     end;
   end loop outer;

You could of course make your inner loop an inline procedure returning
a boolean to separate the logic of the inner and outer loops more clearly:

   outer: loop
     declare 
       procedure inner_loop(doit : out boolean) is
       begin 
         Complex_Actions_1;
         if First_Condition then
           Some_Justify_Actions;
           doit := false;
         else
           Complex_Actions_2;
           if Second_Condition then
             Some_Other_Justify_Actions;
             doit := false;
           else        
             Complex_Actions_3;
             doit := true;
           end if;
         end if;
       end inner_loop;
       pragma inline(inner_loop);
       
       execute_outer_statements : boolean;
     begin
       inner_loop(execute_outer_statements);
       if execute_outer_statements then
         Complex_End_Statments;
         exit outer when Other_Condition;
         More_Statments;
       end if;
     end;
   end loop outer;

After looking at these alternatives, I would probably stick with the goto.

-- 
Strange attractors stole my wife



  parent reply	other threads:[~2003-11-03 13:54 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
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 [this message]
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