How to go to a certain place in a loop?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

How to go to a certain place in a loop?

19 Jun 2021, 03:42

Hi,

I want to go from the nested loop to the end of the main loop several times (the amount of times of the main loop).
"Goto" doesn't get the job done because it goes there, but then it freezes, it doesn't repeat the amount of times that the main loop is supposed to run.

In this example how to go from the first nested loop to the end of the main loop three times (amount of the main loop)?

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        Goto, L1
    }
    Loop  ; Second Nested Loop
    {
    }
    L1:  ; It should come here three times.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: How to go to a certain place in a loop?

19 Jun 2021, 04:08

Try:

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: How to go to a certain place in a loop?

19 Jun 2021, 04:40

I don't see where your problem is. In this example, L1 is reached 3 times, then the main loop ends.

Code: Select all

NumpadSub::
Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        random, x,1,10
        If (x=5) ; if something happens then go to label L1
            Goto, L1
    }
    Loop  ; Second Nested Loop
    {
        msgbox This code is never reached
    }
    L1:  ; It should come here three times.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
return
if your program freezes, you will have to show more of your code, because the reason must be somewhere else.
In the code as you show it, what is the sense of your second loop? That code will never be reached, because the first loop never ends in a normal way.
iamkajal
Posts: 4
Joined: 10 Jun 2021, 21:38

Re: How to go to a certain place in a loop?

19 Jun 2021, 04:44

you should try this code

Code: Select all

loop(food = 0; foodNeeded = 10) {
  if (food >= foodNeeded) {
    exit loop;
    // We have enough food; let's go home
  } else {
    food += 2; // Spend an hour collecting 2 more food
    // loop will then run again
  }
}
Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to go to a certain place in a loop?

19 Jun 2021, 07:53

Hallo,
how about this structure?

Code: Select all

NumpadSub::
Loop, 3  ; Main Loop
{
    IF A_Index > 1
		MsgBox, 0, , Continue Main Loop
	Loop  ; First Nested Loop
    {
        random, x,1,10
        If (x=5) ; if something happens then continue the Main Loop
            Continue, 2
    }
    Loop  ; Second Nested Loop
    {
        msgbox This code is never reached
    }
}
return
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

Re: How to go to a certain place in a loop?

21 Jun 2021, 16:20

rommmcek wrote:
19 Jun 2021, 04:08
Try:

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
Thank you. Your solution worked.
What if I got a third nested loop or several nested loops that should also go to the same place as the first nested loop?

Something like this:

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
        Else If ... ; THIS ONE SHOULD ALSO GO TO THE SAME PLACE AS THE FIRST LOOP
        {
        }
        Else
        {
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
Sally2Q
Posts: 43
Joined: 16 Jun 2021, 10:18

Re: How to go to a certain place in a loop?

21 Jun 2021, 17:26

I would think this way

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
    }
    
    if !BrkSub1
       Loop  ; Second Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 21 Jun 2021, 20:00, edited 1 time in total.
Reason: Please use [code] tags. Thank you!
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

Re: How to go to a certain place in a loop?

21 Jun 2021, 21:22

Sally2Q wrote:
21 Jun 2021, 17:26
I would think this way ...
Thank you.
I explained myself incorrectly.
I meant what if I got a second If in the same first nested loop (in this case the first Else If) that should also go to the same place as the first If?
Basically, what if there are two or more If (Else If) conditions in the same nested loop that should go to the same place?

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
        Else If ... ; THIS ONE SHOULD ALSO GO TO THE SAME PLACE AS THE FIRST "If" CONDITION
        {
        }
        Else
        {
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
Sally2Q
Posts: 43
Joined: 16 Jun 2021, 10:18

Re: How to go to a certain place in a loop?

21 Jun 2021, 22:20

Code for additional if or elseif blocks could be done exactly the same way as for the first one. Any break will entirely get out of the first nested loop.
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

Re: How to go to a certain place in a loop?

22 Jun 2021, 00:02

Sally2Q wrote:
21 Jun 2021, 22:20
Code for additional if or elseif blocks could be done exactly the same way as for the first one. Any break will entirely get out of the first nested loop.
It won't be exactly the same, because in your example the second nested loop can be reached only after executing the first loop.
I need the first ElseIf to be reached at the same time as the first If.

Can you please write the script where If and ElseIf in the first loop go to the same place?
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: How to go to a certain place in a loop?

22 Jun 2021, 00:24

Muhammadi wrote: Can you please write the script where If and ElseIf in the first loop go to the same place?
It would be accomplished as Sally2Q described. Why do you think it would not work? Which line in the code below do you think is the next line of code to be executed after either Break? (Hint: It's the same for both.)

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
        Else If ... ; THIS ONE SHOULD ALSO GO TO THE SAME PLACE AS THE FIRST "If" CONDITION
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
        Else
        {
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
Sally2Q
Posts: 43
Joined: 16 Jun 2021, 10:18

Re: How to go to a certain place in a loop?

22 Jun 2021, 00:41

I don't know if you misunderstood what I was saying but @boiler 's code is precisely what I was talking about. It should work.
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

Re: How to go to a certain place in a loop?

22 Jun 2021, 22:05

Sally2Q wrote:
22 Jun 2021, 00:41
I don't know if you misunderstood what I was saying but @boiler 's code is precisely what I was talking about. It should work.
@boiler's code is not exactly the same as yours, there are small changes.
These small changes are obvious and easy for you, but not for me. Because I'm not a programmer, it's all new to me)
@boiler's code works.
Thank you all very much!

What if I want the first ElseIf to go to a certain place after the second nested loop (message "Place after Second Nested Loop")?

Code: Select all

Loop, 3  ; Main Loop
{
    Loop  ; First Nested Loop
    {
        If ... ; if something happens then go to label L1
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
        Else If ... ; THIS ONE SHOULD GO TO THE PLACE AFTER SECOND NESTED LOOP
        {
            ;Goto, L1
            BrkSub1:= 1
            Break
        }
        Else
        {
        }
    }
    
    if !BrkSub1 ; only if nothing happens execute Second Nested Loop
        Loop  ; Second Nested Loop
        {
        }
        MsgBox, 0, , Place after Second Nested Loop ; FIRST ELSEIF SHOULD COME HERE.
    Else BrkSub1:= ""
    ;L1: ; it should come here three times and then end.
    MsgBox, 0, , End of Loop  ; This message should appear three times.
}
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: How to go to a certain place in a loop?

22 Jun 2021, 22:26

Muhammadi wrote: @boiler's code is not exactly the same as yours, there are small changes.
These small changes are obvious and easy for you, but not for me. Because I'm not a programmer, it's all new to me)
@boiler's code works.
No, to be fair, all I did was exactly what Sally2Q said to do, to which you replied it wouldn't work (which it does, as you noted after seeing it in code). There were no small changes at all between what I did and what Sally2Q said to do.

Muhammadi wrote: What if I want the first ElseIf to go to a certain place after the second nested loop (message "Place after Second Nested Loop")?
The basic idea is to set a flag using a variable, which is BrkSub1 in this case, then you can set up all the if/else type of scenarios you want after that. I'll leave it to you to see how you can assign different values to BrkSub1 before breaking out of the first inner loop, then checking for those values when deciding whether or not to execute certain sections of code. Although it could be spelled out again in code, it would be better for you to think about the logic and implement it yourself, as the details of any one scenario aren't as important as working through and understanding the logic of the approach.
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

Re: How to go to a certain place in a loop?

22 Jun 2021, 23:32

boiler wrote:
22 Jun 2021, 22:26
No, to be fair, all I did was exactly what Sally2Q said to do, to which you replied it wouldn't work (which it does, as you noted after seeing it in code). There were no small changes at all between what I did and what Sally2Q said to do.
I didn't understand @Sally2Q.
I thought @Sally2Q meant the code is exactly the same for both situations, not that I should do something with the code.
Your codes themselves (the order of the characters/symbols) are not the same.

Now I'll try to figure out what you told me to do)

Meanwhile, if someone can just write the code, it'll be much appreciated.

Thanks,
Muhammadi
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: How to go to a certain place in a loop?

22 Jun 2021, 23:44

Just for the record: Sally2Q was responding to your question on how to change the code that you posted and told you how to do that. You said it wouldn’t work, so I implemented exactly what was said into your code, and then you saw that it worked.
User avatar
Muhammadi
Posts: 36
Joined: 27 May 2021, 20:00

Re: How to go to a certain place in a loop?

22 Jun 2021, 23:57

@boiler, for your record: you're wrong)
I didn't say it wouldn't work.

I said the code won't the same as @Sally2Q's code.
I meant that the code itself (order of the characters/symbols) won't be the same as @Sally2Q's code.
Your code itself is not the same as @Sally2Q's code.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], OrangeCat and 134 guests