Jump to content

for step & case weirdness


Recommended Posts

I was playing with some functions here and there and ran into this bit of weirdness. I have heard before that it's not that great of an idea to do so, and was wondering why exactly.

#include <MsgBoxConstants.au3>

Global $input1

HotKeySet("{ESC}", "End")

doit()

Func doit()
For $i = 5 To 1 Step -1
    $nmsg = GUIGetMsg()
    Switch $nmsg
        Case $i = 5
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "5")
        Case $i = 4
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "4")
        Case $i = 3
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "3")
        Case $i = 2
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "2")
        Case $i = 1
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "1")
    EndSwitch
Next
MsgBox($MB_SYSTEMMODAL, "", "Blast Off!")
EndFunc

Func End()
    Exit
EndFunc

 

Link to comment
Share on other sites

8 minutes ago, HankHell said:

I was playing with some functions here and there and ran into this bit of weirdness. I have heard before that it's not that great of an idea to do so, and was wondering why exactly.

#include <MsgBoxConstants.au3>

Global $input1

HotKeySet("{ESC}", "End")

doit()

Func doit()
For $i = 5 To 1 Step -1
    $nmsg = GUIGetMsg()
    Switch $nmsg
        Case $i = 5
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "5")
        Case $i = 4
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "4")
        Case $i = 3
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "3")
        Case $i = 2
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "2")
        Case $i = 1
            GUICtrlSetData($input1, $i)
            MsgBox(0, "title", "1")
    EndSwitch
Next
MsgBox($MB_SYSTEMMODAL, "", "Blast Off!")
EndFunc

Func End()
    Exit
EndFunc

 

That will never work for multiple reasons

 

Link to comment
Share on other sites

Just now, Bilgus said:

End is a reserved keyword

You aren't checking for valid cases of $nmsg

 

Did you even try running this code?

yep, and it never said that End() was improper either, and it runs just fine on my end... I can press the button 5 times as expected, but I think some sort of weird race condition makes the text scrambled

Link to comment
Share on other sites

#include <MsgBoxConstants.au3>

;Global $input1

HotKeySet("{ESC}", "End")

doit()

Func doit()
For $i = 5 To 1 Step -1
    $nmsg = GUIGetMsg()
    Switch $nmsg

        Case $i <> 5
            MsgBox(0, "title", "5 :" & $nmsg & ": " & ($i = 5))
        Case $i = 4
            MsgBox(0, "title", "4 :" & $nmsg& ": " & ($i = 4))
        Case $i = 3
            MsgBox(0, "title", "3 :" & $nmsg& ": " & ($i = 3))
        Case $i = 2
            MsgBox(0, "title", "2 :" & $nmsg& ": " & ($i = 2))
        Case $i = 1
            MsgBox(0, "title", "1 :" & $nmsg& ": " & ($i = 1))
        Case Else
            Consolewrite($nmsg & @CRLF)
    EndSwitch
Next
MsgBox($MB_SYSTEMMODAL, "", "Blast Off!")
EndFunc

Func End()
    Exit
EndFunc

Maybe that will elucidate it for you

 

So since you are switching on $nmsg it matches (what autoit calls) True and False instead of switching on $i

Edited by Bilgus
Link to comment
Share on other sites

9 minutes ago, Bilgus said:
#include <MsgBoxConstants.au3>

;Global $input1

HotKeySet("{ESC}", "End")

doit()

Func doit()
For $i = 5 To 1 Step -1
    $nmsg = GUIGetMsg()
    Switch $nmsg

        Case $i <> 5
            MsgBox(0, "title", "5 :" & $nmsg & ": " & ($i = 5))
        Case $i = 4
            MsgBox(0, "title", "4 :" & $nmsg& ": " & ($i = 4))
        Case $i = 3
            MsgBox(0, "title", "3 :" & $nmsg& ": " & ($i = 3))
        Case $i = 2
            MsgBox(0, "title", "2 :" & $nmsg& ": " & ($i = 2))
        Case $i = 1
            MsgBox(0, "title", "1 :" & $nmsg& ": " & ($i = 1))
        Case Else
            Consolewrite($nmsg & @CRLF)
    EndSwitch
Next
MsgBox($MB_SYSTEMMODAL, "", "Blast Off!")
EndFunc

Func End()
    Exit
EndFunc

Maybe that will elucidate it for you

 

So since you are switching on $nmsg it matches (what autoit calls) True and False instead of switching on $i

nope, still getting random numbers, as in "54323 blast off"

Link to comment
Share on other sites

Comments added to example in post #1:-

#include <MsgBoxConstants.au3>

Global $input1

HotKeySet("{ESC}", "End")

doit()

Func doit()
    For $i = 5 To 1 Step -1
        $nmsg = GUIGetMsg() ; $nmsg = 0, $GUI_EVENT_NONE (0): No event
        ConsoleWrite("$nmsg: " & $nmsg & "  $i: " & $i & @CRLF)
        Switch $nmsg ; The Switch Case will match when the Case value = 0

            Case $i = 5 ; 1st time thru loop $i = 5. So, 5 = 5 = true = 1  $nmsg <> 1
                GUICtrlSetData($input1, $i)
                MsgBox(0, "title", "5")
            Case $i = 4 ; 1st time thru loop $1 = 5 so 4 = 5 = false = 0 and $nmsg = 0, This case is triggered.
                GUICtrlSetData($input1, $i)
                MsgBox(0, "title", "4")
            Case $i = 3
                GUICtrlSetData($input1, $i)
                MsgBox(0, "title", "3")
            Case $i = 2
                GUICtrlSetData($input1, $i)
                MsgBox(0, "title", "2")
            Case $i = 1
                GUICtrlSetData($input1, $i)
                MsgBox(0, "title", "1")
        EndSwitch
    Next
    MsgBox($MB_SYSTEMMODAL, "", "Blast Off!")
EndFunc   ;==>doit

Func End()
    Exit
EndFunc   ;==>End

Each time through the For-Next loop $nmsg = 0, $GUI_EVENT_NONE (0): No event. So when the Case is 0, the case is triggered in the Switch-EndSwitch function.

1st time thru loop Case $i=5 So, (5 = 5) = True = 1  $nmsg <> 1, 
                     Case $i=4 So, (5 = 4) = False = 0  $nmsg = 0 - Matches this Case.

2nd time thru loop Case $i=5 So, (4 = 5) = False = 0  $nmsg = 0 - Matches this Case.
In subsequent loops, the 1st case (Case $i=5) is matched because all ($i=5) = False = 0 and the Switch expression, $nmsg, remains at 0.

The order of message boxes is:-
4
5
5
5
5
Blast Off!
This outcome is logically correct because of the weird coding.

You might want to compare the functions "Switch...Case...EndSwitch" and "Select...Case...EndSelect" 
 

Edited by Malkey
Added "weird" adjective to "weird coding"
Link to comment
Share on other sites

HankHell, The Switch and Case expressions should be comparable data types.

If the Switch expression evaluates to an integer value then the Case expressions should also evaluate to integers.

If the Switch expression evaluates to a boolean value then the Case expressions should also evaluate to booleans.

And so on.

In the code in first post I would replace "Switch $nmsg" with "Switch $nmsg <> 0" to make it clear that it's about boolean values.

Edited by LarsJ
$nmsg > 0 --> $nmsg <> 0
Link to comment
Share on other sites

None of these examples will actually work, ever.

You're switch expression is looking at $nmsg, the case values are using the loop variable $i, the ONLY time those will be fired is if the message from your non-existent GUI matches to 1 or 0, and if $i is 5, then it will ignore that case , because when $i = 5, the non-existent GUI will have to be sending a message of 1. There is no event from GUIGetMsg that ever returns 1. So, the Case $i = 4 will be actioned, because $i is not 4, which evaluates to zero (false), and the GUI message will also be zero, after that the loop $i now equals 4, and because $i doesn't equal 5, the Case $i = 5 will be actioned, because $i = 5 is now zero (false), and so on. That's why 5 keeps coming up after the first loop.

Either switch on the $i variable, or learn what message you're looking to action, because trying to compare your cases against the event messages from a non-existent GUI is definitely not the way to do it.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I think it works like this?

Opt('TrayMenuMode',1)
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

HotKeySet("{ESC}", "_End")

Global $hGUI = GUICreate("AutoIt GUI", 347, 52, 583, 263)
Global $Input_1 = GUICtrlCreateInput("", 16, 16, 225, 21)
Global $Button_START = GUICtrlCreateButton("START", 256, 16, 75, 25)
GUISetState(@SW_SHOW)

Global $nMsg
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_START
            _DoIt()
    EndSwitch
    Sleep(10)
WEnd

Func _DoIt()
    For $i=5 To 1 Step -1
        Switch $i
            Case 5
                GUICtrlSetData($Input_1, $i)
                MsgBox(0, "title", "5")
            Case 4
                GUICtrlSetData($Input_1, $i)
                MsgBox(0, "title", "4")
            Case 3
                GUICtrlSetData($Input_1, $i)
                MsgBox(0, "title", "3")
            Case 2
                GUICtrlSetData($Input_1, $i)
                MsgBox(0, "title", "2")
            Case 1
                GUICtrlSetData($Input_1, $i)
                MsgBox(0, "title", "1")
        EndSwitch
    Next
    MsgBox(0, "", "Blast Off!")
EndFunc   ;==>_DoIt

Func _End()
    Exit
EndFunc   ;==>_End

 

Regards,
 

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