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