comp.lang.ada
 help / color / mirror / Atom feed
* loop variable
@ 2008-09-29  1:54 jedivaughn
  2008-09-29  3:20 ` george.priv
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: jedivaughn @ 2008-09-29  1:54 UTC (permalink / raw)


What data_type is a loop variable
as an example
for i in 1..10
what data type is i?
and can I convert it to an integer?


-John



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

* Re: loop variable
  2008-09-29  1:54 loop variable jedivaughn
@ 2008-09-29  3:20 ` george.priv
  2008-09-29  8:52   ` Georg Bauhaus
  2008-09-29 10:38 ` Peter C. Chapin
  2008-09-29 14:54 ` Adam Beneschan
  2 siblings, 1 reply; 6+ messages in thread
From: george.priv @ 2008-09-29  3:20 UTC (permalink / raw)


On Sep 28, 9:54 pm, jedivaughn <jedivaugh...@gmail.com> wrote:
> What data_type is a loop variable
> as an example
> for i in 1..10
> what data type is i?
> and can I convert it to an integer?
>
> -John

In your example I will be Standard.Integer.  In case:

type Weekday_Type is (sun, mon, tue, wed, thu, fri, sat);
...
for I in Weekday_Type'Range loop

.or.

for I in mon..fri loop

it will be Weekday_Type.  See AARM 5.5: http://www.adaic.com/standards/05aarm/html/AA-5-5.html

George



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

* Re: loop variable
  2008-09-29  3:20 ` george.priv
@ 2008-09-29  8:52   ` Georg Bauhaus
  0 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2008-09-29  8:52 UTC (permalink / raw)


george.priv@gmail.com schrieb:
> On Sep 28, 9:54 pm, jedivaughn <jedivaugh...@gmail.com> wrote:
>> What data_type is a loop variable
>> as an example
>> for i in 1..10
>> what data type is i?
>> and can I convert it to an integer?

As an alternative, don't convert a loop
variable to an integer if possible; instead,
consider what the loop variable is used for,
and whether the numbers you could be make
a numeric (sub)type of their own.

You can then use a technique such as the one
already mentioned ('Range) in another posting.

(Integer tends to invite many "evil" things
such as bound errors, lack of distinction between
numeric data, and more, and there is nothing
gained when using predefined Ada Integer
instead of your own problem's numeric types
:-)



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

* Re: loop variable
  2008-09-29  1:54 loop variable jedivaughn
  2008-09-29  3:20 ` george.priv
@ 2008-09-29 10:38 ` Peter C. Chapin
  2008-09-30  7:52   ` Jean-Pierre Rosen
  2008-09-29 14:54 ` Adam Beneschan
  2 siblings, 1 reply; 6+ messages in thread
From: Peter C. Chapin @ 2008-09-29 10:38 UTC (permalink / raw)


jedivaughn wrote:

> What data_type is a loop variable
> as an example
> for i in 1..10
> what data type is i?

The subtype of a loop variable is the subtype used to define the loop's
range. Something like '1..10' is shorthand for 'Integer range 1..10' and
so in your example I has type Integer.

However, I echo the comments Georg Bauhaus made in his post: be careful
with conversions. They often (not always) mean you haven't defined your
types properly or that you haven't declared your variables with the most
appropriate types.

Peter



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

* Re: loop variable
  2008-09-29  1:54 loop variable jedivaughn
  2008-09-29  3:20 ` george.priv
  2008-09-29 10:38 ` Peter C. Chapin
@ 2008-09-29 14:54 ` Adam Beneschan
  2 siblings, 0 replies; 6+ messages in thread
From: Adam Beneschan @ 2008-09-29 14:54 UTC (permalink / raw)


On Sep 28, 6:54 pm, jedivaughn <jedivaugh...@gmail.com> wrote:
> What data_type is a loop variable
> as an example
> for i in 1..10
> what data type is i?
> and can I convert it to an integer?

If either of the expressions in the range had a particular type, "I"
would be of that type.  E.g.

   for I in 1..X

where X is declared with some user-defined integer type, then "I"
would have the same type as X.  The above case, where the range is
made up of two universal integers, is special, and the language
defines the type to be Standard.Integer in that case.  You can also
say something like

   for I in My_Int_Type range 1..10

and then "I" would have the type My_Int_Type.

                                -- Adam






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

* Re: loop variable
  2008-09-29 10:38 ` Peter C. Chapin
@ 2008-09-30  7:52   ` Jean-Pierre Rosen
  0 siblings, 0 replies; 6+ messages in thread
From: Jean-Pierre Rosen @ 2008-09-30  7:52 UTC (permalink / raw)


Peter C. Chapin a �crit :
> jedivaughn wrote:
> 
>> What data_type is a loop variable
>> as an example
>> for i in 1..10
>> what data type is i?
> 
> The subtype of a loop variable is the subtype used to define the loop's
> range. Something like '1..10' is shorthand for 'Integer range 1..10' and
> so in your example I has type Integer.
> 
> However, I echo the comments Georg Bauhaus made in his post: be careful
> with conversions. They often (not always) mean you haven't defined your
> types properly or that you haven't declared your variables with the most
> appropriate types.
> 
To concur with that:
The declaration of a loop control variable is the only case in Ada where 
you can define an object without giving (explicitely) its type in the 
same declaration - and it's unfortunate. Remember that you are always 
allowed (although not required) to specify the type explicitely:
    for I in My_Integer_Type range 1 .. 10 loop

Of course, there is a rule in AdaControl to check for loops that don't 
follow this advice ;-)

-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



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

end of thread, other threads:[~2008-09-30  7:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-29  1:54 loop variable jedivaughn
2008-09-29  3:20 ` george.priv
2008-09-29  8:52   ` Georg Bauhaus
2008-09-29 10:38 ` Peter C. Chapin
2008-09-30  7:52   ` Jean-Pierre Rosen
2008-09-29 14:54 ` Adam Beneschan

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