zILikeAutoITz Posted December 17, 2010 Share Posted December 17, 2010 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. Link to comment Share on other sites More sharing options...
nitekram Posted December 17, 2010 Share Posted December 17, 2010 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... 2¢ 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."  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 Link to comment Share on other sites More sharing options...
czardas Posted December 17, 2010 Share Posted December 17, 2010 (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 December 17, 2010 by czardas operator64Â Â ArrayWorkshop Link to comment Share on other sites More sharing options...
zILikeAutoITz Posted December 17, 2010 Author Share Posted December 17, 2010 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) 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 Link to comment Share on other sites More sharing options...
zILikeAutoITz Posted December 17, 2010 Author Share Posted December 17, 2010 I prefer to use WM_COMMAND in a situation like this.boah, 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. 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? Link to comment Share on other sites More sharing options...
sulfurious Posted December 17, 2010 Share Posted December 17, 2010 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. Link to comment Share on other sites More sharing options...
zILikeAutoITz Posted December 17, 2010 Author Share Posted December 17, 2010 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 thisIf (the value) ThenEndIf...Thank you very much, for advicing me. 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