Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Option _Explicit Keyword(s) of day XXX:
#13
(06-07-2023, 02:47 PM)Dimster Wrote: Once the loop is finished the control variable carries a value of Loop Limit +1. 

If I do not DIM a control variable or I avoid using Option _ Explicit 
 - I'm in less danger of an unexpected value to the control variable.

I hope you never use STEP in the same line as FOR in your programs because that statement of "loop is finished" isn't always true.

OPTION _EXPLICIT has nothing to do with a loop variable being changed without being expected. If a loop variable isn't going how it's expected for FOR... NEXT then there is bad code between those two keywords.

A lot of people are getting by with single-precision variables as FOR... NEXT control variables because they hate that much to use OPTION _EXPLICIT and/or to use DIM to make sure the variables are integers. That's OK with me by this time. But otherwise please investigate the code that goes on inside the loop -- be careful with anything assigned to the variable related to FOR.

Code: (Select All)
FOR x = 1 to 10
  x = computesomethingcomplex() '<-- be careful with a rogue line like this! The "FOR" should be the only one with "x is assigned".
  '(rest of loop)
NEXT

Try not to use colons excessively to cut down on lines of code which makes code difficult to read and makes it harder to look for such rogue assignments.

I just thought of something else. Be careful also with something like this:

Code: (Select All)
DIM AS INTEGER x, y

FOR x = 1 to 10
  callsomething x, y
  PRINT x
NEXT

SUB callsomething (x as integer, y as integer)
  x = y * 2
END SUB

Note that I have used DIM for "x" and "y" to better match the SUB definition. It's possible for the loop control variable to be sent as parameter to a SUB or FUNCTION, which could get changed by the subprogram.

We need to build a serious debugger for you. Smile
Reply


Messages In This Thread
RE: Option _Explicit Keyword(s) of day XXX: - by mnrvovrfc - 06-07-2023, 05:16 PM



Users browsing this thread: 3 Guest(s)