James,
I see what you're saying with your two examples. So what's really more
similar is that both leave the variable i as 1 greater than it was last
processed within the loop (if that makes sense). So while i may = 10
after the first loop finishes, the loop only processed i from 0 to 9. I
think this is the commonality Simon was really trying to get at (correct
me if I'm wrong Simon, I don't want to put words in your mouth).
-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx
[mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of James Rich
Sent: Monday, February 02, 2009 3:45 PM
To: RPG programming on the IBM i / System i
Subject: Re: FOR loop limit
On Tue, 3 Feb 2009, Simon Coulter wrote:
generally 0-based. However, in all programming languages (at least
that I can think of), on for-loop termination, the index of the loop
is always 1 greater than the terminal value--regardless of the
starting value.
Not necessarily true, depending on how you think of "termination value".
In many languages, a common construct for for loops is the following:
for (i=0; i<10; i++)
In this case the loop terminates when i reaches a value of ten, i.e.
equal
to the termination value. Contrast the above to this:
for (i=1; i<=10; i++)
The above construct is common in RPG using the DO and FOR opcodes. In
this case i does reach a value which is one greater than the termination
value of ten.
Of course none of the above matters either way if you are coding a for
loop using an index that cannot fit the test required. If you use an
index that is 4,0 and your test is i < 10000 then it will never work
because you're index cannot ever get to your termination value of 10000.
The case is similar for i <= 9999. In my opinion, the first way of
coding
(i.e. the "C way") is more clear that your index needs to be large
enough
to reach the termination value. I think seeing "10000" makes the size
requirement jump off the page more than "9999".
James Rich
if you want to understand why that is, there are many good books on
the design of operating systems. please pass them along to redmond
when you're done reading them :)
- Paul Davis on ardour-dev
As an Amazon Associate we earn from qualifying purchases.