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.
statement is a single statement, while statementList may be multiple statements, each on its own line). The else
portion is always optional.
Single Line Conditionals
Syntax:
if condition then statementif 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 counter is greater than 5 then LogError "There is a problem." -- When used inside a repeat loop, the counter() function tracks which iteration the repeat loop is on
Example:
put 4..10 into numRange-- Creates a numeric range from 4 to 10
repeat with each item of numRange -- Iterates based on each value in the range
if it is an even number then put it -- Checks whether the value of the current item is an even number, and if so, displays the value
end repeat
Output:
4
6
8
10
Multi-Line Single Statement Conditionals
Syntax:
if condition
then statementif 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
Multiple Statement Block Conditionals
Syntax: if condition {then}
statementList
end ifif condition {then}
statementList
else
statementList
end if
In Multiple Statement Block Conditionals, where the word then
appears at the end of the line, it may be omitted for simplicity, if desired.
Example:
if myString contains "testcase" then
delete ")" from myString
delete "(" from myString
log myString
end if
Example:
If imagefound("on")
Click foundimagelocation() -- Clicks the location where the previous image "on" was found
else
Click "off"
end if
Chained Conditionals
The 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 ifif condition1 {then}
statementList
else if condition2 {then}
statementList
else statementList
end if
In 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 might otherwise 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.
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-caseif
statement, as opposed to a regularif ... 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-caseif
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 another 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 equal to
, etc.):
is
matches
less than
greater than
less than or equal to
greater than or equal to
begins with
ends with
is in
contains
is within
If expression comparisonOperator ...
{...} value [then | :] singleStatement {...} value [then | :] statementList {...} {else} {:} {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
put monthName --> "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} {:}
{statementList}
end If
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.
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:
Wil is a...
Teenager
Example:
In form 2, cases can individually access properties to evaluate. 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
Output:
Name with S
Person is a minor