IF <condition> {THEN}
<statements>
{ELSEIF <condition>
<statements>
{ELSE
<statements>
ENDIF
This newer form of block-structured IF statement was originally introduced in BASICplus, and later extended by A-Shell build 1125 to support the elseif clause. Advantages over the original if statement include:
• | No need to use colons to separate statements or ampersands to continue on to additional lines. |
• | For conditions that divide more than two mutually exclusive ways, you can use as many optional elseif clauses as you need. |
• | Ability to nest statements (including other IF...ENDIF or other control structures within the clauses.) |
Compatability: requires /X:2 or /RC. The A-Shell implementation is compatible with the BASICplus version at the source code level, except for the ELSEIF clause which is an A-Shell extension.
Example
if COST < 10 then
PAY'METHOD = PM_CASH
PETTY'CASH -= COST
elseif COST < 100 then
PAY'METHOD = PM_CHECK
CHECKING'ACCOUNT -= COST
elseif COST < 1000 then
PAY'METHOD = PM_CREDIT
else
CALL BUY'LOTTERY'TICKET
if TICKET = WINNER then
CALL GET'AN'ACCOUNTANT()
else
CALL ROB'BANK()
endif
endif
Comments
The compiler determines whether an if statement is of the original or new (block-structured form) by whether there is a statement following the condition prior to the end of the logical line. It is possible to insert old-style if statements into the body of new-style if..endif statements, but this goes against most tenets of style.
Once you get more than a couple of elseif clauses, you may find the conceptually-equivalent switch statement to be a better match.
Beware: elseif (one word) and else if (two words) are not equivalent! The former marks the beginning of a/another mutually exclusive conditional clause that ends with either the next elseif, else, or endif. The latter marks the beginning of an else clause, and within that clause, starts a new if statement.
Subtopics