Notifications
Clear all
[Closed] Newbie question: get last for-loop variable value
Jan 27, 2018 12:24 pm
Is there an easy way without losing performance to get the last value of a for-loop variable?
Example:
for i = 0 to 9 by 2 do (); print i
I logically get ‘undefined’ instead of ‘8’. I would like to avoid the use of an external variable and assign the value of ‘i’ to it in each loop.
In fact, what I need is to check ‘8’ against ‘9’ and, if they are different, do one more loop cycle with the last value (‘9’).
2 Replies
Jan 27, 2018 12:24 pm
If you know the iteration start, end and step values you could calculate the reminder:
fn GetIterationReminder start end step =
(
r = floor ((end-start)/double step)
return mod end ((r*step)+start)
)
Test:
(
clearlistener()
fn GetIterationReminder start end step =
(
r = floor ((end-start)/double step)
return mod end ((r*step)+start)
)
/* Test ================================ */
t = int -- double float int
start = random (t 0) (t 10)
end = random (t 20) (t 30)
step = random (t 1) (t 5)
last_i = 0
for i = start to end by step do last_i = i
r = GetIterationReminder start end step
format "start:% end:% step:%
" start end step
format "reminder: %
" r
format "result : %
" (r+last_i)
/* ===================================== */
)