Jump to content

While...WEnd


Recommended Posts

Wend terminates the first While loop it finds. It's called scope. Nesting While loops inside any other while, do, for, or if structure is possible. Most people use tabs to help space their script to show scope.

While 1   ; Scope 1
    DoSomething()   ; Scope 2
    If 1 = 2 Then   ; Scope 2
        SystemError()   ; Scope 3
    Else   ; Scope 2
        $a = 1   ; Scope 3
        While $a < 5   ; Scope 3
            DoMore()   ; Scope 4
            $a = $a + 1   ; Scope 4
        Wend
    EndIf
Wend

Edit: Silly forum doesn't like my spacing...

Edited by Valik
Link to comment
Share on other sites

  • Developers

Is it possible to name "while" functions, so that when you use two "while" commands it will know which "while" to end?

Wend always ends the last While.

When you use proper tabbing you can easily see which one you are ending.

Also when you use an editor like Scite, you can use code folding, which will make it easy to see which one you'r closing....

You could put a comment behind it like:

While 1 ; first level
   ; somecode
    While 1  ; level 2
       ; some other code
    Wend  ; Level 2
Wend ; Level 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks for all the quick replys. I still don't understand though :/. Maybe if I show an example:

While 1
;code

While 2
;code

WEnd; I want to end the first while though not the second

;code

WEnd; End the second while

How would I do that? I tryed the ExitLoop command, but it didn't work. Is it possible to do that? Sorry if this was answered before I don't understand.

Link to comment
Share on other sites

I want to end the first while though not the second

You can't do that. But there should never be a need to. Instead, switch the the ;code segments around, so that you end the other one in the 2nd While- WEnd pair.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Alright after messing with this timer for a long time I still don't get it. I have tried to switch it around. I have tried to use ExitLoop, ContinueLoop. Here is an example of my problem:

Global $hTimer = TimerStart()

While 1
    If TimerStop($hTimer) >= (10000) Then Reconnect()

    $Hellowall = 2

    Sleep(1000)
    Send ("Howdy Partner!")

    While $Hellowall > 1
        Send ("Hahahaha")
        Sleep(2000)
    WEnd
WEnd

Func Reconnect()
    Sleep(20000)
    Send ("It worked!")
    Sleep(1000)
    $hTimer = TimerStart(); Reset timer
EndFunc

With this it never gets to my Func Reconnect(). I have tried to use an If..Then instead While..WEnd, but it doesn't work the same. Any ideas on how to fix this?

Edited by Larry
Link to comment
Share on other sites

but how would I change the variable and still get the timer to run every ten seconds, and then that loop to run again?

The variable changes by assigning a new value to it. Like $var = $var -1 or $var = $var +1 to decrease or increase it by one. If you need to test for the timer in both loops, why not call your conditional statement in the 2nd loop as well as the first? As long as the timer is reset before it comes out of the timer handeling function, it won't interfere (as long as running the timer handeling function is not a problem from within the 2nd loop.)

Edited to fix my vars so they matched

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

I'm sorta confused could you write an example?

Your loop has no way out of it since you start with a variale larger than 1, and tell it to do some stuff while that variable is larger than one. It will always be larger than one, and therefore you will never get out of that loop unless you ExitLoop, or use a seperate function call (or AdLib) to get out. Here is a basic example of incrimenting a variable in a while loop. Please note that this is only a simple example, and technically a For loop would be more useful, but I'm trying to demonstrate keeping as much of your code as I can.

While $Hellowall > 1
Send ("Hahahaha")
Sleep(2000)
$Hellowall = $Hellowall - 1
WEnd

That will decrease $Hellowall by 1 each time the loop is run. When it is 1, you will exit the loop. If not, you will keep decreasing it until it is.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

What are you trying to do? If you nest loops (that means putting a loop inside another loop) it will do the following:

  • Run the main body of your 1st while loop (up to the 2nd While command)
  • Run the main body of your 2nd while loop
  • If the condition for your 2nd while loop is true, go to step #2
  • Run any steps in the main body of your 1st while loop that are after the WEnd of the 2nd while loop
  • If the condition for your 1st while loop is true, go to step #1
Understand that you will keep doing step #2 over and over until the condition (see #3) is false. If this is not what you want, then don't nest your statements, and only use one loop to test the timer, and then do other stuff, and go back to the top of the loop again.

Technical note: (Edit) Technically the loop is testing the condition before it runs the loop, so you could run a while loop 0 times. However, when I wrote these example steps, I wasn't thinking about it, so it doesn't show excatally how the computer is thinking about it. But it's close enough to understand what nesting is, and to make sure you actually want to nest your while functions.

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

What are you trying to do?

I just wanna get this thing to work, lol.

Okay, I think I know what your talking about. I want to keep it the same but just have both loops work (Is this possible?). I want it them both to work within each other. What would I change to make it work? Like if your using the same script as the one I posted is it possible to have them both run together? How would I do that?

If I changed the variable then it would quit looping correct? So maybe I don't want to nest my statements. Then how would I unnest them? Sorry for all the noobish questions :D

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