Guest Posted December 7, 2013 Share Posted December 7, 2013 (edited) I think I found a logical bug in the compiler. Because logically, the code I'm going to show you must Exit from the loop if $a > 5 but in fact, it is not happening... What actually happens is this: At the first round in the the main loop, the loop calling to the function Test($a) with $a = 1. after that the return from Test($a) is 0. then for some reason, the condition: If $Test = "ExitLoop" Then activated even if $Test is 0. why this is happen? i have autoit 3.3.8 not beta. this is the code: #AutoIt3Wrapper_Run_AU3Check=n $a = 0 While 1 Sleep(1000) ToolTip("test") $a = $a+1 $Test = Test($a) MsgBox(0,"Return from Test($a):",$Test) If $Test = "ExitLoop" Then MsgBox(0,"","ExitLoop Executed",2) ExitLoop EndIf WEnd Func Test($a) If $a > 5 Then Return = "ExitLoop" EndFunc Edited December 7, 2013 by Guest Link to comment Share on other sites More sharing options...
water Posted December 7, 2013 Share Posted December 7, 2013 (edited) WAD. If $a not > 5 then the default return value of 0 (Integer) is being returned by function Test. You then compare an Integer to a String. AutoIt internally converts the String to a number. Converting letters to a number gives 0. That's why the comparison is always True. When your function always returns a string value then you are fine. That's good coding practice to always return the same type of data. #AutoIt3Wrapper_Run_AU3Check=n $a = 0 While 1 Sleep(1000) ToolTip("test") $a = $a + 1 $Test = Test($a) ConsoleWrite($Test & @LF) MsgBox(0, "Return from Test($a):", $Test) If $Test = "ExitLoop" Then MsgBox(0, "", "ExitLoop Executed", 2) ExitLoop EndIf WEnd Func Test($a) If $a > 5 Then Return "ExitLoop" ; <== Modified Return "" ; <== Added EndFunc ;==>Test Edited December 7, 2013 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 7, 2013 Moderators Share Posted December 7, 2013 gil900,Firstly, you have been here long enough to know that you do NOT post general quesris in the Examples section - please pay more attention in future. And there is no bug in AutoIt, but several errors in your code. - 1. ou have the wrong syntax for the Return line - you do not need the "=", as clearly shown in the Help file - and so you are returning a numreical 0 from each pass.- 2. In the comparison you are therefroe comparing a number to a string (0 to "ExitLoop") - in this case Autoit converts the string to number and, guess what, it comes back as numerical 0 because there are no leading digits. So your comparison is true every time.Take a look at this modified version:#AutoIt3Wrapper_Run_AU3Check=n $a = 0 While 1 Sleep(500) $a = $a + 1 $Test = Test($a) MsgBox(0, "Return from Test($a):", $Test) If $Test = "ExitLoop" Then MsgBox(0, "", "ExitLoop Executed", 2) ExitLoop EndIf WEnd Func Test($a) ; Always return a string Local $sRet = "Stay running" ; You could just use an empty string "" If $a > 5 Then $sRet = "ExitLoop" EndIf Return $sRet EndFunc ;==>TestAll clear? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Guest Posted December 7, 2013 Share Posted December 7, 2013 Firstly, you have been here long enough to know that you do NOT post general quesris in the Examples section - please pay more attention in future. Sorry, I did not mean to open it in the Examples section.. i even not noticed that I opened this thread in the Examples section. Thank you for your help.In fact I always write this method naturally: Func Test($a) ; Always return a string Local $sRet = "Stay running" ; You could just use an empty string "" If $a > 5 Then $sRet = "ExitLoop" EndIf Return $sRet EndFunc ;==>Test But until now I did not know how important it is ..Now I know.Thank you very much! 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