LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Why does this code work?

Root / Programming Questions / [.]

MZ952Created:
DEF F
DIM I
FOR I=0TO 10
DIM A=I?A
NEXT I
END
F
In this code, the variable A is dimensioned several times in the FOR loop. From experience I know that dimensioning a variable more than once leads to a duplicate variable error. Upon inspection, dimensioning it again seems to do absolutely nothing to the value of the variable. Why does this code work anyway?

DEF F
DIM I
FOR I=0TO 10
DIM A=I?A
NEXT I
END
F
In this code, the variable A is dimensioned several times in the FOR loop. From experience I know that dimensioning a variable more than once leads to a duplicate variable error. Upon inspection, dimensioning it again seems to do absolutely nothing to the value of the variable. Why does this code work anyway?
A duplicate definition only happens when you actually have VAR/DIM with the same variable twice in your code:
VAR A
VAR A
not
WHILE 1
 VAR A '<- there's only 1 VAR in the program
WEND
Basically, it ignores when the VAR is executed, and all that matters is the location in the code:
GOTO @SKIP
VAR A
@SKIP
A=1 'still works
If you use = in VAR, that will only run when VAR itself runs. (VAR A=1 is the same as VAR A:A=1, basically)