Jump to content

How to use parentheses in If...Then


 Share

Recommended Posts

G'day everyone

The help file for "If...Then" states that:

"The expression can contain the boolean operators of AND, OR, and NOT as well as the logical operators <, <=, >, >=, =, ==, and <> grouped with parentheses as needed."

...but it doesn't give examples of how this could be accomplished. The following does not work for me:

$format = InputBox ( "Which image format do you want?", "Which image format do you want? Valid options are: JPG, BMP, GIF, PNG and TIF.", "JPG")
If [something] Then
MsgBox (0, "", "Sorry, that is not valid format.", 0)
Else
MsgBox (0, "", "Sorry, the programmer screwed up...", 0)
EndIf

I've tried the following [something]:

($format NOT "JPG") AND ($format NOT "BMP") AND ($format NOT "GIF") AND ($format NOT "PNG") AND ($format NOT "TIF")
$format (NOT "JPG") AND (NOT "BMP") AND (NOT "GIF") AND (NOT "PNG") AND (NOT "TIF")
$format = (NOT "JPG") AND (NOT "BMP") AND (NOT "GIF") AND (NOT "PNG") AND (NOT "TIF")
$format = (NOT "JPG" AND NOT "BMP" AND NOT "GIF" AND NOT "PNG" AND NOT "TIF")

The first two throws me an error. The second two tells me that the programmer screwed up. What I want is to be dol that the format is invalid (see Autoit code above). Obviously I'm doing something wrong here. Can anyone tell me what the correct syntax would be?

Thanks

Samuel

Link to comment
Share on other sites

Well I think the section between parentheses needs to be a valid equation like this:

($format = NOT "JPG")

or

NOT ($format = "BMP")

the above is incorrect, read PsaltyDS' post for the real functionality.

You can also use them the way you would in math, to evaluate the formula inside the parentheses before the rest of the formula like this:

2*5+5 = 15

2*(5+5) = 20

For your script I would advise using a Switch statement like this:

$format = InputBox ( "Which image format do you want?", "Which image format do you want? Valid options are: JPG, BMP, GIF, PNG and TIF.", "JPG")
Switch $format
    Case "JPG", "BMP", "GIF", "PNG", "TIF"
        ConsoleWrite("Case 1 is true" & @CRLF)
    Case Else
        ConsoleWrite("Case 2 is true" & @CRLF)
EndSwitch

edit and re-edit:

removed false information

Edited by Tvern
Link to comment
Share on other sites

As Tvern posted, Switch/Case/EndSwitch is a much better choice for the function. But to address the compare logic:

The NOT operator is simple boolean negation. And NOT is first operator in the order of operations, so be careful with it.

($format = NOT "JPG") will first perform (NOT "JPG") because of order of operations. For strings, if the string is not empty, it evaluates as TRUE, so "JPG" evaluates as Boolean TRUE, and (NOT "JPG") evaluates as FALSE. The actual contents of the string are not evaluated, so (NOT "FALSE") still evaluates as Boolean FALSE because the string "FALSE" is not empty. The result is $format = FALSE for every string except and empty one.

This version suffers from the same problem: NOT ($format = "BMP")

The correct test would be: If $format <> "BMP" Then

So, if for some reason you couldn't use Switch/Case/EndSwitch, the full test would be:

If ($format <> "JPG") AND ($format <> "BMP") AND ($format <> "GIF") AND ($format <> "PNG") AND ($format <> "TIF") Then
   ; Not valid input

Else
   ; Valid input

EndIf

Or, put the other way around:

If ($format = "JPG") OR ($format = "BMP") OR ($format = "GIF") OR ($format = "PNG") OR ($format  "TIF") Then
   ; Valid input

Else
   ; Not valid input

EndIf

:idea:

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

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