comp.lang.ada
 help / color / mirror / Atom feed
* Re: for-loop parameters
  1997-09-24  0:00 for-loop parameters CMSC 330-4021 Student 23
@ 1997-09-24  0:00 ` Tucker Taft
  1997-09-25  0:00   ` Charles Rose
  1997-09-25  0:00   ` Matthew Heaney
  0 siblings, 2 replies; 4+ messages in thread
From: Tucker Taft @ 1997-09-24  0:00 UTC (permalink / raw)



CMSC 330-4021 Student 23 (cs330a23@nova.umuc.edu) wrote:

: If a loop parameter in a for-loop statement should not be declared in the
: variable declaration list, how can the compiler determine what type the
: loop parameter belongs to? Thanks.  

The syntax for a "for" loop includes the ability to specify explicitly
the type of the loop parameter.  By default, it is determined by the
type of the low and high bound, and if both are "universal" integers, 
then it uses Standard.Integer as a fallback.

For example:

    for I in 1..10 loop  -- uses Standard.Integer

    for MI in My_Integer range 1..10 loop  -- uses My_Integer

    for J in An_Array'Range loop  -- uses index subtype of An_Array

    for K in 1..N loop  -- uses type of N (unless univesal, in which case
                        -- uses Standard.Integer)

    for C in Red .. Green loop  -- uses enum type of Red and Green
                                     (presuming there is only one
                                      such type with directly visible
                                      enumerals)

    for MC in My_Color range Red .. Green loop
                               -- in case there are 2 or more
                               -- enum types with directly visible
                               -- enumerals Red and Green

    for X in F(Y) .. G(Z) loop  -- uses result type of F and G

etc...

I would guess that in 98% of the cases, the explicit type can be
left out, especially if you typically avoid having "magic" numeric 
literals scattered about in your code.

See RM95 5.5(4) for the "for" loop syntax, and RM95 3.6(6,17-19)
for the explanation of discrete_subtype_definition.

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




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

* for-loop parameters
@ 1997-09-24  0:00 CMSC 330-4021 Student 23
  1997-09-24  0:00 ` Tucker Taft
  0 siblings, 1 reply; 4+ messages in thread
From: CMSC 330-4021 Student 23 @ 1997-09-24  0:00 UTC (permalink / raw)



If a loop parameter in a for-loop statement should not be declared in the
variable declaration list, how can the compiler determine what type the
loop parameter belongs to? Thanks.  





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

* Re: for-loop parameters
  1997-09-24  0:00 ` Tucker Taft
  1997-09-25  0:00   ` Charles Rose
@ 1997-09-25  0:00   ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1997-09-25  0:00 UTC (permalink / raw)



In article <EH0yCt.43p.0.-s@inmet.camb.inmet.com>,
stt@houdini.camb.inmet.com (Tucker Taft) wrote:


>The syntax for a "for" loop includes the ability to specify explicitly
>the type of the loop parameter.  By default, it is determined by the
>type of the low and high bound, and if both are "universal" integers, 
>then it uses Standard.Integer as a fallback.
>
>For example:
>
>    for I in 1..10 loop  -- uses Standard.Integer
>
>    for MI in My_Integer range 1..10 loop  -- uses My_Integer


I like the latter formulation - always try to assert the type of the index
explicitly.  For example, a better way to formulate the former would be

for I in Integer range 1 .. 10 loop

If I is used to index an array, then asserting the array index subtype
directly is better still (this can also turn some constraint checks off).

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: for-loop parameters
  1997-09-24  0:00 ` Tucker Taft
@ 1997-09-25  0:00   ` Charles Rose
  1997-09-25  0:00   ` Matthew Heaney
  1 sibling, 0 replies; 4+ messages in thread
From: Charles Rose @ 1997-09-25  0:00 UTC (permalink / raw)



Tucker Taft gave many examples of for loop parameters but left out

     for I in My_Subtype loop

which uses a loop index that ranges over all values of My_Subtype,
and is equivalent to

     for I in My_Subtype range My_Subtype'first..My_Subtype'last loop

When the loop index is an array index, I prefer to cite
the array object instead of the array index subtype, as in

>     for J in An_Array'Range loop  -- uses index subtype of An_Array

These two forms do not explicitly state the limits of the loop, but
rather infer them.  I use these forms whenever I can.




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

end of thread, other threads:[~1997-09-25  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-24  0:00 for-loop parameters CMSC 330-4021 Student 23
1997-09-24  0:00 ` Tucker Taft
1997-09-25  0:00   ` Charles Rose
1997-09-25  0:00   ` Matthew Heaney

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