Jump to content

Exit if statement


Recommended Posts

Hello, this is my code 

While 1
    If someFunc() Then
        $var1 = otherFunc()
        ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & " " & $var1 & @CRLF)
        ExitLoop
    EndIf
WEnd

The problem here is that ExitLoop exits form while loop. I want to break only the if statement. How I can do it without change the if condition? (someFunc() always return true)

Edited by theimmortalbg
Link to comment
Share on other sites

You gotta set another condition (or a double if condition).

Example

I wanna run an if statement if the number of apples is >50 and only on Saturday

While 1
    If $Apples>50 And DayFunc() == Saturday Then
    
        ;Do Stuff
        
    EndIf
While 1
    If $Apples>50 Then
        
        If  DayFunc() == "Saturday"
            ;Do Stuff
        EndIf
        
    EndIf

Both of those with a righ set condition, will not enter the If loop. If it's not a loop but a logic operator, hence why you gotta exit it with a condition and not an ExitLoop.

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

Welcome to Autoit and the forum!

As someFunc always returns True your script could be stripped down to

While 1
     someFunc()
     $var1 = otherFunc()
     ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & " " & $var1 & @CRLF)
WEnd

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

  • 11 months later...

I realize the post is old; I was looking for a solution for this. ExitLoop does not work with If Statements. And you don't need it.

Do this:

If Test = True Then

;just leave this line blank or no blank line between If and ElseIf.

ElseIf Test = False Then

   DoSomething()

Else

   DoSomethingElse()

EndIf

MsgBox ($MB_OK, "Info", "Test was True")

Eh, it worked for me.

Link to comment
Share on other sites

  • 3 years later...

What If I have a really long nested If statement or contained within a function, and I need to break out of if,else,elseif statment though? 

eg

Func X()

Do stuff

If $x = 0 Then
  Do Stuff
Else
  Do Stuff
  -- >I want to leave if statement if something doesn't work here to avoid Doing more stuff
  Doing more Stuff
EndIf

More stuff

EndFunc

According to the posters logic above I should rewrite all my If statements to seperate functionS?

Edited by NDog
Link to comment
Share on other sites

You do it this way. Use ExitLoop to quit your If statement at any point:

While 1
  If $x = 0 Then
    Do Stuff
  Else
    Do Stuff
    -- >I want to leave if statement if something doesn't work here to avoid Doing more stuff
    Doing more Stuff
  EndIf
  ExitLoop
WEnd

 

Link to comment
Share on other sites

If you need a While loop as in the first post, you go this way:

While 1
  While 1
    If $x = 0 Then
      Do Stuff
    Else
      Do Stuff
      -- >I want to leave if statement if something doesn't work here to avoid Doing more stuff
      Doing more Stuff
    EndIf
    ExitLoop
  WEnd
  ; Do Stuff
WEnd

 

Link to comment
Share on other sites

spaghetti logic is UN-maintainable and a complete waste of time. use the while loops and switch/case statements when necessary

structured logic, always please. you will do well and go far if you heed those words.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

But that was not the question. Neither in the first nor fifth post. Why do you rule out that both theimmortalbg and NDog have not already made these considerations?

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