comp.lang.ada
 help / color / mirror / Atom feed
From: davidf@worf.mks.com (David J. Fiander)
Subject: Emulating Modula-3's FOR loop
Date: 1996/07/07
Date: 1996-07-07T00:00:00+00:00	[thread overview]
Message-ID: <4rocup$rvo@ia.mks.com> (raw)


I just ran across something minor, but annoying.  Suppose I've
got an array, indexed by the type Subscript, and I want to
operate on every n'th element of the array.  In Modula-3, I'd say
something like

	FOR i FROM n TO last BY n do
	    (* process the array elements *)
	END;

Now, if last is not divisible by n, this will still work, without
raising a Constraint_Error (or the modula equivalent).  But in
Ada, the obvious translation

	procedure Operate(Arr: in out Array(Subscript) of Elt;
			  N: in Subscript) is
	    I: Subscript := N;
	begin
	    while I <= Subscript'Last loop
	        -- Process the array
	        I := I + N;
	    end loop;
	end Operate;

will raise a Constraint_Error on the last addition.  Is there a
better way to deal with this than

	...
	begin
	    while I <= Subscript'Last loop
	        -- process
	        I := I + N;
	    end loop;
	exception
	    when Constraint_Error => null;
	end;





             reply	other threads:[~1996-07-07  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-07  0:00 David J. Fiander [this message]
1996-07-07  0:00 ` Emulating Modula-3's FOR loop Robert Dewar
replies disabled

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