Statements (as in most algebraic languages) provide the sequencing of expression evaluation. In @command{bc} statements are executed "as soon as possible." Execution happens when a newline in encountered and there is one or more complete statements. Due to this immediate execution, newlines are very important in @command{bc}. In fact, both a semicolon and a newline are used as statement separators. An improperly placed newline will cause a syntax error. Because newlines are statement separators, it is possible to hide a newline by using the backslash character. The sequence "\<nl>", where <nl> is the newline appears to @command{bc} as whitespace instead of a newline. A statement list is a series of statements separated by semicolons and newlines. The following is a list of @command{bc} statements and what they do: (Things enclosed in brackets ( [ ] ) are optional parts of the statement.)
print
list
print
statement (an extension) provides another method of
output. The list is a list of strings and expressions separated by
commas. Each string or expression is printed in the order of the list.
No terminating newline is printed. Expressions are evaluated and their
value is printed and assigned to the variable last
. Strings in
the print statement are printed to the output and may contain special
characters. Special characters start with the backslash character (\e).
The special characters recognized by @command{bc} are "a" (alert or
bell), "b" (backspace), "f" (form feed), "n" (newline), "r" (carriage
return), "q" (double quote), "t" (tab), and "\e" (backslash). Any other
character following the backslash will be ignored.
if
( expression ) statement1 [else
statement2]
else
clause is an extension.)
while
( expression ) statement
break
statement.
for
( [expression1] ; [expression2] ; [expression3] ) statement
for
statement controls repeated execution of the statement.
Expression1 is evaluated before the loop. Expression2 is
evaluated before each execution of the statement. If it is non-zero,
the statement is evaluated. If it is zero, the loop is terminated.
After each execution of the statement, expression3 is evaluated
before the reevaluation of expression2. If expression1 or
expression3 are missing, nothing is evaluated at the point they
would be evaluated. If expression2 is missing, it is the same as
substituting the value 1 for expression2. (The optional
expressions are an extension. POSIX @command{bc} requires all three
expressions.) The following is equivalent code for the for
statement:
expression1; while (expression2) { statement; expression3; }
break
while
statement or for
statement.
continue
continue
statement (an extension) causes the most recent enclosing
for
statement to start the next iteration.
halt
halt
statement (an extension) is an executed statement that
causes the @command{bc} processor to quit only when it is executed. For
example, "if (0 == 1) halt" will not cause @command{bc} to terminate
because the halt
is not executed.
return
return
( expression )
These statements are not statements in the traditional sense. They are not executed statements. Their function is performed at "compile" time.
limits
quit
quit
statement is read, the @command{bc} processor
is terminated, regardless of where the quit
statement is found. For
example, "if (0 == 1) quit" will cause @command{bc} to terminate.
warranty
Go to the first, previous, next, last section, table of contents.