numeric constants and the Step of for/next

Just starting out? Need help? Post your questions and find answers here.
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

numeric constants and the Step of for/next

Post 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.
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Re: numeric constants and the Step of for/next

Post by Jeromyal »

I am embarrassing myself once again.
I am pretty sure I can accomplish this with while/wend
I am sorry.
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: numeric constants and the Step of for/next

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: numeric constants and the Step of for/next

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: numeric constants and the Step of for/next

Post 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"
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: numeric constants and the Step of for/next

Post 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.
Post Reply