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