Jump to content

Slow Reaction.. or Loop +1 [Solved]


Recommended Posts

I am doing some Loops, and realized that sometimes the Loop repeats 1 more time than intended, even though the Until is forfilled.

Why is that?

Edit: "The expression is tested after the loop is executed, so the loop will be executed one or more times."

Perhaps that solves the Why.. but not how to prevent it

Edited by Schoening
Link to comment
Share on other sites

The expression 'For $i = 1 To 5' will execute the code contained within the loop 5 times, since the script exits the loop when $i becomes equal to 6. There is no mystery here. The only confusion occurs when looping through a zero based array (or something similar). Then you would generally write something like 'For $i = 0 To 4'. This loop also runs 5 times and stops when $i becomes equal to 5.

Edited by czardas
Link to comment
Share on other sites

If I may add a thought to czardas's contribution here: The fact that your variable, $i, is used to loop say from '1 to 5' then the loop exits but outside of the loop $i = 6 is occasionally potentially handy; if you have a condition which may exit the loop early and you know that "For $i = 1 To 5" then you can test "If $i < 6" outside of the loop for proof of the loop's full cycle.

Link to comment
Share on other sites

If I may add a thought to czardas's contribution here: The fact that your variable, $i, is used to loop say from '1 to 5' then the loop exits but outside of the loop $i = 6 is occasionally potentially handy; if you have a condition which may exit the loop early and you know that "For $i = 1 To 5" then you can test "If $i < 6" outside of the loop for proof of the loop's full cycle.

How certain are you of that?

Is $i a Global/Local variable, or is it just a counter which exista only

in that loop?

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

How certain are you of that?

Is $i a Global/Local variable, or is it just a counter which exista only

in that loop?

I'm sorry, I did not expand on the assumption of what I meant '...you know that "For $i = 1 To 5"...' If you Know that the $i you are testing is the same $i in the same scope then you can use that variable in that way. If I am not able to keep track of whether a variable I am testing at the moment is the correct name for the variable I mean to reference in that scope or not then it might be best to carefuly to name every variable I use, in any project (in case a project is modified to support the possiblity of being utilized in part by other projects), a completely unique name every time, just to be sure. Perhaps something like "$the_integer_for_counting_the_number_of_times_I_looped_in_function_A_of_script_scriptadoodle", but now I'm just being silly because honestly, I meant in that statement a case where the individual is aware of what scope the variable being referenced is in and the "$i" was used as an example name only because I didn't have any more specific case in mind like "$iRowCount" or "$iLikeCountingLoops".

Link to comment
Share on other sites

  • Moderators

JohnOne,

I am absolutely certain. :)

From the Help file page for For...To...Step...Next:

"The Variable will be created automatically with a LOCAL scope, even when MustDeclareVars is on"

So the Variable will always have the value of stop + 1 if it runs through the loop without interruption. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...