comp.lang.ada
 help / color / mirror / Atom feed
From: Gene <gene.ressler@gmail.com>
Subject: Re: Processing array subsections, a newbie question.
Date: Sat, 12 Jun 2010 18:20:26 -0700 (PDT)
Date: 2010-06-12T18:20:26-07:00	[thread overview]
Message-ID: <9e1f3f35-6f49-468f-be89-46d4f51f8193@f17g2000vbl.googlegroups.com> (raw)
In-Reply-To: 8739wsottd.fsf@ludovic-brenta.org

On Jun 12, 4:54 pm, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
> "Peter C. Chapin" <pcc482...@gmail.com> writes:
>
> > Ok        : Boolean := True;
> > Index_Fst : Natural := Buffer'First;
> > Index_Lst : Natural;
> > ...
>
> > while Ok and Index_Fst <= Buffer'Last loop
> >   Do_Something(Buffer, Index_Fst, Index_Lst, Ok);
> >   Index_Fst := Index_Lst + 1;
> > end loop;
>
> > The problem with this code is that the assignment to Index_Fst inside the loop
> > might raise Constraint_Error if Index_Lst = Buffer'Last after Do_Something
> > finishes. I can work around this problem but my solutions tend to be rather
> > ungainly looking. Surely there must be an easy way to handle this.
>
> How about:
>
> Ok        : Boolean := True;
> Index_Fst : Natural := Buffer'First;
> Index_Lst : Natural;
> ...
> loop
>    Do_Something(Buffer, Index_Fst, Index_Lst, Ok);
>    exit when not Ok;
>    exit when Index_Fst = Buffer'Last;
>    Index_Fst := Index_Lst + 1;
> end loop;

Yes!  Or save a line with

loop
   Do_Something(Buffer, Index_Fst, Index_Lst, Ok);
   exit when not Ok or Index_Fst = Buffer'Last;
   Index_Fst := Index_Lst + 1;
end loop;

Loop exit (with optional naming, which obviates the problems of
'break' in C and its successors) is one of the small beauties of Ada
and an example of the way things should have been - with benefit of
hindsight - in all ALGOL-like languages.  Rather than while ... and
do ... while ... , which are inherently limited in expressive power,
every language ought to offer loop ... exit ...  end; Lord knows it
would make teaching beginning programming easier.  My favorite example
is

loop
  Put(prompt to user);
  Get(user input);
  exit when user input is okay;
  Put(the input was not correct because...);
end loop;

rather than the more complex, ugly, and potentially less efficient

declare
  Boolean Input_Okay = False;
begin
  while not Input_Okay loop
    Put(prompt to user);
    Get(user input);
    Input_Okay := test for user input is okay;
    if not Input_Okay then
      Put(the input was not correct because...);
    end if;
  end while;
end;

And my experience is that a language construct that is good for
beginning programmers is always good for general practice.



  reply	other threads:[~2010-06-13  1:20 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-12 19:11 Processing array subsections, a newbie question Peter C. Chapin
2010-06-12 19:38 ` Yannick Duchêne (Hibou57)
2010-06-12 19:41 ` Yannick Duchêne (Hibou57)
2010-06-12 20:54 ` Ludovic Brenta
2010-06-13  1:20   ` Gene [this message]
2010-06-13 14:01     ` Peter C. Chapin
2010-06-13 15:48       ` Yannick Duchêne (Hibou57)
2010-06-13 16:57       ` Phil Thornley
2010-06-13 18:39         ` Yannick Duchêne (Hibou57)
2010-06-13 19:04           ` Phil Thornley
2010-06-13 18:58         ` Peter C. Chapin
2010-06-13  6:28   ` Niklas Holsti
2010-06-13  6:54     ` Jeffrey R. Carter
2010-06-16 19:03       ` Niklas Holsti
2010-06-16 19:22       ` Ludovic Brenta
2010-06-13 14:09     ` Peter C. Chapin
2010-06-13 11:00 ` Stephen Leake
2010-06-13 11:04   ` Simon Wright
2010-06-14  1:45     ` Stephen Leake
2010-06-14 18:23 ` Colin Paul Gloster
2010-06-14 19:41   ` Simon Wright
2010-06-14 23:54     ` Peter C. Chapin
2010-06-15  3:28       ` Jeffrey R. Carter
2010-06-15  6:13       ` Simon Wright
2010-06-15 11:24         ` Peter C. Chapin
2010-06-15  9:45       ` Phil Thornley
2010-06-15 11:27         ` Peter C. Chapin
2010-06-15 12:11           ` Yannick Duchêne (Hibou57)
replies disabled

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