条件文
条件文を使用すると、スクリプトは選択を行い、特定の条件下でのみ一部のアクションを実行し、他の条件下で他のアクションを実行します。
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
が表示される場合、必要に応じて簡潔さのために省略することができます。
例:
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文
挙動: 複数ケースの if
文は、多くの条件と結果を持つ条件文を明確かつシンプルに書くことを可能にします。これは、else if
を使って示す通常の代替条件が多くなるときに便利で、構文を単純化します。また、フォールスルーやケースのチェックを続けるステートメントにより、実行のフローをさらに柔軟に制御することができます。
SenseTalkの複数ケースのif文は、他の言語のswitch文やcase文に似ています。
複数ケースのifの三つの形式
複数ケースの if
文には3つの形式があります。これらの構文のバリエーションは柔軟性を提供し、テス トのニーズに合わせて複数ケースの if
文をカスタマイズできます。
全3つの形式では、省略記号 (…) を使用して、テストする条件を初期行とケースを定義するいくつかの後続行の間で分割します。全3つの形式は同じ基本構造に従い、次の要件を順に満たす必要があります。
- 最初の行は
if
で始まり、省略記号 (...
) で終わる必要があります(この最初の省略記号は、通常のif ... then ... else
条件文ではなく、複数ケースのif
文を書いていることを示しています)。 - 条件が真と評価された場合(真と評価される場合)に実行されるコードを提供する、少なくとも一つの可能なマッチングケースが存在する必要があります。
- オプション:
else
ケース。 end if
行。これは複数ケースのif
文の終わりを示します。
異なる形式では、条件文を2つの部分に分割できますが、文内の異なる点で、異なるレベルの柔軟性が可能です。
形式1:if
value operator ...
(各ケースは値)
この形式では、比較演算子の後の省略記号で条件文を分割します。これは、最初の行で初期値と比較演算子が提供され、各ケースが比較のための第2の値を提供することを意味します。
この形式では、限定的な数の比較演算子を使用できます。これらには以下のものが含まれ、いずれもその変形や否定形(つまり、「is」の他に「=」、「are」、「isn't」、「is not」など)を使用できます:
is
matches
less than
greater than
less than or equal to
greater than or equal to
begins with
ends with
is in
contains
is within
構文: If expression comparisonOperator ... {...} value [then | :] singleStatement {...} value [then | :] statementList {...} {else} {:} {statementList} end If
例:
この例は、変数に格納された情報に基づいてアクションを実行するために、マルチケース if
文をどのように使用できるかを示しています。
set monthNumber to the month of "2020-08-19" -- この値はいくつかの方法で設定できます。
>
if monthNumber is ... -- このマルチケースif 文は、提供された日付を見て月の番号を決定し、その月の名前を対応するようにログに記録します。
1 : set monthName to "1月"
2 : set monthName to "2月"
3 : set monthName to "3月"
4 : set monthName to "4月"
5 : set monthName to "5月"
6 : set monthName to "6月"
7 : set monthName to "7月"
8 : set monthName to "8月"
9 : set monthName to "9月"
10 : set monthName to "10月"
11 : set monthName to "11月"
12 : set monthName to "12月"
else :
throw "カレンダーが壊れています!" -- もし月がカレンダーの中のどの月とも一致しなければ、このカレンダーが壊れているという例外を投げます。
end if
log monthName -- "8月"をログに記録します。
形式2: if
value ...
(各ケースが条件を完成させる)
この形式では、if文の最初の行は初期値のみを提供します。その後、各ケースは条件を完成させるための演算子と、そのケースで実行うべき文を開始します。これにより、単一の値に対して複数の異なるテストを容易に行うことができます。
If value ...
... operator expression [then | :] singleStatement
... operator expression [then | :]
statementList
{...} {else} {:}
{statementList}
end If
形式2を使用する際は、各ケースの前に省略記号が必要です。
例:
この例では、マルチケース if
文を使用して、同じ文の中でさまざまな演算子を使用して同じ値に対する異なるテストを行う方法を示しています。形式2では、下のコードが示すように、ケースはそれぞれがアポストロフィーs ('s
) およびドット (.
) 文法を使用して評価するプロパティに個別にアクセスできます。
set Wilbur to (nickName:"Wil", lastName:"Wilberforce", age:16)
put Wilbur's nickName & " は..."
if Wilbur's age ...
... is less than 1 then
put "Infant"
... <= 3 then
put "幼児"
... is between 13 and 19 :
put "Teenager"
... less than 18 then put "子供"
... is more than 65 : put "シニア"
else
put "赤ん坊"
end if