Sign in to follow this  
Followers 0
czardas

For Int to less than Int

13 posts in this topic

#1 ·  Posted (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)
Next

It seems illogical to me.

Edited by czardas

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for confirming that. I sometimes find myself in doubt over such things that appear odd like this. :x

Edited by czardas

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

"In each cycle it evaluates if $j > 0(max)" ... thats for "step >= 0"

- for "step < 0" ... its "$j < 0(max)"

Edited 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 ...
 

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

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 ...
 

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

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. :x

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. :P

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

#13 ·  Posted (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 by czardas

Share this post


Link to post
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
Sign in to follow this  
Followers 0