Jump to content
Sign in to follow this  
GoogleDude

Using "And" and "Or" statement help?

Recommended Posts

GoogleDude

So I am trying to understand how to use "and" and "or" statements in an autoit script. first place I looked was the help file. well nothing came up when looking for "and" or "or" and when I searched for it the help file came back and said I cant search for those terms.

I am wanting to have my script check to see if 1 of about 10 different $vars exist. If they do then call a function. the below sample shows the msgbox even if the Test1 var is set to "Test1" or Test3. Why is that?

$State = "Test1"
If $State <> "Test1" or $State <> "Test2" Then
    MsgBox(1024,$State,"var is something other than Test1 or Test2")
EndIf

Share this post


Link to post
Share on other sites
sleepydvdr

You do realize that "<>" means "not equal to", right? Therefore, if $State is not equal to "Test1" OR if it is not equal to "Test3" then give the message box. It is equal to only one thing and you are saying if either of those statements are not true (Test3 is not true), then give the message box. Therefore, in this case, no matter what you set the value to, it will always give a message box. A single variable cannot fulfill both sides of the statement simultaneously.

You should google "truth tables". I took a beginner's programming class and truth tables helped me a lot.


#include <ByteMe.au3>

Share this post


Link to post
Share on other sites
PsaltyDS

Basic logic error, if it matches one, it doesn't match the other.

You can select multiple matches in a single Case:

$State = "Test1"
Switch $State
     Case "Test1", "Test2"
          MsgBox(1024,$State,"var is Test1 or Test2")
     Case Else
          MsgBox(1024,$State,"var is something other than Test1 or Test2")
EndIf

Edit:

Or just change your Boolean operation to And:

If ($State <> "Test1") And ($State <> "Test2") Then
The parens are not required, but good practice for readability.

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
somdcomputerguy

Also, see Language Reference | Operators in the Helpfile.

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites
GoogleDude

lol. my bad. I was thinking that <> meant greater than or less than or anything other than. I have about 10 or so different $vars that can be set. but want to call a function if another $var other than the 10 gets set?

thanks by the way.

~GD

Share this post


Link to post
Share on other sites
GoogleDude

Or just change your Boolean operation to And:

If ($State <> "Test1") And ($State <> "Test2") Then
The parens are not required, but good practice for readability.

:)

Thanks. Thanks what I was missing....the ( and the ).

For every script I write I learn something new.

Thanks,

GD

Share this post


Link to post
Share on other sites
Tvern

lol. my bad. I was thinking that <> meant greater than or less than or anything other than.

It does, that's the problem. We can make a truth table for your statement if you like:

;Case 1
$State = "Test1"
If $State <> "Test1" Or $State <> "Test2" Then ;means:
If ("Test1" <> "Test1") Or ("Test1"<> "Test2") Then ;means:
If ("Test1" is not "Test1") Or ("Test1" is not "Test2") Then ;means:
If (False) Or (True) Then ;as the second case is true, the msgbox is shown


;Case 2
$State = "Test2"
If $State <> "Test1" Or $State <> "Test2" Then ;means:
If ("Test2" <> "Test1") Or ("Test2"<> "Test2") Then ;means:
If ("Test2" is not "Test1") Or ("Test2" is not "Test2") Then ;means:
If (True) Or (False) Then ;as the first case is true, the msgbox is shown


;Case 3
$State = "Anything" ;as long as it isn't test1, or test2
 If $State <> "Test1" Or $State <> "Test2" Then ;means:
 If ("Anything" <> "Test1") Or ("Anything"<> "Test2") Then ;means:
 If ("Anything" is not "Test1") Or ("Anything" is not "Test2") Then ;means:
 If (True) Or (True) Then ;as both cases is true, the msgbox is shown

As you can see there is no possible value for $State that will return False for both tests, and as Or only needs one of the tests to be true, a messagebox is always shown.

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  

×