Page 1 of 1
numeric constants and the Step of for/next
Posted: Tue Oct 17, 2023 2:33 pm
by Jeromyal
I have arrived at a situation where I need to set a value of to step with for next statements upon initialization of a program
The compiler complains that it needs to be a numeric constant if I try to use an integer variable.
Is there a way around this?
I need to functionally increment var1 to var2 with step var3 in a loop.
Re: numeric constants and the Step of for/next
Posted: Tue Oct 17, 2023 2:36 pm
by Jeromyal
I am embarrassing myself once again.
I am pretty sure I can accomplish this with while/wend
I am sorry.
Re: numeric constants and the Step of for/next
Posted: Tue Oct 17, 2023 3:37 pm
by mk-soft
As a reminder
Code: Select all
varFrom.d = 10
varTo.d = 20
varStep.d = 0.5
i.d = varFrom
While (i <= varTo)
a.d = i
Debug a
i + varStep
Wend
Re: numeric constants and the Step of for/next
Posted: Tue Oct 17, 2023 10:08 pm
by BarryG
It's always seemed strange to me that PureBasic doesn't accept floats for the "Next" parameter. Every other BASIC can do it without making the user resort to another type of loop.
Re: numeric constants and the Step of for/next
Posted: Tue Oct 17, 2023 10:50 pm
by STARGĂ…TE
Due to the nature of floats and doubles, you cannot simply use floats or doubles as step value and iterate until it reaches a specific value.
In certain cases such approach would lead to unexpected behaviors, if it would be used in a For:Next:Step loop.
Code: Select all
Define x.d, y.d
x = 0.0
While x <= 0.6
Debug "x = "+StrD(x)
x + 0.2
Wend
Debug "0.6 is missing"
Debug "----"
y = 0.0
Repeat
y + 0.1
Debug "y = "+StrD(y)
Until y >= 1.0
Debug "1.1 is too much"
Re: numeric constants and the Step of for/next
Posted: Tue Oct 24, 2023 2:57 am
by moricode
For/Next in PB has many many limitation and restriction ,
by which , all loops can be done in WHILE/WEND or Repeat/Forever , for/next could be avoided if possible.
in other language (eg. C) , for/next is more capable suppose.