Jump to content
Sign in to follow this  
suroit

Nested While..Wend loops with ExitLoop [level]

Recommended Posts

suroit

I am trying to use a nested While/Wend loop with ExitLoops for each loop. Here is the code I am testing:

Dim $i
Dim $j
$i=0
$j=0
While 1
    While 2
        If $j<3 Then
            $j = $j + 1
            MsgBox(4096, "$j loop", "$i =" & $i & ",$j = "& $j)
        Else    
            ExitLoop (1)
        EndIf
    WEnd
    MsgBox(4096, "", "End of loop $j = "& $j)
    
    If $i <3 Then
        $i = $i +1
        MsgBox(4096, "$i loop", "$i =" & $i & ",$j = "& $j)
    Else
        ExitLoop (2)
    EndIf
WEnd
MsgBox(4096, "", "End of loop $i = "& $i)
Exit

The nested loop (index $j) works fine. When the nested loop reaches the exit condition ($j>=3), the first loop (index $i) seems to work fine until the condition for exit ($i>=3) is reached. When the program has to execute the second ExitLoop (2) command, I get an Autoit Error saying:

Line 20 (File "D:|Testing_nested_while_loops.au3")

ExitLoop(2)

Error: "ExitLoop/ContinueLoop" Statements only valid from inside a For/Do/While loop.

I need some help about how to use the ExitLoop levels. By the way, I tried ExitLoop 2 as well as ExitLoop (2). Same result. It seems to me the second ExitLoop is used inside a While loop, contrary to the error statement. Any comment would be appreciated.

Share this post


Link to post
Share on other sites
omikron48

In your second ExitLoop, there is only one loop it can exit from (the first While...WEnd), so giving it a value of 2 makes it invalid.

Edited by omikron48

Share this post


Link to post
Share on other sites
suroit

Thanks Omikron.

Removing the levels make it work. I thought I had use the ExitLoop levels so that the first loop doesn't exit on the second ExitLoop -- and the help file is really sketchy on this point. But apparently, there is no ambiguity here. I am struggling to find an example where there is ambiguity and the ExitLoop levels are needed...no luck so far.

Share this post


Link to post
Share on other sites
omikron48

If an example is all you're after, that's easy. I normally do it when I'm searching for files in a directory.

Global $searchhandle
While 1
    ;gives you a list of all the .txt files in the C:\toprocess folder
    $searchhandle = FileFindFirstFile("C:\toprocess\*.txt")
    If @error Then
        MsgBox(4096, "Error", "No files/directories matched the search pattern", 1)
        Sleep(10000)
    Else
        Local $filename
        ;process files returned by search filter
        While 1
            $filename = FileFindNextFile($searchhandle)
            ;exit loop if no more .txt files to process
            If @error Then ExitLoop
            ;process $filename
        Wend
    EndIf
WEnd

Share this post


Link to post
Share on other sites
suroit

Thanks, but what I am looking for is an example of nested loops with two ExitLoop levels. But I may have originally misunderstood the level parameter, as described (summarily) in the Autoit Help file:

ExitLoop [level]

Parameters

level: [optional] The number of loop levels to exit from. The default is 1 (meaning the current loop).

Now, my understanding is that with ExitLoop 1, you exit the current loop, with ExitLoop 2, you exit the current loop and the higher level loop, etc.

I think I can test this.

Share this post


Link to post
Share on other sites
omikron48

Yes. That's exactly what it means.

While
    ;some code
    For
        ;some code
        ExitLoop 2 ;<- this will exit the For...Next and the While...WEnd loops
    Next
    ;some code
WEnd
;more code

Share this post


Link to post
Share on other sites
suroit

Excellent, this is totally clear to me now.

Thanks a lot.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.