MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
where initexp and endexp are expressions and relation is a relation. For example,
a=0
for(i=1;i<=10;++i)a+=i
is equivalent to the while example given earlier. C programmers should note that all
three items inside the parentheses must be specified; unlike C, bc does not let you omit
any of these expressions.
if (relation) statement
tests whether the given relation is true. If it is, bc performs the statement; otherwise, bc
skips over statement and goes to the next instruction. For example,
if ((a%2) == 0) "a is even"
displays a is even if a has an even value.
if (relation) statement1 else statement2
is similar to the simple if statement. If relation is true, it performs statement1; other-
wise, it performs statement2. It may be used as follows:
if ((a%2) == 0) "a is even" else "a is odd"
Note: There is no statement separator between "a is even" and the else keyword. This
differs from the C language.
Here is another example:
if (a<10) {
"a "
"is "; "less than 10 "
a
} else {
"a is"
" greater than 10 "
a
}
Note: The braces must be on the same line as the if and the else keywords. This is
because a newline or a semicolon right after (relation) indicates that the body of the state-
ment is null. One common source of errors in bc programs is typing the statement portion of
an if statement on a separate line. If –i is used, the interpreter displays a warning when if
statements with null bodies are encountered.
1-54 Commands and Utilities