Loop Break, Return

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Loop Break, Return

Post by whynotregister » 23 Oct 2021, 21:57

Code: Select all

Loop
{
  If A_Index = 10
  Break 
}
...
Return

Loop
{
  If A_Index = 10
  Return 
}
...
Return
Both codes have the same exit point, but which one is more stable? I'm wondering which one is more stable, break or return, when I need to exit the loop under some circumstances.

User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: Loop Break, Return

Post by boiler » 23 Oct 2021, 22:01

It's not a matter of stability. It's a matter of how the code is supposed to flow when exiting the loop. You would choose break or return to do very specific things. You would use break to have it continue executing the commands below the loop, and you would use return to return back to where it was called from (or to exit the script if it was not called by something).

Examples:

Code: Select all

Loop
{
  If A_Index = 10
  Break 
}
MsgBox, This gets executed
Return

Code: Select all

Loop
{
  If A_Index = 10
  Return 
}
MsgBox, This does not get executed
Return

whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Re: Loop Break, Return

Post by whynotregister » 23 Oct 2021, 22:06

boiler wrote:
23 Oct 2021, 22:01
It's not a matter of stability. It's a matter of how the code is supposed to flow when exiting the loop. You would choose break or return to do very specific things. You would use break to have it continue executing the commands below the loop, and you would use return to return back to where it was called from (or to exit the script if it was not called by something).

Examples:

Code: Select all

Loop
{
  If A_Index = 10
  Break 
}
MsgBox, This gets executed
Return

Code: Select all

Loop
{
  If A_Index = 10
  Return 
}
MsgBox, This does not get executed
Return
It seems I didn't write the question specifically.

Code: Select all

Loop
{
  IfExist,A.exe
  {
    LabelSuspend = 1
    break
  }
}
If LabelSuspend = 1
Return
 
Loop
{
  IfExist,A.exe
  {
    Return
  }
}
Return
For example, when need to find a certain condition in a loop and use Return according to the condition, I wonder if break > return is stable or if it is stable to return immediately.
Because I was wondering if using return instead of break > return to immediately exit the loop could cause goto-like instability.

User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: Loop Break, Return

Post by boiler » 23 Oct 2021, 22:15

whynotregister wrote: Because I was wondering if using return instead of break > return to immediately exit the loop could cause goto-like instability.
It does not. When you return from directly inside a loop, the script doesn't think that you're still inside the loop for any reason.

whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Re: Loop Break, Return

Post by whynotregister » 24 Oct 2021, 08:06

boiler wrote:
23 Oct 2021, 22:15
whynotregister wrote: Because I was wondering if using return instead of break > return to immediately exit the loop could cause goto-like instability.
It does not. When you return from directly inside a loop, the script doesn't think that you're still inside the loop for any reason.
thank you for answer :superhappy:

Post Reply

Return to “Ask for Help (v1)”