theimmortalbg Posted September 8, 2013 Share Posted September 8, 2013 (edited) 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 September 8, 2013 by theimmortalbg Link to comment Share on other sites More sharing options...
Newb Posted September 8, 2013 Share Posted September 8, 2013 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 More sharing options...
water Posted September 8, 2013 Share Posted September 8, 2013 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 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
JibsMan Posted August 8, 2014 Share Posted August 8, 2014 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 More sharing options...
NDog Posted January 9, 2018 Share Posted January 9, 2018 (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 January 9, 2018 by NDog Link to comment Share on other sites More sharing options...
Earthshine Posted January 9, 2018 Share Posted January 9, 2018 Long nested if statements are never a way to go My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted January 9, 2018 Share Posted January 9, 2018 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. Earthshine 1 Link to comment Share on other sites More sharing options...
LarsJ Posted January 9, 2018 Share Posted January 9, 2018 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 Earthshine 1 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
LarsJ Posted January 9, 2018 Share Posted January 9, 2018 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 Earthshine 1 Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
Earthshine Posted January 9, 2018 Share Posted January 9, 2018 (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 January 9, 2018 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
LarsJ Posted January 9, 2018 Share Posted January 9, 2018 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? Controls, File Explorer, ROT objects, UI Automation, Windows Message MonitorCompiled code: Accessing AutoIt variables, DotNet.au3 UDF, Using C# and VB codeShell menus: The Context menu, The Favorites menu. Shell related: Control Panel, System Image ListsGraphics related: Rubik's Cube, OpenGL without external libraries, Navigating in an image, Non-rectangular selectionsListView controls: Colors and fonts, Multi-line header, Multi-line items, Checkboxes and icons, Incremental searchListView controls: Virtual ListViews, Editing cells, Data display functions Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now