Schoening Posted January 18, 2012 Share Posted January 18, 2012 (edited) 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 January 18, 2012 by Schoening Link to comment Share on other sites More sharing options...
JohnOne Posted January 18, 2012 Share Posted January 18, 2012 Use a While loop instead. 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 More sharing options...
enaiman Posted January 18, 2012 Share Posted January 18, 2012 (edited) Edit: JohnOne is right; initially I thought you were using the While/Wend. Edited January 18, 2012 by enaiman SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
Schoening Posted January 18, 2012 Author Share Posted January 18, 2012 had to rewrite and sacrifice some stuff but works now thx Link to comment Share on other sites More sharing options...
JohnOne Posted January 19, 2012 Share Posted January 19, 2012 I don't see why you have to sacrifice 'stuff'. So long as you're happy. 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 More sharing options...
czardas Posted January 19, 2012 Share Posted January 19, 2012 (edited) 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 January 19, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
RobGuy Posted January 19, 2012 Share Posted January 19, 2012 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 More sharing options...
JohnOne Posted January 19, 2012 Share Posted January 19, 2012 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 onlyin 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 More sharing options...
RobGuy Posted January 19, 2012 Share Posted January 19, 2012 How certain are you of that?Is $i a Global/Local variable, or is it just a counter which exista onlyin 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 More sharing options...
Moderators Melba23 Posted January 19, 2012 Moderators Share Posted January 19, 2012 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JohnOne Posted January 20, 2012 Share Posted January 20, 2012 Thanks for the clarification gentlemen. I don't know where I got the idea it would only exist within the 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now