Sign in to follow this  
Followers 0
IvanCodin

If Then syntax usage

11 posts in this topic

I am using the following syntax in my if statement. It does not error when I run the syntax checker in Scite but it doesn't appear to work as I expect it to. Am I using the correct syntax?.

Below is a snippet from my script that shows what I am trying to do.

If $Type = "2" or "3" or "4" or "5" Then
            FileWrite($oputfile, "ip address" & @CRLF)
            FileWrite($oputfile, @CRLF)
        Else
            MsgBox(0, "Info", "System type is not standard")
        EndIf

Me

Share this post


Link to post
Share on other sites



It should be like this

If $Type = "2" or $Type = "3" or $Type = "4" or $Type = "5" Then
            FileWrite($oputfile, "ip address" & @CRLF)
            FileWrite($oputfile, @CRLF)
        Else
            MsgBox(0, "Info", "System type is not standard")
        EndIf

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Thanks! I am suprised Scite didn't catch it!!

I am afraid to ask but ....... I looked in the help file and didn't see an example. Was it there?

Me

Share this post


Link to post
Share on other sites

Change If $Type = "2" or "3" or "4" or "5" Then to If $Type = "2" Or $Type = "3" Or $Type = "4" Or $Type = "5" Then.

Note the caps aren't really necessary, I couldn't help it though.. :mellow:


- 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

Yep I figured that out after I put it in the script and it ran successfully!! As usual the program is smarter than me :-)

One last question are global variables case sensative in Autoit? I would take it from the comment they arent't.

Me

Share this post


Link to post
Share on other sites

Thanks! I am suprised Scite didn't catch it!!

I am afraid to ask but ....... I looked in the help file and didn't see an example. Was it there?

The checker won't catch this because it is a valid syntax (besides it doesn't behave as you were expecting).

If <condition> Then ...

Now $Type = "2" Or "3" is a perfectly valid <condition>. It boils down to evaluating $Type = "2" test as a boolean (this works as you whish) and then "3" as a boolean. When you open the helpfile >> Language Reference >> DataTypes, you read:

The other way around however is different. When you use string comparisons with Boolean values, the following rules apply:

Only an empty string ("") will be a Boolean false

Any other string values (including a string equal "0") will be a Boolean true

Thus "3" will evaluate as Boolean True (and the counpound condition will stop there due to Or short-circuit).

Hope this is clearer now.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

No keyword or user created object (variable, constant, enumeration, function) is case sensitive in AutoIt, either global, local or static.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

If you're referring to the Or's I capitalized, they're not Global variables, they are Logical Operators, and they aren't case sensitive. In the docs, they're all caps, but they'll work either lowercase, all uppercase, or the first letter capitalized.


- 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

If you are checking one variable to many different values I reccomend using a switch statement like this:

$Type = 1 ;Set the value of $Type here
Switch String($Type) ;Autoit should make $Type a string when compared to one, but just to be sure we'll make it one, You could also make it a number and compare to numbers.
    Case "2","3","4","5"
        ConsoleWrite("$Type = 2, or 3, or 4, or 5" & @CRLF)
    Case "1"
        ConsoleWrite("$Type = 1" & @CRLF)
    Case Else
        ConsoleWrite("$Type = not 1, or 2, or 3, or 4, or 5" & @CRLF)
EndSwitch

Share this post


Link to post
Share on other sites

Tvern,

;Autoit should make $Type a string when compared to one

No, it's more involved.

In If 1 = $var Then ... AutoIt does If 1 = Number($var) Then ...

But in

If 1 == $var Then ... AutoIt does If String(1) == String($var) Then ...


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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