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
ifand end in an ellipsis (...). (This first ellipsis indicates that you are writing a multi-caseifstatement, as opposed to a regularif ... then ... elseconditional 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
elsecase. - An
end ifline. This marks the end of the entire multi-caseifstatement.
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.