Sign in to follow this  
Followers 0
zILikeAutoITz

Confused with "If...else" ?

7 posts in this topic

Hi autoitscripter's,

I'm don't do a great job in logic thing's and actually in coding too, and so I'm have trouble with my "if else" check script, actually it works but i need more prove which code is better or should be better.

About my "If" script, it check's whether an checkbox is checked and disable an another checkbox.

And yes, i should use the right control like radio button but i like the style design of the checkbox more, so it bring me much trouble. *hehe*

But now i figure out how to manage them but confusing me self about the "If" and "If...else" script. So i asked for someone looking and helping me next.

I have two examples to compare which do their job greatly but like i sayed i need more prove and myself now is totally confused and scratching my head around here.

Here goes the first script with two "If" checks:

#include <GUIConstantsEx.au3>

Global $Checked_State, $Checked_State2

GUICreate("", 200, 80)
$input_chkBox1 = GUICtrlCreateCheckbox("First choice", 10, 30)
$input_chkBox2 = GUICtrlCreateCheckbox("Second choice", 90, 30)

GUISetState()       ;Show GUI

While 1
    $msg = GUIGetMsg()
    ; Start of confusing
    If BitAnd(GUICtrlRead($input_chkBox1),$GUI_CHECKED) <> $Checked_State Then
        $Checked_State = Not $Checked_State
        If BitAnd(GUICtrlRead($input_chkBox1),$GUI_CHECKED) = $GUI_CHECKED THEN;
            GUICtrlSetState ($input_chkBox2, $GUI_DISABLE)
        ElseIf BitAnd(GUICtrlRead($input_chkBox1),$GUI_UNCHECKED) = $GUI_UNCHECKED THEN;
            GUICtrlSetState ($input_chkBox2, $GUI_ENABLE)
        EndIf
    EndIf
    If BitAnd(GUICtrlRead($input_chkBox2),$GUI_CHECKED) <> $Checked_State2 Then
        $Checked_State2 = Not $Checked_State2
        If BitAnd(GUICtrlRead($input_chkBox2),$GUI_CHECKED) = $GUI_CHECKED THEN;
            GUICtrlSetState ($input_chkBox1, $GUI_DISABLE)
        ElseIf BitAnd(GUICtrlRead($input_chkBox2),$GUI_UNCHECKED) = $GUI_UNCHECKED THEN;
            GUICtrlSetState ($input_chkBox1, $GUI_ENABLE)
        EndIf
    EndIf
    ; End of confusing
    If $msg = $GUI_EVENT_CLOSE Then
        GUIDelete()
        Exit
    EndIf
WEnd

and here the second script with an "else" after the first main check:

#include <GUIConstantsEx.au3>

Global $Checked_State, $Checked_State2

GUICreate("", 200, 80)
$input_chkBox1 = GUICtrlCreateCheckbox("First choice", 10, 30)
$input_chkBox2 = GUICtrlCreateCheckbox("Second choice", 90, 30)

GUISetState()       ;Show GUI

While 1
    $msg = GUIGetMsg()
    ; Start of confusing
    If BitAnd(GUICtrlRead($input_chkBox1),$GUI_CHECKED) <> $Checked_State Then
        $Checked_State = Not $Checked_State
        If BitAnd(GUICtrlRead($input_chkBox1),$GUI_CHECKED) = $GUI_CHECKED THEN;
            GUICtrlSetState ($input_chkBox2, $GUI_DISABLE)
        ElseIf BitAnd(GUICtrlRead($input_chkBox1),$GUI_UNCHECKED) = $GUI_UNCHECKED THEN;
            GUICtrlSetState ($input_chkBox2, $GUI_ENABLE)
        EndIf
    Else ; <<< else added
        If BitAnd(GUICtrlRead($input_chkBox2),$GUI_CHECKED) <> $Checked_State2 Then
            $Checked_State2 = Not $Checked_State2
            If BitAnd(GUICtrlRead($input_chkBox2),$GUI_CHECKED) = $GUI_CHECKED THEN;
                GUICtrlSetState ($input_chkBox1, $GUI_DISABLE)
            ElseIf BitAnd(GUICtrlRead($input_chkBox2),$GUI_UNCHECKED) = $GUI_UNCHECKED THEN;
                GUICtrlSetState ($input_chkBox1, $GUI_ENABLE)
            EndIf
        EndIf
    EndIf
    ; End of confusing
    If $msg = $GUI_EVENT_CLOSE Then
        GUIDelete()
        Exit
    EndIf
WEnd

And if anyone can make my script better or take advice to make it better in an another way like i do it, i'm very appreciate and thankful too. :x

Share this post


Link to post
Share on other sites



Not sure what you are trying to do, but IF ELSE works like this

If Condition ;is true then

complete these steps

else ; condition is not true

complete these other steps

EndIf

So there is no reason to check the first condition again in the second part of the If statement. As if the condition could be only one of two (checked or unchecked) then checking for the first condition, would lead you to the second condition if the first one was not meant.

Hope that is as clear as mud...


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The first script will always run the test on $input_chkBox2. The second script will ONLY run the test on $input_chkBox2 (after the else statement) WHEN $input_chkBox1 is NOT checked. This is the same as what nitekram said, only worded slightly differently. I hope this helps to clear up your confusion.

Edited by czardas

Share this post


Link to post
Share on other sites

Thank you nitekram and czardas,

Sorry for my bad english,

Actually i'm only want to disable the checkbox that was not checked after one of the checkbox's is checked,, (weird explain only to explain it, make me already confused) :x

hmm, so the second script should be a little better, i'm right?

All two script run perfectly like i want but it seems that in performance (it's only a little or maybe in logic thing's, that i'm didn't see the difference) the second code is better.

and it can't be written in a better way, right?

Thanks very much, again :P

Share this post


Link to post
Share on other sites

I prefer to use WM_COMMAND in a situation like this.

boah, :x

That's make the cpu more less working also in performance so much more great solution, that i never know and that weird scripting function and coding ...boaaahh,

I'm really really thank you to show how can this be done in another way (i know it that it can be). That's very deep too, i think.

Thank you very much, very appreciated. :shifty:

Ps: now i'm trying to learn such that deep thing, and search about " WM_COMMAND" and landed at "GUIRegisterMsg ", i'm on the right way? or better don't touch this deep thing stuff? :P

Share this post


Link to post
Share on other sites

When learning, I found it best to always nest my IFs before adding code to them.

For example when I need an IF line, I do this

If (the value) Then

EndIf

It is a small habit to develop that helps a lot. Always start and end your IF statements right away before adding to them. If you decide to start it like this

If (the value Then

(some code here)

without also putting the EndIf in place, it is easy to overlook it later.

When using Scite, you can use its folding to also help keep track of your nesting. Just toggle the fold expanded/contracted, and if your nesting is correct, it should fold up or open up exactly. This is especially useful when you have IFs inside of IFs inside of Whiles or Selects. Scite is fabulous for developing scripts in because of its folding feature.

Sul.

Share this post


Link to post
Share on other sites

When learning, I found it best to always nest my IFs before adding code to them.

For example when I need an IF line, I do this

If (the value) Then

EndIf

...

Thank you very much, for advicing me. :x

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