comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: ada loops
Date: 30 May 2003 12:17:41 -0400
Date: 2003-05-30T12:17:41-04:00	[thread overview]
Message-ID: <wcc1xyg9zoa.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: d3ba4d4c.0305300744.520de992@posting.google.com

rm@gunlab.com.ru (Roman V. Isaev) writes:

> I've got bitten by this (code was translated from pascal by p2ada, so 
> please don't bash using goto):
> 
>       for Ijk in 1 .. 40 loop
>          Sss:=Fnmis-Si(Ijk);
>          if  Sss<0.0 then
>             goto Label_1;
>          end if;
>       end loop; --  IJK
>       <<Label_1>>    null;
>
> Before the loop Ijk is 0, after the loop Ijk is still 0. Huh? Why?

Ijk does not even exist before or after the loop -- the "for Ijk"
*declares* it.  This is different from the way Pascal works.

You didn't show your whole example, but I guess you declared another
variable called Ijk outside the loop, initialized it to 0, and then
never modified it again.

A good Ada compiler will warn you about this situation, presuming you
turn on warnings.

IMHO, hiding is evil.  If I were the boss, the inner Ijk would not hide
the outer one, and the attempted reference to the inner one would be
ambiguous and therefore illegal.

>... I 
> replaced Ijk with temp variable and put Ijk := temp before goto and 
> it solved the problem, but why we can't have final value when we break
> out of the loop? What's the logic behind this language behaviour?

So I can be constant inside the loop.  The way to think of it is that a
new constant called I is created and destroyed *each* time through the
loop.

But as you can see, it backfires if you're used to the way Pascal works.

Actually, doesn't the Pascal rule say that the value of I is *undefined*
after a for loop?  It's been a long time since I used Pascal...

> Also when I try to debug this in GVD I can see Ijk variable, but not Sss 
> (it's a global variable described in outer procedure). I see following
> message in the bottom window:
> 
> (gdb) print Sss
> Cannot access memory at address 0x24803ec
> 
> How to look up these variables?

I see that sort of thing all the time from gdb.  I don't know what you
can do, except complain to your compiler vendor.

- Bob



  parent reply	other threads:[~2003-05-30 16:17 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-30 15:44 ada loops Roman V. Isaev
2003-05-30 16:00 ` Bill Findlay
2003-05-30 16:17 ` Robert A Duff [this message]
2003-05-30 16:46   ` Bill Findlay
2003-05-30 16:51   ` Bill Findlay
2003-05-30 17:25 ` Stephen Leake
2003-05-30 19:40   ` Robert A Duff
2003-05-30 21:33     ` Pascal Obry
2003-05-31 16:19       ` Robert A Duff
2003-05-31 16:29       ` Robert A Duff
2003-05-30 21:38     ` Simon Wright
2003-05-30 21:51       ` chris.danx
2003-05-31  6:40         ` Pascal Obry
2003-05-31 16:21         ` Robert A Duff
2003-06-02 16:39       ` Stephen Leake
2003-05-31 16:24     ` Oliver Kellogg
2003-05-31 23:15       ` Preben Randhol
2003-06-05  5:14   ` Dave Thompson
2003-06-05 18:15     ` 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