条件文
条件文を使用すると、スクリプトは選択を行い、特定の条件下でのみ一部のアクションを実行し、他の条件下で他のアクションを実行します。
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 statementif condition
then statement
else statement
例:
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
複数のステートメントブロック
構文: 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
例:
If imagefound("on")
Click foundimagelocation() -- Clicks the location where the previous image "on" was found
else
Click "off"
end if
連鎖条件
最終的に示される「連鎖条件」は、相互に排他的な一連の条件をテス トできます。else if ブロックを連鎖させて必要なだけ条件をテストし、最終的に end if の前に else ブロックを配置して、テストした条件のいずれにも一致しないケースを捉えます。より洗練されたアプローチについては、「複数のケースに対するif文」(#multi-case-if)を参照してください。
構文:
if condition1 {then}
statementList
else if condition2 {then}
statementList
end ifif condition1 {then}
statementList
else if condition2 {then}
statementList
else statementList
end if
チェーンされた条件文では、行末に単語thenが表示される場合、必要に応じて簡潔さのために省略することができます。