Jump to content

Does AutoIT have "labels"? How to return to code line "X"


Recommended Posts

This is pretty hard to explain but I'll try to make it as clear as possilbe!

When doing a loop, the code will go back to the first line, how to make it go back to not the beginnig but further down in the script?

For $i = 1 To 2 Step 1
;step one code line 1
MsgBox(0, "Title", "This is the line 2 of the code")
;step two code line 3
MsgBox(0, "Title", "This is the line 4 of the code")
;step three code line 5
MsgBox(0, "Title", "This is the line 6 of the code")
;step 4 code line 7
MsgBox(0, "Title", "This is the line 8 of the code")
;what could I use to return to line of code 4 instead of 1?
Next

I can do this is Vista Task studio but wondering if AutoIT can do it also? Here is the help file from the other software, it's also based on Visual Basics I think:

GOTO Label

Jumps to a label. The label can come before or after this command (anywhere in the script). User Variables (such as "%label") may be used as the destination. Labels that are skipped will not be used as a destination.

You can jump directly to a hard-coded step (without defining a Label) by using the {STEP1}, {STEP2}, etc. system variables. These system variables are only accessed by the Goto Label action. If you move steps around, the Goto Label will still jump to the same step.

"Goto Label" serves the same purpose as a GOTO programming statement - it immediately goes to the indicated step. If the specified label does not exist, a warning is displayed and execution stops.

Link to comment
Share on other sites

AutoIt has no "Goto". If you want to repeat only some code in the loop set a flag and execute the code depending on the flag.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

There are different methods. The code below shows two possibie methods.

For $i = 0 To 1 ; Loops twice
    MsgBox(0, "Title", "This is the line 1")
    For $j = 0 To 1 ; Nested loop method
        MsgBox(0, "Title", "This is the line 2")
        MsgBox(0, "Title", "This is the line 3")
    Next
    MsgBox(0, "Title", "This is the line 4")
    _Lines5and6() ; Call a function method
    MsgBox(0, "Title", "This is the line 7")
Next

Func _Lines5and6()
    Local $iCount = 0 ; counter
    Do
        MsgBox(0, "Title", "This is the line 5")
        MsgBox(0, "Title", "This is the line 6")
        $iCount += 1 ; Increment the counter
    Until $iCount = 2
EndFunc
Edited by czardas
Link to comment
Share on other sites

Thanks guys,

I've simplified the code above so I can understand better like this:

MsgBox(0, "Title", "1")
_flagY();GOTO line 10 of code
MsgBox(0, "Title", "2")
_flagX() ;GOTO line 7 of code
MsgBox(0, "Title", "3")
Func _flagX();Set Flag to line 7
    MsgBox(0, "Title", "4")
EndFunc   ;==>_flagX
Func _flagY();Set Flag to line 10
    MsgBox(0, "Title", "5")
EndFunc   ;==>_flagY

Althought this works fine, seems a little complicated...

Water said:

set a flag and execute the code depending on the flag

I've searched for "flag" in help file but nothing there, any clear and very simple example of working with flags?

Or maybe the way I did it up there is already the easiest way?

Thanks

Edited by xuzo
Link to comment
Share on other sites

A "flag" is a simple variable. The content tells you what to do. You decide what the content and the meaning of the content is.

$iFlag = 1
While 1
    If $iFlag = 1 Then
        ; Your code
        $iFlag = $iFlag +1
   EndIf
    If $iFlag = 2 Then
        ; Your code
        ExitLoop
   EndIf
WEnd
Runs the loop 2 times executing different code and then exits (I know it can be done much easiert - but it's just to show how "flags" work).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi Water,

Thanks for the tip, I've interpretated this way:

$myvariable = 1
While 1
If $myvariable = 1 Then
MsgBox(0, "title", "The variable equals 1")
EndIf

$myvariable = $myvariable + 1

If $myvariable = 2 Then
MsgBox(0, "title", "The variable equals 2")
ExitLoop
EndIf
WEnd

so basically $iflag can be anything? Additionally, why are there often some "i"s right after the dollar sign? Is that a convention?

Edited by xuzo
Link to comment
Share on other sites

Yes, The i and others are a convention mostly used in UDFs to show you the data type of the content. i means integer, s is for strings etc.

Details can be found here.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

LOL I deleted my last post because water got there first.

xuzo, you need to stop thinking in terms of GoTo. The point is that functions replaced GoTo, at least in AutoIt. Here's another idea. You can step backwards in a loop, but be careful you don't end up in an infinite loop.

$iFlag = 0
For $i = 1 To 4
    MsgBox(0, "Title", "Index = " & $i)
    If $i = 3 And $iFlag < 4 Then
        $i -= 2
        $iFlag += 1
    EndIf
Next
Edited by czardas
Link to comment
Share on other sites

Will look at that list over a few beers in my regular bar in Thailand a little later ...

Thailand, beer, bar? I will come over and explain everything in detail over a few beers :D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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