loop variable : ADA
This is a discussion on loop variable within the ADA forums in Programming Languages category; 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...
| ADA ADA programming language |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| as an example for i in 1..10 what data type is i? and can I convert it to an integer? -John |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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 :-) |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 |

