Conditional Statements

Conditional statements let your script make choices, carrying out some actions only under certain conditions, and other actions under other conditions.

If ... Then ... Else ...

Behavior: All forms of the if statement evaluate a condition expression, which must evaluate to a logical value (either true or false or one of the equivalent values yes or no, or on or off). An empty value is also treated as false. If the condition is true (or yes or on), SenseTalk executes the statement or statementList following the word then. If the condition is false (or no or off or empty), SenseTalk executes the statement or statementList following the word else (if it is present).

The if statement may take any of the following forms.

Note: statement is a single statement, while statementList may be multiple statements, each on its own line). The else portion is always optional.

Single Line

Syntax:

if condition then statement

 

if condition then statement else statement

Example:

if true then put "Yes!" -- Always puts "Yes!"

Example:

if balance < 1000 then put "The balance is getting low"

Example:

if the repeatIndex is greater than 5 then LogError "There is a problem." -- When used inside a repeat loop, the repeatindex() function tracks which iteration the repeat loop is on

Example:

put 4..10 into numList -- Creates the list '(4,5,6,7,8,9,10)'

repeat with each item of numList -- Iterates based on each item in the list

if it is an even number then log it -- Checks whether the value of the current item is an even number, and if so, logs the value

end repeat

Multi-Line Single Statements

Syntax:

if condition

then statement

 

if condition

then statement

else statement

Example:

put the date into dateoftransaction -- Stores the current date into a variable

if dateoftransaction is between date("January 1") and date("June 30")

then

put "First half" into transactionperiod

else

put "Second half" into transactionperiod

log transactionperiod

end if

Multiple Statement Block

Syntax:

if condition {then}

statementList

end if

if condition {then}

statementList

else

statementList

end if

Example:

if myString contains "testcase" then

delete ")" from myString

delete "(" from myString

log myString

end if

Example:

If imagefound("on")

then

Click foundimagelocation() -- Clicks the location where the previous image "on" was found

else

Click "off"

end if

Chained Conditionals

The final form shown in Chained Conditionals allows testing for a series of mutually exclusive conditions. Test any number of conditions by chaining as many else if blocks as needed, optionally followed by an else block before the closing end if to catch any cases that did not match any of the tested conditions. For a more streamlined approach, see Multi-Case If Statements.

Syntax:

if condition1 {then}

statementList

else if condition2 {then}

statementList

end if

if condition1 {then}

statementList

else if condition2 {then}

statementList

else

statementList

end if

Note: In Multiple Statement Blocks and Chained Conditionals, where the word then appears at the end of the line, it may be omitted for simplicity, if desired.

Example:

if imageFound(image:"DashHome",waitFor:0)

Click "DashHome"

else if imageFound(image:"StartHome",waitFor:0)

TypeText WindowsKey,"r"

WaitFor 8, "RunLine"

else

throw "Image not found", "Desktop not visible." -- Throws an exception if neither of the two conditions are met

end if

Multi-Case If Statements

Behavior: A multi-case if statement allows you to clearly and simply write conditional statements with numerous conditions and outcomes. This is useful for when you have a larger number of alternate conditions that you would normally use else if to indicate, because it simplifies the syntax. It also provides for additional flexibility and control over the flow of execution with fall through and keep checking cases statements.

Note: SenseTalk's multi-case if statement is similar to a switch statement or a case statement in other languages.

Three Forms of Multi-Case If

There are three forms of multi-case if statements. These variations in syntax provide flexibility, allowing you to customize the multi-case if statement to your testing needs.

All three forms use ellipses (...) to split the condition to be tested between the intial line and a number of subsequent lines that define cases. All three forms follow the same basic structure, which must meet these requirements, in order:

  • The first line must start with if and end in an ellipsis (...). (This first ellipsis indicates that you are writing a multi-case if statement, as opposed to a regular if ... then ... else conditional statement.)
  • There must be at least one possible matching case which completes the condition and provides code to be executed when that condition is met (when it evaluates as true).
  • Optional: An else case.
  • An end if line. This marks the end of the entire multi-case if statement.

The different forms allow you to divide the conditional statement into two parts, but at different points in the statement, allowing for different levels of flexibility.

Form 1: if value operator (each case is a value)

This form divides the conditional statement with an ellipsis following the comparison operator. This means that both the initial value and comparison operator are provided on the first line, with each case providing a second value for comparison.

A limited number of comparison operators can be used with this form. They include all of the following, along with any variations or negations (so in addition to "is", you can use "=", "are", "isn't", "is not", etc.):

  • is
  • isn't
  • matches
  • less than
  • greater than
  • less than or equal to
  • greater than or equal to
  • begins with
  • ends with
  • is in
  • contains
  • is within

Syntax:

If expression comparisonOperator ...

{...} value [then|:] singleStatement

{...} value [then|:]

statementList

{...} {else} {:} {singleStatement}

{...} value [then|:]

{statementList}

end If

Example:

This example shows how a multi-case if statement can be used to execute actions based on information stored in a variable.

set monthNumber to the month of "2020-08-19" -- This value could be set in a number of ways.

 

if monthNumber is ... -- This multi-case if statement looks at the provided date to determine the number of the month and log the name of that month accordingly.

1 : set monthName to "January"

2 : set monthName to "February"

3 : set monthName to "March"

4 : set monthName to "April"

5 : set monthName to "May"

6 : set monthName to "June"

7 : set monthName to "July"

8 : set monthName to "August"

9 : set monthName to "September"

10 : set monthName to "October"

11 : set monthName to "November"

12 : set monthName to "December"

else :

throw "Broken Calendar!" -- If the month doesn't match any of the months in the calendar year, this will throw an exception saying that the calendar is broken.

end if

 

log monthName -- Logs "August".

Form 2: if value ... (each case completes the condition)

In this form, the first line of the if statement only provides an initial value. Subsequently, each case begins with an operator to finish the conditional, as well as any statements to be executed in that case. This makes it easy to conduct multiple different tests on a single value.

Syntax:

If value ...

... operator expression [then|:] singleStatement

... operator expression [then|:]

statementList

{{...} else {:} {singleStatement}

{...} operator expression [then|:]

{statementList}}

end If

Note: The ellipses in front of the individual cases are required when using form 2.

Example:

This example shows how a multi-case if statement can be used with a variety of different operators in the same statement to conduct different tests on the same value. In form 2, cases can individually access properties to evaluate using the apostrophe-s ('s) and dot (.) syntax, as demonstrated by the code below.

set Wilbur to (nickName:"Wil", lastName:"Wilberforce", age:16)

 

put Wilbur's nickName & " is a ..."

 

if Wilbur's age ...

... is less than 1 then

put "Infant"

... <= 3 then

put "Toddler"

... is between 13 and 19 :

put "Teenager"

... less than 18 then put "Child"

... is more than 65 : put "Senior"

else

put "Grown-up"

end if

 

Output of this example as written:

Wil is a...

Teenager

Example:

This example accesses the name and age properties of an individual (stored in the person variable) using the apostrophe-s ('s) and dot (.) syntax.

put (name : samantha, age:10) into person

 

if person

's name begins with "S" then -- Accesses the name property using the apostrophe-s ('s) syntax.

put "Name with S"

keep checking cases -- Continues to check cases in order until one that evaluates as true is encountered. For more information, see Keep Checking Cases.

.age < 18 then put "Person is a minor" -- Accesses the age property using the dot (.) syntax.

end if

 

The output of this example as written is:

Name with S

Person is a minor

Form 3: if ... (each case is a complete conditional expression)

In this form, the conditional is divided immediately after the if, so that the entire conditional and code to be executed are provided by each case. This provides the greatest flexibility of the three forms, because each case is fully customizable. A single statement can be provided on the same line as the case, or a number of statements can be provided on subsequent lines as a statement list.

Syntax:

If ...

{...} value expression comparisonOperator [then|:] {singleStatement}

{...} value expression comparisonOperator [then|:]

{statementList}

{...} {else} {:}{singleStatement}

{...} value expression comparisonOperator [then|:]

{statementList}

end If

Example:

This example randomly generates values and then generates different output based on those values using the multi-case if statement form 3. You can copy and paste this example into a script and run it yourself.

set x to random (-5,5)

set y to random (-5,5)

put x,y

 

If ...

... x = 0 and y = 0 :

put "At the origin!"

... x = 0 :

put "On the y axis"

... y = 0 :

put "On the x axis"

... x > 0 and y > 0:

put "First quadrant"

else

put "Another quadrant"

End if

Case Writing and Execution Flow

The typical execution flow of a multi-case if statement is that each case is checked in turn to see if they evaluate as true (the conditions provided are met). After a case is encountered that is found to be true, the code for that case is executed and then control jumps to the next statement in the script following the end if at the end of the multi-case if statement.

When using a multi-case if statement, the flow of execution can be modified within cases, altering the outcome of the script significantly. For more information, see Cases, Fall Through to Execute Next Case, and Keep Checking Cases below.

Case Statements

Case statements complete the conditional of a multi-case if statement, and outline the actions to be taken when that case is evaluated to be true. The case in all forms consists of ... completedConditional [then|:] followed by the code to be executed when that case is evaluated as true. The ellipsis at the beginning of each case is usually optional (this is not available with form 2). This is different than the required ellipsis on the first line of the multi-case if statement.

The code to be executed can appear on the same line as the case condition if you only need to execute one command. To execute multiple commands, use a statement list. Statement lists start on the following line. To see this in action, refer to the syntax for any of the three forms for multi-case if statements.

If you do not need to execute anything in a certain case, and instead want the execution to "fall through", you can omit the code to be executed, and the next code found will execute. No other cases will be checked. For more information, see Fall Through to Execute Next Case Statements.

Fall Through to Execute Next Case Statement

Fall Through to Execute Next Case, which can also be shortened to Fall Through to Execute, Fall Through or Execute Next Case, causes execution to move on to the next case with a statement or statement list. No cases are evaluated in this process; the next available line of code is simply executed.

You can also cause a similar effect by writing cases that don't have any executable code. These automatically cause a fall through to the next instance of executable code.

Syntax:

Fall Through {to Execute {{the} Next Case}}

or

Execute {the} Next Case

Example:

This example shows Fall Through being used to execute different greetings based on a provided holiday and age. It uses a nested if ... then ... else conditional to determine the execution based on the age. The entire multi-case if statement is executed via a handler.

put "Halloween" into holiday

Put 25 into age

 

HolidayGreetings holiday, age

 

to Handle HolidayGreetings holiday, age -- A "to" handler that generates greetings based on the holiday provided

if holiday ...

... is "Christmas" then

Put "Go drink beer and eat food with family."

... is "Thanksgiving" then

put "Happy Autumn"

... is "Halloween" then

put "BOO!"

if age is less than 21 then -- A standard if...then...else conditional statement nested within the multi-case if statement allows for the fall through to be executed conditionally.

put "Go trick-or-treating!"

else

fall through to next case -- If the age is over 21, this falls through and executes the code associated with "St. Patrick's Day". That case is not evaluated.

end if

... is "St. Patrick's Day" then

put "Go drink beer and eat food with friends."

fall through -- This fall through statement causes the "else" to execute without being evaluated.

... else

put "Have a lovely " & holiday & "!"

end if

end HolidayGreetings

 

Output of this example as written:

BOO!

Go drink beer and eat food with friends.

Have a lovely Halloween!

Example:

This example shows how Fall Through to Execute Next Case can be executed by not adding any text to your script. This way of combining multiple cases into one lets the execution fall to the first case statement provided following any number of cases without statements.

put 9 into x

if x ...

... = 9: -- Because there is no code statement to be executed on this line, the behavior is to Fall Through and Execute by default.

... = 10: -- This value is not checked because 9 was executed as a Fall Through.

... = 11: -- This value is not checked, for the same reason as the above line.

put "Hooray!" -- If the value of x is either 9, 10, or 11 this statement will execute and "Hooray!" will be printed.

...>= 12:

put "Boo" -- If the value is greater than or equal to 12, this statement will execute and "Boo" will be printed.

end if

 

Output of this example as written:

Hooray!

Keep Checking Cases Statements

In situations where there it is possible for multiple cases to evaluate as true, you can use Keep Checking Cases to continue to evaluate cases after finding one to be true. Keep Checking Cases causes execution to continue until another case is encountered that evaluates as true. To have execution continue to check cases after that, another Keep Checking Cases must be included. Keep Checking Cases can be included at any point in a case, but is typically at the end.

Syntax:

keep checking cases

Example:

set Wilbur to (nickName:"Wil", lastName:"Wilberforce", age:13)

 

put Wilbur's nickName & " is a ..."

 

if Wilbur's age ... -- The beginning of this multi-case if statement specifies a value: Wilbur's age.

... is less than 1 then -- This case provides an operator and a value.

put "Infant" -- Code to be executed when the above case is found to be true.

... <= 3 then

put "Toddler"

... less than 14: -- This is found to be true in this example, because Wilbur is 13.

put "child"

keep checking cases -- If Wilbur is found to be less than 14 years old, execution will continue and the following cases will be checked until one is found to be true

... is between 13 and 19 : -- This is found to be true in this example, because Wilbur is 13.

put "Teenager"

keep checking cases-- If this case is found to be true (which it is in this example), the script would usually stop here (even if he was also less than 13). This Keep Checking Cases line means that it will instead continue to check cases until another true case is found.

... less than 18 then put "Minor" -- This case is found to be true, because Wilbur is 13.

... is more than 65 : put "Senior"

else

put "Grown-up"

end if

 

This topic was last updated on July 02, 2020, at 02:51:49 PM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2020 Eggplant