Sign in to follow this  
Followers 0
waiwai

What is the different select endselect from if..endif

5 posts in this topic

Hello

I am just learning the autoit. When i look up the help file with select....endselect function. But i unable to distinguish what is the different with if.....endif

I have writen the two syntax respectively. But the result is the same, anyone give me some comments.

Select...... Endselect

$var=1

$var2="test"

Select

Case $var = 1

MsgBox(0, "", "第一个条件成立")

Case $var2 = "test"

MsgBox(0, "", "第二个条件成立")

Case Else

MsgBox(0, "", "所有条件都不成立!")

EndSelect

IF.....Endif

$var=1

$var2="test"

If $var=1 then

MsgBox(0, "", "第一个条件成立")

elseif $var2="test" then

MsgBox(0, "", "第二个条件成立")

else

MsgBox(0, "", "所有条件都不成立!")

Endif

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Select gives slightly more lisible code, but both construct are exactly equivalent.

There is nonetheless one big difference: you can "chain" successive Cases within Select or Switch using ContinueCase, which has no If... counterpart. Note that ContinueCase has the reverse semantics of the C break!

EDIT:

I didn't notice at first:welcome to the AutoIt forum!

Edited by jchd

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

#3 ·  Posted (edited)

Hello

I am just learning the autoit. When i look up the help file with select....endselect function. But i unable to distinguish what is the different with if.....endif

I have writen the two syntax respectively. But the result is the same, anyone give me some comments.

Welcome!!!!! I know this post will be long, but i wanted to include some good information for you about how to 'think' about what you want to evaluate. As the previous poster said, as well, there are additional reasons to use 'Select'/'Switch' as well, such as 'continueCase', which will allow you to skip to the next branch of the evalaution, which If/Then cannot do. But, that isn't covered here, instead, i'll talk about logic:

In this case, it's not a matter of the result being different, as to determine the best method to use for conditional statement evaluation. It's the data you have to evaluate that chooses if you use Select, Switch or If statements.

When evaluating data that provides a constant set, using Switch or Select is often better than using If/Endif's only because the data is known to the programmer. Meaning, for example, if we were evaluating the hour of the day, we know there to be only 24 possibilities (0 through 23); using Select or Switch, in this case, evaluates the data in a more structured manner, than simply typing out each if/then/else/endif statement to evaluate the hours.

If, however, we were not fully aware of all the possible constants being entered, such as evaluating data entered by a user, we would still use 'If/Then' statements to help route the entered information properly.

Some examples of this, is when, in autoit, we use input boxes, input controls, edit boxes, etc, things that ask users for information, and on button pushes, we evaluate if the information they entered is in the PROPER format. In those cases, we use IF statements:

$input = GUICtrlCreateInput("Enter ipaddress:  ", 0, 0, 80, 20)
$button = GUICtrlCreateButton("Submit", 85, 0, 50, 20)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $button
            If StringStripWS(GUICtrlRead($input), 8) <> "" Then
                ;XXXXX COMMANDS HERE
            Else
                ;XXXX IGNORE COMMANDS HERE
            EndIf
    EndSwitch
WEnd

Note, in the above, that i'm using an IF declaration to check to see if the data entered in the input box was empty or not. If it was not empty (<> ""), then it would process the commands after the 'Then' on the first condition that is true (in this case, the first condition); and if the input box was indeed blank on the button press, it would execute what's in between the else/endif block (the ignore section). We do it this way, simply because we do not know the data being entered, we only know it's possible states: Containing data, being empty.

Now, if we knew the states, we would be able to use Select/Switch to a much higher degree. For example, look at the code above, and look at what the guigetmsg() command is doing. Every cpu cycle, it will poll the gui to see if any commands took place, and if they did, it will return that command control identifier via the $msg variable. I'm using the 'Switch' command to handle that processing. I'm then using 'cases' to evaluate what those commands will be doing. As i know, in this case, what commands I want it to check for, ($button), i can use Switch to better define what i want the program to do.

The ability to optmize what we read, and how we debug a program is just as important as being able to execute a script that's fast. For example:

$var = @HOUR
$timer = TimerInit()
Switch $var
    Case 0 To 5
        ConsoleWrite("0 to 5am" & @CRLF)
    Case 6 To 23
        ConsoleWrite("6 or higher" & @CRLF)
    Case Else
        ConsoleWrite("nothing" & @CRLF)
EndSwitch
ConsoleWrite("Time:  " & TimerDiff($timer) & @CRLF)

$var = @HOUR
$timer = TimerInit()
Select
    Case $var < 6
        ConsoleWrite("less than 6am" & @CRLF)
    Case $var > 6
        ConsoleWrite("6am or higher" & @CRLF)
    Case Else
        ConsoleWrite("nothing" & @CRLF)
EndSelect
ConsoleWrite("Time:  " & TimerDiff($timer) & @CRLF)

Provides the following results:

0 to 5am

Time: 0.13024247184356

less than 6am

Time: 0.0288798524522676

Granted, the select was working much faster, but it was much harder to read than the first instance. Why? Because using a 'Switch' we can evaluate data as specifically as possible (we can 'Switch' a specific 'Variable' and then run cases against it's data), while as using a 'Select', we have to specify what we are looking for in the case itself (i.e. we are not evaluating a single data source).

Can this be accomplished with an If/Then? Yes, but, it would look just as messy, if not more messy, than the Select. Does that mean you shouldn't use it? No, it just means for ease of use in constant data sets, if/thens are less desireable than using Switch/Select. The rule of thumb i use is: 'If evaluating user input, use IF's, if evaluating a set of data already known, use Switch, if evluating multiple sets, use select'; this allows me to be able to diverse in how i handle data evaluation.

Lastly, nesting is vital to programming, or even scripting as the case may be for autoit. Eventually we have to nest our variables to get what we want:

$input = GUICtrlCreateInput("Enter ipaddress:  ", 0, 0, 80, 20)
$button = GUICtrlCreateButton("Submit", 85, 0, 50, 20)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $button
            If StringStripWS(GUICtrlRead($input), 8) <> "" Then
                ConsoleWrite("Not blank" & @CRLF)
                if $input = "Test" Then
                    ; run TEST commands
                Else
                    ; run OTHER commands
                EndIf
            Else
                ;XXXX IGNORE COMMANDS HERE
            EndIf
    EndSwitch
WEnd

Notice the nested IF there? I was able to evaluate if the input box was blank, and then also if the input box was NOT blank, to see if it was a specific word 'test' and evaluate that as well, or just do othe commands if it wasnt. This affords me a much wider posibility within the 'TRUE' condition of the input box not being blank. Can i accomplish that with a 'select' or a 'switch'? yes, and No, but why bother writing cases for only small sets of data?

Now, let's say we are evaluating the following scenario:

* all numbers, 0 through 1000

* evaluate for values gruped by 10's (meaning each set 0 to 10, 11 to 20, 21 TO 30, etc) will have a different function ran

What would i use?

Could i use If/Then? Yes, but that's a TON of writing, and would contain lots of $var statements. So not very easy to read, and takes too much space and adds extra characters in.

Could I use Select? Yes, but then again, i'm only evaluating a single variable, ie the one $var containing any number from 0 to 1000; so why bother doing a select, if we only have a single variable?

Could i use Switch? Yes! As i'm just evaluating a single set of numbers, it's easier to mess w/ the result, than worry about figuring out WHAT to evaluate when i'm evaluating it.

In a similiar scenario:

* All numbers, 0 through 1000

* All numbers, 15000 through 30000

* evaluate for values grouped by 10's (meaning each set 0 to 10, 11, to 20, 21 to 30, etc) will have different functions ran

What would I use?

Could I use If/Then? Again yes, but it'd be extra words, $vars, etc, that are not necessary, and take up extra space in the script

Could I use Switch? Again, yes, however, what happens if I want to do a complex evaluation, such as if in the first $variable (numbers 0 through 1000), i want to interrelate them to $variable2 (numbers 15000 thorugh 30000), when variable 2 is less than 16000? In that case, it's kind of annoying to have to nest the extra SWITCH in there.

Could I use Select? Oh yes! Since select doesn't bind itself to a single result set (Switch $var, for example, binds itself to a result set for $var), i can evaluate each $variable set within the select statement:

$var = 6
$var2 = 15550
Select
    Case $var < 11 AND $var2 > 15999
        ConsoleWrite("var 1 is less than 11, and var 2 is greater than 15999" & @CRLF)
    Case $var2 < 16000 And $var < 11
        ConsoleWrite("var 2 is less than 16000 and $var is less than 11" & @CRLF)
EndSelect

Note in the above cases, we can select the proper values and interrelate the sets of data. Can we do this with If/Then/Else/Endif? Yes, but then we have more $var data to inject, and more then's, etc, than we really want. Whereas if we have a subset of 'cases' it's easier to read, and often easier to debug when things go wrong.

I hope that gave you some clarification.

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites

Another practical distinction between Switch and If... is that the test expression is only evaluated once in a Switch. So you may use a side-effect function as test expression with Switch, but you need to use an intermediate variable with If and (of course) Select. It doesn't mean I recommend the systematic use of such side-effect (it's a potential maintenance pitfall), but it's nonetheless possible. Also and more conventionnally, when your test variable is an element of a non-trivial array or uses computed index (e.g. $MyArray[$i - 1][3 * $k + 2][...]), you only evaluate once the indexes and underlying computations.

Func IncRef(ByRef $n)
    $n += 1
EndFunc

Local $i = 8

Switch IncRef($i)   ; evaluated only once
    Case 1 To 3
        ConsoleWrite('Case 1..3 - $i = ' & $i & @LF)
    Case 4 To 6
        ConsoleWrite('Case 4..6 - $i = ' & $i & @LF)
    Case 7 to 8
        ConsoleWrite('Case 7..9 - $i = ' & $i & @LF)
    Case Else
        ConsoleWrite('Case Else - $i = ' & $i & @LF)
EndSwitch

$i = 8

If  IncRef($i) >= 1 And IncRef($i) <= 3 Then    ; Oops!
    ConsoleWrite('If 1..3 - $i = ' & $i & @LF)
ElseIf IncRef($i) >= 4 And IncRef($i) <= 6 Then ; Oops!
    ConsoleWrite('If 4..6 - $i = ' & $i & @LF)
ElseIf IncRef($i) >= 7 And IncRef($i) <= 9 Then ; Oops!
    ConsoleWrite('If 7..9 - $i = ' & $i & @LF)
Else
    ConsoleWrite('Else  - $i = ' & $i & @LF)
EndIf

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

#5 ·  Posted (edited)

I have read the advantage of select....endselect from help. It said if you want to test a large number of conditions as it is generally easier to read than than large If/ElseIf type block.

That's i agree

Edited by waiwai

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