3.5. The if-else if Statement
Writing nested if statements can become unwieldy, as you can see from the previous example. Luckily, a special form of the if statement comes to the rescue and enables you to combine the if with the else on the same line. The general format of the if-else if statement is
if Boolean-expression1 then statement statement ... else if Boolean-expression2 then statement statement ...
else if Boolean-expression3 then statement statement ... else statement statement ... end if
Execution of the if-else if statement proceeds as follows:
Boolean-expressionl gets evaluated first. If the result is true, the statements that follow up to the first else if get executed, and all remaining statements up to the terminating end if get skipped. Otherwise, Boolean-expression2 gets evaluated; if the result of its evaluation is true, the statements that follow up to the next else if get executed and the remaining statements up to the end if get skipped.
This process of evaluating Boolean expressions continues until either one of the expressions evaluates true or the optional else clause is encountered. If the else is present and none of the preceding Boolean expressions evaluated true, the statements that follow the else up to the terminating end if get executed.
If the else is not included in the if-else if, it's possible that none of the statements in the if-else if will be executed.
Here's how you would rewrite the nested if statement from the previous section in the form ...
Get Beginning AppleScript® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.