Jump to content
Sign in to follow this  
theimmortalbg

Exit if statement

Recommended Posts

theimmortalbg

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

Share this post


Link to post
Share on other sites
Newb

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.

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
JibsMan

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.

Share this post


Link to post
Share on other sites
NDog
Posted (edited)

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

Share this post


Link to post
Share on other sites
Earthshine

Long nested if statements are never a way to go


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
ViciousXUSMC

If you only have 2 result conditions you do not need the 2nd IF just an Else.

Because if its not true then by elimination it must be false.

If $condition = True Then
    ;Do something
Else
    ;Do something else
EndIf

 

When you have multiple conditions, that is often times when the Case/Switch syntax can be used.

 

  • Like 1

Share this post


Link to post
Share on other sites
LarsJ

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

 

  • Like 1

Share this post


Link to post
Share on other sites
LarsJ

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

 

  • Like 1

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

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

 

Share this post


Link to post
Share on other sites
LarsJ

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?

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  

×