条件文
条件文を使用すると、スクリプトは選択を行い、特定の条件下でのみ一部のアクションを実行し、他の条件下で他のアクションを実行します。
If ... Then ... Else ...
振る舞い: if
文のすべての形式は条件式を評価し、それは論理値(true
または false
、または同等の値 yes
または no
、または on
または off
)に評価する必要があります。空の値も false
として扱われます。条件が true
(または yes
または on
)である場合、SenseTalk は単語 then
に続く文または文のリストを実行します。条件が false
(または no
または off
または空)である場合、SenseTalk は単語 else
に続く文または文のリストを実行します(存在する場合)。
if
文は次のいずれかの形式をとることができます。
statement は単一の文であり、statementList は複数の文であり、それぞれが自分の行にあります。else
部分は常にオプションです。
単行
if condition
then statementif condition then statement else statement
例:
if true then put "Yes!" -- Always puts "Yes!"
例:
if balance < 1000 then put "The balance is getting low"
例:
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
例:
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
出力:
4
6
8
10
複数行の単一文
if condition
then statement
else statementif condition {then}
statementList
end if
例:
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
複数のステートメントブロック
Syntax: if condition {then}
statementList
end ifif condition {then}
statementList
else
statementList
end if
複数のステートメントブロックと連鎖条件では、then
が行の最後にある場合、必要に応じて省略することも可能です。
例:
if myString contains "testcase" then
delete ")" from myString
delete "(" from myString
log myString
end if