条件文
条件文を使用すると、スクリプトは選択を行い、特定の条件下でのみ一部のアクションを実行し、他の条件下で他のアクションを実行します。
If ... Then ... Else ...
Behavior: 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
else amp;nbsp; statementList
end if
チェーンされた条件文では、行末に単語thenが表示される場合、必要に応じて簡潔さのために省略することができます。
例:
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." -- Neither of the two conditions are met, so an exception is thrown
end if
複数ケースのif文
Behavior: 複数ケースの if 文は、多くの条件と結果を持つ条件文を明確かつシンプルに書くことを可能にします。これは、else if を使って示す通常の代替条件が多くなるときに便利で、構文を単純化します。また、フォールスルーやケースのチェックを続けるステートメントにより、実行のフローをさらに柔軟に制御することができます。
SenseTalkの複数ケースのif文は、他の言語のswitch文やcase文に似ています。
複数ケースのifの三つの形式
複数ケースの if 文には3つの形式があります。これらの構文のバリエーションは柔軟性を提供し、テストのニーズに合わせて複数ケースの if 文をカスタマイズできます。
全3つの形式では、省略記号 (…) を使用して、テストする条件を初期行とケースを定義するいくつかの後続行の間で分割します。全3つの形式は同じ基本構造に従い、次の要件を順に満たす必要があります。
- 最初の行は
ifで始まり、省略記号 (...) で終わる必要があります(この最初の省略記号は、通常のif ... then ... else条件文ではなく、複数ケースのif文を書いていることを示しています)。 - 条件が真と評価された場合(真と評価される場合)に実行されるコードを提供する、少なくとも一つの可能なマッチングケースが存在する必要があります。