Jump to content

Using "And" and "Or" statement help?


Recommended Posts

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
Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 8 years later...

Hey

i have a similar question to the "And"

 

Thats my Script (its a little hospital simulator

#include <INet.au3>
#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>

$sList = ""

Global $Line1 = FileReadLine ("Anordnungen.txt", 1)
Global $Line2 = FileReadLine ("Anordnungen.txt", 2)


#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Patientensim", 615, 437, 192, 124)
$Tab1 = GUICtrlCreateTab(0, 0, 615, 437)

$TabSheet1 = GUICtrlCreateTabItem("Fallbeschreibung")
            $Label4 = GUICtrlCreateLabel("Herr Peters ist 56 Jahre alt und gibt bei der Anamnese Husten und ein Rasseln auf der Lunge an. Bei der Vitalzeichenmessung stellte sich eine erhöhte Temperatur raus.", 50 , 50, 500, 300)


$TabSheet2 = GUICtrlCreateTabItem("Patientenbett")
            GUISwitch($Form1,$Tabsheet2)
            $Pic1 = GUICtrlCreatePic("C:\Users\axelf\Desktop\patbett.jpg", 216, 48, 321, 237)
            $Button1 = GUICtrlCreateButton("Aktueller Zustand", 40, 50, 153, 57)


$TabSheet3 = GUICtrlCreateTabItem("Anordnungen")
            GUISwitch($Form1,$Tabsheet3)
            Global $Linie1 = FileReadLine ("Anordnungen.txt", 1)
            Global $Linie2 = FileReadLine ("Anordnungen.txt", 2)
            $Label1 = GUICtrlCreateLabel("Aktuelle Medikamentenanordnungen", 50 , 50, 200, 20)
            $Label3 = GUICtrlCreateLabel("Medikament 1", 50 , 103, 100, 20)
            $hCombo = GUICtrlCreateCombo($Linie1, 250, 100, 200, 20)
            $hCombo1 = GUICtrlCreateCombo($Linie2, 250, 153, 200, 20)
            $Label2 = GUICtrlCreateLabel("Medikament 2", 50 , 153, 100, 20)
            $Button_Speichern = GUICtrlCreateButton("Anordnung aktuallisieren", 40, 250, 153, 57)
            $Button_abrufen = GUICtrlCreateButton("Anordnung abrufen", 250, 250, 153, 57)
            GUICtrlSetData($hCombo, "Inhalation mit NaCl 3% 1-1-1-1|Paracetamol 1g  i.v. bei Bedarf|")
            GUICtrlSetData($hCombo1, "Inhalation mit NaCl 3% 1-1-1-1|Paracetamol 1g  i.v. bei Bedarf|")

$TabSheet4 = GUICtrlCreateTabItem("Diagnosen")
            GUISwitch($Form1,$Tabsheet4)
            $Button3 = GUICtrlCreateButton("Diagnose Stellen", 210, 200, 153, 57)
            $hCombo3 = GUICtrlCreateCombo("", 90, 100, 400, 20)
            GUICtrlSetData($hCombo3, "I26.- Lungenembolie|J18. 9 Pneumonie, nicht näher bezeichnet|J44. 1- COPD mit akuter Exazerbation, nicht näher bezeichnet")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

         Case $Button3
            $Diag = GUICtrlRead($hCombo3)

            If $Diag = "J18. 9 Pneumonie, nicht näher bezeichnet" Then
               MsgBox (1, "Richtige Diagnose", "Herzlichen Glückwunsch, Sie haben die richtige Diagnose gestellt. Behandeln Sie den Patienten nun, falls Sie dies noch nicht getan haben und achten Sie immer auf seinen Zustand")
            ElseIf $Diag = "I26.- Lungenembolie" or "J44. 1- COPD mit akuter Exazerbation, nicht näher bezeichnet" Then
               MsgBox (1, "Diagnose überprüfen", "Bitte prüfen Sie erneut Ihre Auswertung")

            ;ElseIf $Diag = " " Then
            ;   MsgBox (1, "Diagnose stellen", "Bitte stellen Sie zunächst Ihre Diagnose")
            EndIf

         Case $Button1
            $Line1 = FileReadLine ("Anordnungen.txt", 1)
            $Line2 = FileReadLine ("Anordnungen.txt", 2)

            If Not FileExists("C:\Users\axelf\Desktop\Anordnungen.txt") Then
            $Line1 = ""
            EndIf


            if $Line1 = "" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich habe 39°C gemessen und huste sehr verschleimt")
            EndIf

            If $Line1 = "Paracetamol 1g  i.v. bei Bedarf" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Beim messen sind es jetzt 37,3°C")
            ElseIf $Line2 = "Paracetamol 1g  i.v. bei Bedarf" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Beim messen sind es jetzt 37,3°C")
            EndIf

            If $Line1 Or $Line2= "Inhalation mit NaCl 3% 1-1-1-1" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm")
            ElseIf $Line1 = "Inhalation mit NaCl 3% 1-1-1-1" And $Line2 = "" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann auch schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm, aber habe immernoch Fieber")
            ElseIf $Line2 = "Inhalation mit NaCl 3% 1-1-1-1" And $Line1 = "" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann auch schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm, aber habe immernoch Fieber")
            EndIf




         Case $Button_Speichern
               FileDelete ("Anordnungen.txt")
               $C= GUICtrlRead($hCombo)
               $C1= GUICtrlRead($hCombo1)
               FileWrite ("Anordnungen.txt", $C & @CRLF & $C1)

               Case $Button_abrufen
                  $Fread = FileRead("Anordnungen.txt")

                  MsgBox (1, "Werte", "Das sind die aktuellen anordnungen:" & @CRLF & $Fread & @CRLF)


    EndSwitch
WEnd

And in line 91-97 i´ve my problem

 

If $Line1 Or $Line2= "Inhalation mit NaCl 3% 1-1-1-1" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm")
            ElseIf $Line1 = "Inhalation mit NaCl 3% 1-1-1-1" And $Line2 = "" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann auch schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm, aber habe immernoch Fieber")
            ElseIf $Line2 = "Inhalation mit NaCl 3% 1-1-1-1" And $Line1 = "" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann auch schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm, aber habe immernoch Fieber")
            EndIf

My Problem is, that it wont show the second version of the MsgBox when line 1 or 2 = "" and the other Line = "Inhalation mit NaCl 3% 1-1-1-1"

 

what i´ve done wrong?

Link to comment
Share on other sites

The first line is incorrect, it should be :

If $Line1 = "Inhalation mit NaCl 3% 1-1-1-1" And $Line2 = "Inhalation mit NaCl 3% 1-1-1-1" Then

Must be a and instead of a or

Edited by Nine
Link to comment
Share on other sites

But the first line works good,

but the second time

 

ElseIf $Line1 = "Inhalation mit NaCl 3% 1-1-1-1" And $Line2 = "" Then
            MsgBox (1, "Aktueller Zustand", "Herr Peters:" & @CRLF & "Ich kann auch schon viel besser atmen und der Husten und Schleim ist auch nicht mehr so schlimm, aber habe immernoch Fieber")

doenst work

 

i want to say with this line

 

If at Line1 stands something and line 2 not it sould get a other reaction

like that:

 

If you put in $Line1 "Inhale" and in $Line2 dont stand "Antipyretic" the Guy should say
"my cough is better by i still have fever"

 

and the same should happens if i turn the content of the lines so that in $Line1 = "Antipyretic" and $Line2 stands "Inhale

 

Only that the Guy say what he miss

if you give nothing "I have cough and fever"

if you give only "Inhale" and second line is empty he should say "my cough is better but i still have fever"

if  you give only "Antipyretic" and second line is empty he should say "my fever is better but i still have cough"

If you give him both he should say "Thanks no my cough is better and my fever too"

 

 

Do you understand what i mean ? haha :D

i tried to translate the script, so you can better comprehend what i mean with the text

 

#include <INet.au3>
#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>

$sList = ""

Global $Line1 = FileReadLine ("Anordnungen.txt", 1)
Global $Line2 = FileReadLine ("Anordnungen.txt", 2)


#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Patientensim", 615, 437, 192, 124)
$Tab1 = GUICtrlCreateTab(0, 0, 615, 437)

$TabSheet1 = GUICtrlCreateTabItem("Case")
            $Label4 = GUICtrlCreateLabel("Mr. Peters, 56 years old come to your emergency department and says >> Hi Dr. i have really bad cough since yesterday and my temperation is higher then 40°C.", 50 , 50, 500, 300)


$TabSheet2 = GUICtrlCreateTabItem("Patient room")
            GUISwitch($Form1,$Tabsheet2)
            $Pic1 = GUICtrlCreatePic("C:\Users\axelf\Desktop\patbett.jpg", 216, 48, 321, 237)
            $Button1 = GUICtrlCreateButton("Patient status", 40, 50, 153, 57)


$TabSheet3 = GUICtrlCreateTabItem("Arrangement")
            GUISwitch($Form1,$Tabsheet3)
            Global $Linie1 = FileReadLine ("Arrangement.txt", 1)
            Global $Linie2 = FileReadLine ("Arrangement.txt", 2)
            $Label1 = GUICtrlCreateLabel("current medication", 50 , 50, 200, 20)
            $Label3 = GUICtrlCreateLabel("Drug 1", 50 , 103, 100, 20)
            $hCombo = GUICtrlCreateCombo($Linie1, 250, 100, 200, 20)
            $hCombo1 = GUICtrlCreateCombo($Linie2, 250, 153, 200, 20)
            $Label2 = GUICtrlCreateLabel("Drug 2", 50 , 153, 100, 20)
            $Button_Speichern = GUICtrlCreateButton("update arrangement", 40, 250, 153, 57)
            $Button_abrufen = GUICtrlCreateButton("retrieve arrangement", 250, 250, 153, 57)
            GUICtrlSetData($hCombo, "Inhale with NaCl 3% 1-1-1-1|Paracetamol 1g  i.v. if necessary|")
            GUICtrlSetData($hCombo1, "Inhalation mit NaCl 3% 1-1-1-1|Paracetamol 1g  i.v. if necessary|")

$TabSheet4 = GUICtrlCreateTabItem("Diagnosis")
            GUISwitch($Form1,$Tabsheet4)
            $Button3 = GUICtrlCreateButton("Set diagnosis", 210, 200, 153, 57)
            $hCombo3 = GUICtrlCreateCombo("", 90, 100, 400, 20)
            GUICtrlSetData($hCombo3, "I26.- pulmonary embolism|J18. 9 pneumonia|J44. 1- COPD")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

         Case $Button3
            $Diag = GUICtrlRead($hCombo3)

            If $Diag = "J18. 9 Pneumonia" Then
               MsgBox (1, "Right Diagnosis", "Greate work, you set the right diagnosis. If you dont treat your patient yet, do it now")
            ElseIf $Diag = "I26.- pulmonary embolism" or "J44. 1- COPD" Then
               MsgBox (1, "Check diagnosis", "Please check one more time your answer")


            EndIf

         Case $Button1
            $Line1 = FileReadLine ("Arrangement.txt", 1)
            $Line2 = FileReadLine ("Arrangement.txt", 2)

            If Not FileExists("C:\Users\axelf\Desktop\Anordnungen.txt") Then
            $Line1 = ""
            EndIf


            if $Line1 = "" Then
            MsgBox (1, "current state", "Mr. Peters:" & @CRLF & "i´ve got over 40°C and a bad cough")
            EndIf

            If $Line1 = "Paracetamol 1g  i.v. if nessesary" Then
            MsgBox (1, "current state", "Mr Peters:" & @CRLF & "now i have 37,3°C")
            ElseIf $Line2 = "Paracetamol 1g  i.v. if nessesary" Then
            MsgBox (1, "current state", "Mr. Peters:" & @CRLF & "now i have 37,3°C")
            EndIf

            If $Line1 Or $Line2= "Inhale with NaCl 3% 1-1-1-1" Then
            MsgBox (1, "current state", "Mr. Peters:" & @CRLF & "now i can better breath and my cough is also better")
            ElseIf $Line1 <> "Inhalation mit NaCl 3% 1-1-1-1" And $Line2 <> "" Then
            MsgBox (1, "current state", "Herr Peters:" & @CRLF & "now i can better breath and my cough is also better, but i still have fever")
            ElseIf $Line2 <> "Inhalation mit NaCl 3% 1-1-1-1" And $Line1 <> "" Then
            MsgBox (1, "current state", "Herr Peters:" & @CRLF & "now i can better breath and my cough is also better, but i still have fever")
            EndIf




         Case $Button_Speichern
               FileDelete ("Arrangement.txt")
               $C= GUICtrlRead($hCombo)
               $C1= GUICtrlRead($hCombo1)
               FileWrite ("Arrangement.txt", $C & @CRLF & $C1)

               Case $Button_abrufen
                  $Fread = FileRead("Arrangement.txt")

                  MsgBox (1, "values", "Thats the current arrangements:" & @CRLF & $Fread & @CRLF)


    EndSwitch
WEnd

 

Link to comment
Share on other sites

7 minutes ago, Headsniper1997 said:

If at Line1 stands something and line 2 not it sould get a other reaction

elseif $line1 and not $line2 then
Quote

If you put in $Line1 "Inhale" and in $Line2 dont stand "Antipyretic" the Guy should say

else if $Line1 = "Inhale" and $Line2 <> "Antipyretic" then

Not sure what is the problem to be honest...

Edited by Nine
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...