Statements (as in most algebraic languages) provide the sequencing of
expression evaluation. In 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 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
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 bc
statements and what they do: (Things
enclosed in brackets ( [ ] ) are optional parts of the statement.)
bc
uses a multi-character digit method of printing the numbers
where each higher base digit is printed as a base 10 number. The
multi-character digits are separated by spaces. Each digit contains the
number of characters required to represent the base ten value of
"obase -1". Since numbers are of arbitrary precision, some
numbers may not be printable on a single output line. These long
numbers will be split across lines using the "\" as the last character
on a line. The maximum number of characters printed per line is 70.
Due to the interactive nature of bc
, printing a number causes
the side effect of assigning the printed value to the special variable
last. This allows the user to recover the last value printed
without having to retype the expression that printed the number.
Assigning to last is legal and will overwrite the last printed
value with the assigned value. The newly assigned value will remain
until the next number is printed or another value is assigned to
last. (Some installations may allow the use of a single period
(.) which is not part of a number as a short hand notation for for
last.)
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 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 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 bc
processor to quit only when it is executed. For
example, "if (0 == 1) halt" will not cause bc
to terminate
because the halt
is not executed.
return
return
( expression )
Go to the first, previous, next, last section, table of contents.