Sign in to follow this  
Followers 0
HelloIDistance

While...WEnd

31 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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

Wouldn't you do your exitloop priorities in the function?

Rick


Only $2.00 with Resale Rights How to Block Better for Martial Artists and NonMartial Artistshttp://kirkhamsebooks.com/MartialArts/Bloc...tterEbook_m.htm

Share this post


Link to post
Share on other sites

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

In general, you can end a While conditionally, or with ExitLoop n inside.

n: The level.

Edit: Format

Edited by josbe

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Ah, okay. I never thought about switching it around :D .

Thanks!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

hmmm...I think that, the problem is with your Loop :

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

WEnd

Because, $Hellowall is greater than 1, this repeats infinitely.

$Hellowall variable needs change in some time?

Share this post


Link to post
Share on other sites

You made an infinite loop. If you want to get out of it, the $Hellowall variable has to change.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Ah, That is my problem. I get it now, but how would I change the variable and still get the timer to run every ten seconds, and then that loop to run again? After it does the func Reconnect()?

Edited by HelloIDistance

Share this post


Link to post
Share on other sites

Bump^

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

Okay I am kind of confused your saying I should decrease the value of the variable to one so it doesn't loop then change it back to two? Sorry I'm sorta confused could you write an example?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Okay, so would ExitLoop before the loop work? How would I re-enter the loop? Is it possible then to run the $Hellowall loop and the Timer loop?

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

What are you trying to do?

That's the question you should answer first...

Share this post


Link to post
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

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