? Why does this code work? (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password
Contest results!

Why does this code work?

  • #1 ✎ 142 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies 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? Posted
  • #2 ✎ 1507 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS OSP Contest 2 Winner I won the second SmileBASIC Source OSP Contest! Programming Contest Second Year My account is over 2 years old Website
    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 Anot 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)
    Posted Edited by 12Me21