From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: for-loop parameters
Date: 1997/09/24
Date: 1997-09-24T00:00:00+00:00 [thread overview]
Message-ID: <EH0yCt.43p.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: Pine.GSO.3.96.970924130910.6333B-100000@nova.umuc.edu
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
next prev parent reply other threads:[~1997-09-24 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-09-24 0:00 for-loop parameters CMSC 330-4021 Student 23
1997-09-24 0:00 ` Tucker Taft [this message]
1997-09-25 0:00 ` Matthew Heaney
1997-09-25 0:00 ` Charles Rose
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox