Arbortext Command Language > Using the Arbortext Command Language > if, while, for, switch
  
if, while, for, switch
The Command Language has four commands available to conditionalize execution of commands to a specific condition. These commands are if, while, for, and switch.
The if statement has the following structure:
if (condition) {
commands
} else {
commands
}
If the condition defined by condition is true (that is, not equal to 0 (zero)), then the first group of commands is executed. If the condition is false (that is, equal to 0), then the second group of commands (defined by the else statement) is executed. The else statement in this command is optional.
In the following example, a find command is executed. The if statement checks the $status variable to evaluate if the find was successful. If the condition $status == 0 (that is, if the find was successful, and thus the status variable is equal to 0), then the string is replaced, otherwise a message is issued indicating that the string was not found.
find "Peter"
if ($status == 0) {
delete_mark
insert_string "John"
} else {
message "Peter not found!"
}
The while command described below is a loop statement that relates repeated execution of a group of commands to a specific condition.
while (condition) {
commands
}
The commands defined by commands are repeatedly executed until condition becomes false (that is, equal to 0 (zero)).
In the next example, the find and replace commands are repeatedly executed until the find command is unsuccessful, which sets the status variable to something other than 0 (zero). The result is that every occurrence of the string in a document is replaced, rather than only the first one, as in the previous example.
find "Peter"
while ($status == 0) {
delete_mark
insert_string "John"
find "Peter"
}
The for command can also execute a group of commands repeatedly. There are two ways to use the command.
In the following example, for is used to execute a group of commands for every element in an array. If $array is an array variable with five elements, then commands is executed five times (for each element in this array). Each time, $index has the current value of the array index.
for ($index in $array) { commands }
The second way to use the for command is to relate its execution to a counter. In the following example, $count is set to a start value of 1. The commands are executed while $count is less than 5. After every cycle the counter is incremented by 1. This means that the commands are executed exactly four times.
for ($count=1; $count<5; $count+=1)
{
commands
}
Note that at the exit of the for loop the value of $count is equal to 5.
The syntax of the second use of for is just like in C language:
for (expression1;
expression2
;expression3)
{
commands
}
where each expression is optional. This form is equivalent to:
expression1
while (expression2) {
commands
expression3;
}
The switch command selects from a group of statements based on the value of the expression. The form of the statement is similar to that of the C programming language. It has the following syntax:
switch (expression)
{
case constant1:
statements
break
case constant2:
statements
break

default:
statements
break
}
The body of the switch statement must be enclosed in braces. The case statements do not need to be enclosed in braces.
The case and default prefixes do not alter flow of control. The break statement may be used to transfer control out of the switch statement.
Unlike C, the case constant value does not only need to be an integer. The constant may be a string value delimited by single or double quotes, or a regular expression delimited by slashes. For example, this:
switch (x) {
case 1:
message "x is 1"
break
case "one":
message "x is 'one'"
break
case /this|that/:
message "x contains 'this' or 'that'"
break
}
is equivalent to this:
if (x == 1) {
message "x is 1"
} else if (x == "one") {
message "x is 'one'"
} else if (match(x, "this|that")) {
message "x contains 'this' or 'that'"
}
Since the match function is used to evaluate a case prefix that specifies a regular expression, the statements following the case may use the match functions match_result, match_start, and match_length to extract parts of the matched expression.
If the case constants are all integer values or string constants of a single character, a jump table may be used to transfer control to the matched value. This executes considerably faster than a series of if, else if clauses. If the case values are all string values, a binary search is used (for more than three cases). A series of if, else if statements are compiled if the case values are regular expressions or are a mixture of string and integer value.
The case constant label allows for variable substitution. For example:
readIncompete=-2
readError=-1
readEOF=0
switch (read(ch, buf, 512)) {
case $readIncomplete:
...
case $readError:
...
case $readEOF:
...
default:
...
}