(bc.info)Statements


Next: Functions Prev: Expressions Up: Top
Enter node , (file) or (file)node

4 Statements
************

Pseudo Statements
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.)

'EXPRESSION'
     This statement does one of two things.  If the expression starts
     with "<variable> <assignment> ...", it is considered to be an
     assignment statement.  If the expression is not an assignment
     statement, the expression is evaluated and printed to the output.
     After the number is printed, a newline is printed.  For example,
     "a=1" is an assignment statement and "(a=1)" is an expression that
     has an embedded assignment.  All numbers that are printed are
     printed in the base specified by the variable OBASE.  The legal
     values for OBASE are 2 through BC_BASE_MAX (Note: Environment
     Variables).  For bases 2 through 16, the usual method of writing
     numbers is used.  For bases greater than 16, '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.)

'STRING'
     The string is printed to the output.  Strings start with a double
     quote character and contain all characters until the next double
     quote character.  All characters are taken literally, including any
     newline.  No newline character is printed after the string.

'print LIST'
     The '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.

'{ STATEMENT_LIST }'
     This is the compound statement.  It allows multiple statements to
     be grouped together for execution.

'if ( EXPRESSION ) STATEMENT1 [else STATEMENT2]'
     The if statement evaluates the expression and executes statement1
     or statement2 depending on the value of the expression.  If the
     expression is non-zero, statement1 is executed.  If statement2 is
     present and the value of the expression is 0, then statement2 is
     executed.  (The 'else' clause is an extension.)

'while ( EXPRESSION ) STATEMENT'
     The while statement will execute the statement while the expression
     is non-zero.  It evaluates the expression before each execution of
     the statement.  Termination of the loop is caused by a zero
     expression value or the execution of a 'break' statement.

'for ( [EXPRESSION1] ; [EXPRESSION2] ; [EXPRESSION3] ) STATEMENT'
     The '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'
     This statement causes a forced exit of the most recent enclosing
     'while' statement or 'for' statement.

'continue'
     The 'continue' statement (an extension) causes the most recent
     enclosing 'for' statement to start the next iteration.

'halt'
     The '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 the value 0 from a function.  (Note: Functions.)

'return ( EXPRESSION )'
     Return the value of the expression from a function.  (Note:
     Functions.)  As an extension, the parenthesis are not required.


automatically generated by info2www version 1.2.2.9