Page 1 of 1

For - Next - Step

Posted: Wed Nov 30, 2022 12:27 pm
by bfernhout
In the documentation the step must be an constand. But in many Basic programs i see is that the step is not a constant but a integer variable.
Why is this in PureBasic a constant. Can it be made to an Integer variable.

This I found out becaus i try to use the step with a variable but PB will not allow this.

Code: Select all

 
    X2=x1+35*dx
    dx=dx*4
    DisplayTransparentSprite(BombSprite(0),x1,y1)
    For i=x1 To X2 Step dx ;<-- this is a variable while PB want it to be a constant
      DisplayTransparentSprite(BombSprite(0),i,y1)
      DisplayTransparentSprite(BombSprite(0),i + dx,y1)
    Next
    
In this peace of code i got the error. Cause dx is not allowed. While dx is a integer. In this code the sprite wil go faster every step the program is comming here.

Re: For - Next - Step

Posted: Wed Nov 30, 2022 1:05 pm
by STARGĂ…TE

Re: For - Next - Step

Posted: Fri Dec 02, 2022 1:56 pm
by gally
Hello,

juste :

Code: Select all

x1 = 0
x2 = (X1+1) * 100
dx = (X1+1) * 10

For i=x1 To x2 Step 1
  Debug i
  i = i + (dx - 1)
Next i
best regard

Re: For - Next - Step

Posted: Fri Dec 02, 2022 2:57 pm
by NicTheQuick
Better use just a simple while loop:

Code: Select all

x1 = 0
x2 = (X1+1) * 100
dx = (X1+1) * 10

i = x1
While i <= x2
	Debug i
	i + dx
Wend

Re: For - Next - Step

Posted: Fri Dec 02, 2022 5:33 pm
by Jeff8888
I assume there are valid reasons for step be a constant. Interesting to me is that stop value can be changed inside the for loop, I am not sure why one would want to do this. Here is code to show this:

Code: Select all

first=1
last=10
For i=first To last
  Debug i
Next
For i=first To last
  last=last-1
  Debug i
Next
End