comp.lang.ada
 help / color / mirror / Atom feed
* Re: Simple Ada question
       [not found] <8f8fm5$de5$1@news.uit.no>
  2000-05-16  0:00 ` Simple Ada question Gisle S�lensminde
  2000-05-16  0:00 ` Jeff Carter
@ 2000-05-16  0:00 ` Robert A Duff
  2000-05-18  0:00   ` John English
  2000-05-16  0:00 ` tmoran
  3 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2000-05-16  0:00 UTC (permalink / raw)


reinert@ola.npolar.no (Reinert Korsnes) writes:

>    n,I : Integer;
>...
>        for I in 1 .. 10_000_000 loop

> Why it says "I is never assigned a value" ?

You have two different I's.  The first one is never assigned a value,
and not used in any way.  The 'for' loop declares a new I, which hides
the outer I.

You're writing this the way you're supposed to do it in Pascal.  In Ada,
there's no need to declare the 'for' loop variable, because it's
automatically declared.  If you *do* declare a(nother) I, it will be a
completely separate variable.  If you reference the outer I after the
loop, you're refering to an uninitialized variable.

- Bob




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

* Re: Simple Ada question
       [not found] <8f8fm5$de5$1@news.uit.no>
                   ` (2 preceding siblings ...)
  2000-05-16  0:00 ` Robert A Duff
@ 2000-05-16  0:00 ` tmoran
  3 siblings, 0 replies; 9+ messages in thread
From: tmoran @ 2000-05-16  0:00 UTC (permalink / raw)


>test1.adb:8:06: warning: "I" is never assigned a value

Line 8 of the program is
   n,I : Integer;
and in column 6 we see the variable I defined.  It is never used
in the rest of the program.

  Probably you are confused because some programming languages
would require this declaration of I and would use that I in the loop
       for I in 1 .. 10_000_000 loop
whereas in Ada the loop variable is a brand new one, automatically
created, in this case with the name I.  eg,

       for I in 1 .. 10_000_000 loop
         ...
       end loop;

acts like:

   declare
       I : Integer;
   begin
       for I in 1 .. 10_000_000 loop
         ...
       end loop;
   end;

So you can drop the declaration of "I" on line 8.




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

* Re: Simple Ada question
       [not found] <8f8fm5$de5$1@news.uit.no>
  2000-05-16  0:00 ` Simple Ada question Gisle S�lensminde
@ 2000-05-16  0:00 ` Jeff Carter
  2000-05-16  0:00 ` Robert A Duff
  2000-05-16  0:00 ` tmoran
  3 siblings, 0 replies; 9+ messages in thread
From: Jeff Carter @ 2000-05-16  0:00 UTC (permalink / raw)


You have two things named I. One is the variable declared on line 8 and
never assigned a value, and the other is the for-loop counter. Since you
never use the variable I, you can get rid of it.

In addition, N is a constant and should be declared as such:

N : constant := 50_000;

-- 
Jeff Carter
"I blow my nose on you."
Monty Python & the Holy Grail




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

* Re: Simple Ada question
       [not found] <8f8fm5$de5$1@news.uit.no>
@ 2000-05-16  0:00 ` Gisle S�lensminde
  2000-05-16  0:00 ` Jeff Carter
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Gisle S�lensminde @ 2000-05-16  0:00 UTC (permalink / raw)


In article <8f8fm5$de5$1@news.uit.no>, Reinert Korsnes wrote:
>
>
>Given the following simple test program which I try using gnat-3.12p-11
>under Linux :
>
>-----------------------------------------------------------------------
>
>with Ada.Numerics.Generic_Elementary_Functions,Text_IO;
>procedure test1 is
>   package Flt_Io is new Text_IO.Float_Io   (Float);
>   package Int_Io is new Text_IO.Integer_Io (Integer);
>   package E_F is new
>                  Ada.Numerics.Generic_Elementary_Functions (Float);
>   use E_F,Flt_Io,Int_Io;
>   n,I : Integer;
>   x : Float;
>   begin
>     x := 10.0;
>     n := 50000;
>       for I in 1 .. 10_000_000 loop
>          x := x + Float(I)/1000_000.0;
>          x := x*0.5 + 1.0 + sin(x)*cos(x) + sin(x) + cos(x) +
>                             sin(x)*sin(x) + cos(x)*cos(x);
>          if I mod n = 0 then
>             Put(I); Put (x,3,4,0);Put(Integer(Float'Floor(x)));
>             Text_IO.New_Line (1);
>          end if;
>       end loop;
>end;
>------------------------------------------------------------------------
>
>I get from "gnatmake test1.adb":
>
>gnatgcc -c test1.adb
>test1.adb:8:06: warning: "I" is never assigned a value
>gnatbind -x test1.ali
>gnatlink test1.ali
>

In Ada, the index variable in the loop is implicitly defined in
the for loop statment, and only in the scope the for-loop defines.
That means that the index variable overrides the I variable you 
decleard in the declaration section of the main procedure. 

The following code is legal ada.

with ada.text_io;

procedure test is
begin
  for I in 1 .. 100 loop
    ada.text_IO.put_line(integer'image(I));
  end loop;
end;

-- 
--
Gisle S�lensminde ( gisle@ii.uib.no )   

ln -s /dev/null ~/.netscape/cookies




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

* Re: Simple Ada question
  2000-05-16  0:00 ` Robert A Duff
@ 2000-05-18  0:00   ` John English
  2000-05-21  0:00     ` Robert Dewar
  0 siblings, 1 reply; 9+ messages in thread
From: John English @ 2000-05-18  0:00 UTC (permalink / raw)


Robert A Duff wrote:
> 
> reinert@ola.npolar.no (Reinert Korsnes) writes:
> 
> >    n,I : Integer;
> >...
> >        for I in 1 .. 10_000_000 loop
> 
> > Why it says "I is never assigned a value" ?
> 
> You have two different I's.  The first one is never assigned a value,
> and not used in any way.  The 'for' loop declares a new I, which hides
> the outer I.

I've seen enough students fall over this one that I wonder if a
compiler warning should be issued if the loop index has the same
name as a variable in the enclosing scope...

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




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

* Re: Simple Ada question
  2000-05-18  0:00   ` John English
@ 2000-05-21  0:00     ` Robert Dewar
       [not found]       ` <Pine.A41.3.96-heb-2.07.1000521193502.30412B-100000@pluto.mscc.huji.ac.il>
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Dewar @ 2000-05-21  0:00 UTC (permalink / raw)


In article <3923B647.82A765D4@bton.ac.uk>,
  John English <je@bton.ac.uk> wrote:
> I've seen enough students fall over this one that I wonder if
a
> compiler warning should be issued if the loop index has the
same
> name as a variable in the enclosing scope...


In practice, GNAT will almost always give a warning in this
case. If the outer variable is not referenced at all, then
that will certainly generate a warning. If the outer variable
is referenced after the loop, then a warning will be generated
about reference to an uninitialized variable.

This will not catch 100% of cases, but it's very close


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Simple Ada question
       [not found]       ` <Pine.A41.3.96-heb-2.07.1000521193502.30412B-100000@pluto.mscc.huji.ac.il>
@ 2000-05-22  0:00         ` John English
  2000-05-22  0:00           ` David C. Hoos, Sr.
  2000-05-22  0:00           ` Ehud Lamm
  0 siblings, 2 replies; 9+ messages in thread
From: John English @ 2000-05-22  0:00 UTC (permalink / raw)


Ehud Lamm wrote:
> On Sun, 21 May 2000, Robert Dewar wrote:
> 
> |In article <3923B647.82A765D4@bton.ac.uk>,
> |John English <je@bton.ac.uk> wrote:
> |> I've seen enough students fall over this one that I wonder if a
> |> compiler warning should be issued if the loop index has the same
> |> name as a variable in the enclosing scope...
> |
> |In practice, GNAT will almost always give a warning in this
> |case. If the outer variable is not referenced at all, then
> |that will certainly generate a warning. If the outer variable
> |is referenced after the loop, then a warning will be generated
> |about reference to an uninitialized variable.
> |
> |This will not catch 100% of cases, but it's very close
> 
> Actually a student came to me saying "I found a compiler bug" it says that
> i is not referenced, where as I clearly use it in the for loop....

Which is why I think a more explicit warning might be a good thing:
"loop index uses the same name as an existing object" or something
like that (although I'm sure the wording could be improved...) since
this is nearly always an indication of an error where the writer
believes that the two names will refer to the same thing.

Just a thought...

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




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

* Re: Simple Ada question
  2000-05-22  0:00         ` John English
  2000-05-22  0:00           ` David C. Hoos, Sr.
@ 2000-05-22  0:00           ` Ehud Lamm
  1 sibling, 0 replies; 9+ messages in thread
From: Ehud Lamm @ 2000-05-22  0:00 UTC (permalink / raw)


On Mon, 22 May 2000, John English wrote:

|Ehud Lamm wrote:
|> 
|> Actually a student came to me saying "I found a compiler bug" it says that
|> i is not referenced, where as I clearly use it in the for loop....
|
|Which is why I think a more explicit warning might be a good thing:
|"loop index uses the same name as an existing object" or something
|like that (although I'm sure the wording could be improved...) since
|this is nearly always an indication of an error where the writer
|believes that the two names will refer to the same thing.
|

I agree. That's why I gave this example. 

On the other hand, learning to understand compiler messages is also
important.

Ehud Lamm mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <== My home on the web 
Check it out and subscribe to the E-List- for interesting essays and more!

 






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

* Re: Simple Ada question
  2000-05-22  0:00         ` John English
@ 2000-05-22  0:00           ` David C. Hoos, Sr.
  2000-05-22  0:00           ` Ehud Lamm
  1 sibling, 0 replies; 9+ messages in thread
From: David C. Hoos, Sr. @ 2000-05-22  0:00 UTC (permalink / raw)



John English <je@bton.ac.uk> wrote in message
news:392934E9.FA87156E@bton.ac.uk...
> Ehud Lamm wrote:
> > On Sun, 21 May 2000, Robert Dewar wrote:
> >
> > |In article <3923B647.82A765D4@bton.ac.uk>,
> > |John English <je@bton.ac.uk> wrote:
> > |> I've seen enough students fall over this one that I wonder if a
> > |> compiler warning should be issued if the loop index has the same
> > |> name as a variable in the enclosing scope...
> > |
> > |In practice, GNAT will almost always give a warning in this
> > |case. If the outer variable is not referenced at all, then
> > |that will certainly generate a warning. If the outer variable
> > |is referenced after the loop, then a warning will be generated
> > |about reference to an uninitialized variable.
> > |
> > |This will not catch 100% of cases, but it's very close
> >
> > Actually a student came to me saying "I found a compiler bug" it says
that
> > i is not referenced, where as I clearly use it in the for loop....
>
> Which is why I think a more explicit warning might be a good thing:
> "loop index uses the same name as an existing object" or something
> like that (although I'm sure the wording could be improved...) since
> this is nearly always an indication of an error where the writer
> believes that the two names will refer to the same thing.
>
> Just a thought...
>
One Ada83 compiler issues a warning (IIRC) like "I' declared at line xxx
hides declaration at line yyy.







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

end of thread, other threads:[~2000-05-22  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <8f8fm5$de5$1@news.uit.no>
2000-05-16  0:00 ` Simple Ada question Gisle S�lensminde
2000-05-16  0:00 ` Jeff Carter
2000-05-16  0:00 ` Robert A Duff
2000-05-18  0:00   ` John English
2000-05-21  0:00     ` Robert Dewar
     [not found]       ` <Pine.A41.3.96-heb-2.07.1000521193502.30412B-100000@pluto.mscc.huji.ac.il>
2000-05-22  0:00         ` John English
2000-05-22  0:00           ` David C. Hoos, Sr.
2000-05-22  0:00           ` Ehud Lamm
2000-05-16  0:00 ` tmoran

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