FOR NEXT ?

بواسطة Hit-Biter

Master (175)

صورة Hit-Biter

22-08-2022, 13:44

Hey guys, Just wish to clear up a Syntax issue ! Ive been typing in a program recently and notice a few FOR NEXT loops. These are followed by a NEXT statement as you would expect. However I've noticed they do not always have the variable stated with the NEXT statement ? Is it necessary to specify the variable with the Next statement ? e.g.

FOR T=1 TO 32:NEXT T

or

FOR T=1TO 32:NEXT

which is correct or are BOTH equally valid ?

Login أوregister لوضع تعليقاتك

بواسطة gdx

Enlighted (6439)

صورة gdx

22-08-2022, 14:00

If I remember well, it's a little slow with a variable behind NEXT.

بواسطة Hit-Biter

Master (175)

صورة Hit-Biter

22-08-2022, 14:14

But both will work ? And would there be instances where you would HAVE to specify the variable like maybe in a nested loop within a loop ?

بواسطة mars2000you

Enlighted (6562)

صورة mars2000you

22-08-2022, 14:16

Both will work. Specifying the variables makes the code easier to read and helps to avoid mistakes when developing the game.

بواسطة Hit-Biter

Master (175)

صورة Hit-Biter

22-08-2022, 14:21

Thanks to you both for this clarification. Sorry for my newbie questions :/

بواسطة wolf_

Ambassador_ (10135)

صورة wolf_

22-08-2022, 14:26

As for code being easier to read: that's where indenting comes in.

بواسطة turbor

Hero (529)

صورة turbor

22-08-2022, 14:27

Also, if you use variables you can combine multiple variables in a nested loop:
for i = 0 to 2 : for j = 0 to 2 : print i,j : next j,i
In that case, do not forget to inverse the order of the variablenames Wink

Otherwise you could still write it as
for i = 0 to 2 : for j = 0 to 2 : print i,j : next : next