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

How to exit from "for t in C when f(t) loop" ?

120 views
Skip to first unread message

reinert

unread,
Oct 23, 2023, 1:03:01 AM10/23/23
to
Assume the loop:

for t of C loop
if f(t) then
do something....
elsif f(t) > f1 then
exit;
end if;
end loop;

I would like to simplify this construct (somehow) like this:

for t of C when f(t) loop
do something....
end loop;

However, this is not computationally effective, since t in this case runs through the whole C.
Any ideas for a compromise?

reinert

reinert

unread,
Oct 23, 2023, 3:04:30 AM10/23/23
to
Just a correction of the subject, it should be: How to exit from "for t *of* C when f(t) loop ? -reinert

min...@gmail.com

unread,
Oct 24, 2023, 10:32:58 PM10/24/23
to
Normally one does something
t := C'first;
loop
do something...
exit when Condition;
t := t + 1;
end loop;

The conditon is just a boolean expression. It can be as simple or as complicated as one desires.

reinert

unread,
Oct 27, 2023, 10:53:15 AM10/27/23
to
I would like an exit possibility somehow like this:

for t of C when f(t), but exit when f(t) loop
do something....
end loop;

Three lines less - keeping errors down :-)

reinert

Jere

unread,
Oct 27, 2023, 6:28:19 PM10/27/23
to
On Friday, October 27, 2023 at 10:53:15 AM UTC-4, reinert wrote:
> I would like an exit possibility somehow like this:
>
> for t of C when f(t), but exit when f(t) loop
> do something....
> end loop;
>
> Three lines less - keeping errors down :-)
>
> reinert
Would the new Ada22 feature of iterator filters help at all?
http://www.ada-auth.org/standards/22over/html/Ov22-4-6.html

AdaMagica

unread,
Oct 28, 2023, 10:42:10 AM10/28/23
to
reinert schrieb am Freitag, 27. Oktober 2023 um 16:53:15 UTC+2:
> I would like an exit possibility somehow like this:
>
> for t of C when f(t), but exit when f(t)
except when Y then skip next -- many more lines saved ;-)

Stephen Leake

unread,
Oct 28, 2023, 1:16:10 PM10/28/23
to
On Saturday, October 28, 2023 at 7:42:10 AM UTC-7, AdaMagica wrote:
> reinert schrieb am Freitag, 27. Oktober 2023 um 16:53:15 UTC+2:
> > I would like an exit possibility somehow like this:
> >
> > for t of C when f(t), but exit when f(t)
> except when Y then skip next -- many more lines saved ;-)
> > loop
> > do something....
> > end loop;
> >
'exit when condition;' is perfectly legal Ada.

It is more problematic that you are executing f(t) twice; that's either wrong if it has side effects, or wasteful if not.
And you are treating f(t) as returning a Boolean and a scalar!

for t of C loop
declare
A : foo renames f(t);
begin
exit when A; -- Boolean
exit when A >= f1; -- scalar
do something;
end;
end loop;
0 new messages