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.

Note: statement is a single statement, while statementList may be multiple statements, each on its own line). The else portion is always optional.

Single Line

Syntax:

if condition then statement

 

if condition then statement else statement

Note: SenseTalk is not case-sensitive.

Example:

if true then put "Yes!" // Always puts "Yes!"

Example:

if balance < 1000 then put "The balance is getting low"

Example:

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

Example:

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

Multi-Line Single Statements

Syntax:

if condition

then statement

 

if condition

then statement

else statement

Note: SenseTalk is not case-sensitive.

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

end if

Multiple Statement Block

Syntax:

if condition {then}

statementList

end if

 

if condition {then}

statementList

else

statementList

end if

Note: SenseTalk is not case-sensitive.

Example:

if myString contains "testcase" then

delete ")" from myString

delete "(" from myString

log myString

end if

Example:

If imagefound("on")

then

Click foundimagelocation() // Clicks the location where the previous image "on" was found

else

Click "off"

end if

Chained Conditionals

The final form shown in Chained Conditionals allows testing for a series of mutually exclusive conditions. Test any number of conditions by chaining as many else if blocks as needed, optionally followed by an else block before the closing end if to catch any cases that did not match any of the tested conditions. For a more streamlined approach, see Multi-Case If Statements.

Syntax:

if condition1 {then}

statementList

else if condition2 {then}

statementList

end if

 

if condition1 {then}

statementList

else if condition2 {then}

statementList

else

statementList

end if

Note: SenseTalk is not case-sensitive.
Note: In Multiple Statement Blocks and Chained Conditionals, where the word then appears at the end of the line, it may be omitted for simplicity, if desired.

Example:

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." // Throws an exception if neither of the two conditions are met

end if

 

This topic was last updated on March 12, 2020, at 07:26:53 PM.

Eggplant icon Eggplantsoftware.com | Documentation Home | User Forums | Support | Copyright © 2020 Eggplant