Sign in to follow this  
Followers 0
gil900

Can anyone tell me why this test code not work?

4 posts in this topic

#1 ·  Posted (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 by gil900

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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   ;==>Test
All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

 

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!

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  
Followers 0