FOR NEXT ?

Por Hit-Biter

Master (166)

Imagen del 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 sesión o register para postear comentarios

Por gdx

Enlighted (6207)

Imagen del gdx

22-08-2022, 14:00

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

Por Hit-Biter

Master (166)

Imagen del 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 ?

Por mars2000you

Enlighted (6480)

Imagen del 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.

Por Hit-Biter

Master (166)

Imagen del Hit-Biter

22-08-2022, 14:21

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

Por wolf_

Ambassador_ (10109)

Imagen del wolf_

22-08-2022, 14:26

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

Por turbor

Hero (520)

Imagen del 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