czardas Posted January 3, 2011 Share Posted January 3, 2011 (edited) My code involves a loop that runs between 0 and any number of times. I can't predict the number of times the loop needs to run. Even though the following code works fine, I just don't feel comfortable with the second statement when $i < 2. I was wondering if I ought to try and rewrite this, even though it works exactly the way I want it to work. Is there anything wrong with the following strange statement?For $j = 1 To 0 (step 1)For $i = 1 To 3 For $j = 1 To $i - 1 MsgBox(0, "","hello world") ; Or whatever code! Next MsgBox(0, "Number of completed loops", $j -1) NextIt seems illogical to me. Edited January 3, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Zedna Posted January 3, 2011 Share Posted January 3, 2011 For $j = 1 To 0 In each cycle it evaluates if $j > 0 (max) - if not then does the loop - if true (here we go now with max=0) then it exits the loop So I see no problem here. For/Next loop evaluates all for you Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
czardas Posted January 3, 2011 Author Share Posted January 3, 2011 (edited) Thanks for confirming that. I sometimes find myself in doubt over such things that appear odd like this. Edited January 3, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
MvGulik Posted January 3, 2011 Share Posted January 3, 2011 (edited) "In each cycle it evaluates if $j > 0(max)" ... thats for "step >= 0"- for "step < 0" ... its "$j < 0(max)" Edited January 3, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
czardas Posted January 3, 2011 Author Share Posted January 3, 2011 (edited) Yeah, it's just linguistically odd. The statements have no meaning outside of programming, and that's unusual. In real language 'For $j = 1 To 1 step 1' should never run, and 'For $j = 1 To 0 Step 1' should produce an infinite loop. Imagine how it would work with Do ... Until $j = 0 For $j = 1 To 1 evaluates to => While (Not $j > 1) Edited January 3, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
MvGulik Posted January 3, 2011 Share Posted January 3, 2011 I'm not really following your "linguistically odd" and "real language" parts.If 'For $j = 1 To 1 step 1' should never run, than shouldn't logic dictated that 'For $j = 1 To 3 step 1' should run only 2 times? ($j=[2,3])I think the bottom line is that unlike in some other languages where you can/have to set the compare part.for (i = 0; i < 10; i++){ /* loop body ;; Don't know C details, so I'm not sure if this loop would start with i=0 or i=1 */ }And as you can't do that in the AutoIt's For..Next loop command, AutoIt has to make a smart guess on what compare(bailout) to use. "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Mat Posted January 5, 2011 Share Posted January 5, 2011 For $i = START To END Step STEP CONTENT Nextfor ( int i = START ; i <= END ; i += STEP ) { CONTENT }Local $i = START While (STEP < 0 And $i >= END) Or (Step > 0 And $i <= END) CONTENT $i += STEP WEndThat last one explains most of the queries about the loop. AutoIt Project Listing Link to comment Share on other sites More sharing options...
czardas Posted January 5, 2011 Author Share Posted January 5, 2011 (edited) Please don't anyone think that I misunderstand it, or that I'm complaining, because that is not the case. However, when I say linguistically I mean it is misleading to use the word 'To'. In fact it makes more sense (linguistically) to say:For $i = $Start To (Beyond) $End...because the loop still runs one more time when $Start = $End. If I have a ladder with 10 rungs, and the loop stops when the $End = 11 (rungs), this would be impossible on the actual ladder. Consider the statement 'For $i = 1 To 0 Step 1' ...If I step forward I will never collide with whatever is behind me, no matter how many steps I take. Edited January 5, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Mat Posted January 5, 2011 Share Posted January 5, 2011 If you say it like this then it makes a lot more sense: For $i = START To END Inclusive AutoIt Project Listing Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 5, 2011 Moderators Share Posted January 5, 2011 czardas,If you take the view that the loop index is incremented and evaluated at the Next line, then I find it does make sense - even linguistically. On the first pass the If line looks to see if the Start value is greater than the End value - if it is then the loop never runs.After the first pass we get to the Next line, where the index is incremented by the Step value and the comparison made again. If the index is now greater than the End value, the loop ends there - if not then the loop runs again from the line after the If, which is never actually used after the first pass.So in your ladder case, we get to the 10th rung, increase the index, realise that we have no 11th rung and do not loop again to use it = no broken legs. 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...
czardas Posted January 5, 2011 Author Share Posted January 5, 2011 (edited) I figured out how it works, and it's quite convenient that it works the way it does: especially because the step value can be any value, and it does not need to be a divisor of the max value. Edited January 5, 2011 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Mat Posted January 5, 2011 Share Posted January 5, 2011 I'd love to be able to do this though, it would be very useful. For $i = 1 To 10 Step * 2 AutoIt Project Listing Link to comment Share on other sites More sharing options...
czardas Posted January 5, 2011 Author Share Posted January 5, 2011 (edited) I'd love to be able to do this though, it would be very useful. For $i = 1 To 10 Step * 2 Using Step 0 is one way to simplify things. You have to watch out for infinite loops though. For $i = 1.5 To 129.746337890625 Step 0 ConsoleWrite($i &@LF) $i *= 1.5 Next Edited January 5, 2011 by czardas operator64 ArrayWorkshop 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