For - Next - Step

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

For - Next - Step

Post 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.
From my first self made computer till now I stil like computers.
User avatar
STARGÅTE
Addict
Addict
Posts: 2063
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: For - Next - Step

Post by STARGÅTE »

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
User avatar
gally
User
User
Posts: 37
Joined: Thu May 28, 2009 9:07 pm
Location: France
Contact:

Re: For - Next - Step

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1218
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: For - Next - Step

Post 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
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Jeff8888
User
User
Posts: 38
Joined: Fri Jan 31, 2020 6:48 pm

Re: For - Next - Step

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