FOR NEXT ?

By Hit-Biter

Master (163)

Аватар пользователя 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

By gdx

Enlighted (5823)

Аватар пользователя gdx

22-08-2022, 14:00

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

By Hit-Biter

Master (163)

Аватар пользователя 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 ?

By mars2000you

Enlighted (6346)

Аватар пользователя 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.

By Hit-Biter

Master (163)

Аватар пользователя Hit-Biter

22-08-2022, 14:21

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

By wolf_

Ambassador_ (10032)

Аватар пользователя wolf_

22-08-2022, 14:26

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

By turbor

Champion (507)

Аватар пользователя 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