1 1 4 Statements 1 ************ 1
1 · Pseudo Statements 1 1 Statements (as in most algebraic languages) provide the sequencing of 1 expression evaluation. In 'bc' statements are executed "as soon as 1 possible." Execution happens when a newline in encountered and there is 1 one or more complete statements. Due to this immediate execution, 1 newlines are very important in 'bc'. In fact, both a semicolon and a 1 newline are used as statement separators. An improperly placed newline 1 will cause a syntax error. Because newlines are statement separators, 1 it is possible to hide a newline by using the backslash character. The 1 sequence "\<nl>", where <nl> is the newline appears to 'bc' as 1 whitespace instead of a newline. A statement list is a series of 1 statements separated by semicolons and newlines. The following is a 1 list of 'bc' statements and what they do: (Things enclosed in brackets ( 1 [ ] ) are optional parts of the statement.) 1 1 'EXPRESSION' 1 This statement does one of two things. If the expression starts 1 with "<variable> <assignment> ...", it is considered to be an 1 assignment statement. If the expression is not an assignment 1 statement, the expression is evaluated and printed to the output. 1 After the number is printed, a newline is printed. For example, 1 "a=1" is an assignment statement and "(a=1)" is an expression that 1 has an embedded assignment. All numbers that are printed are 1 printed in the base specified by the variable OBASE. The legal 11 values for OBASE are 2 through BC_BASE_MAX (⇒Environment Variables). For bases 2 through 16, the usual method of writing 1 numbers is used. For bases greater than 16, 'bc' uses a 1 multi-character digit method of printing the numbers where each 1 higher base digit is printed as a base 10 number. The 1 multi-character digits are separated by spaces. Each digit 1 contains the number of characters required to represent the base 1 ten value of "OBASE -1". Since numbers are of arbitrary precision, 1 some numbers may not be printable on a single output line. These 1 long numbers will be split across lines using the "\" as the last 1 character on a line. The maximum number of characters printed per 1 line is 70. Due to the interactive nature of 'bc', printing a 1 number causes the side effect of assigning the printed value to the 1 special variable LAST. This allows the user to recover the last 1 value printed without having to retype the expression that printed 1 the number. Assigning to LAST is legal and will overwrite the last 1 printed value with the assigned value. The newly assigned value 1 will remain until the next number is printed or another value is 1 assigned to LAST. (Some installations may allow the use of a 1 single period (.) which is not part of a number as a short hand 1 notation for for LAST.) 1 1 'STRING' 1 The string is printed to the output. Strings start with a double 1 quote character and contain all characters until the next double 1 quote character. All characters are taken literally, including any 1 newline. No newline character is printed after the string. 1 1 'print LIST' 1 The 'print' statement (an extension) provides another method of 1 output. The LIST is a list of strings and expressions separated by 1 commas. Each string or expression is printed in the order of the 1 list. No terminating newline is printed. Expressions are 1 evaluated and their value is printed and assigned to the variable 1 'last'. Strings in the print statement are printed to the output 1 and may contain special characters. Special characters start with 1 the backslash character (\e). The special characters recognized by 1 'bc' are "a" (alert or bell), "b" (backspace), "f" (form feed), "n" 1 (newline), "r" (carriage return), "q" (double quote), "t" (tab), 1 and "\e" (backslash). Any other character following the backslash 1 will be ignored. 1 1 '{ STATEMENT_LIST }' 1 This is the compound statement. It allows multiple statements to 1 be grouped together for execution. 1 1 'if ( EXPRESSION ) STATEMENT1 [else STATEMENT2]' 1 The if statement evaluates the expression and executes statement1 1 or statement2 depending on the value of the expression. If the 1 expression is non-zero, statement1 is executed. If statement2 is 1 present and the value of the expression is 0, then statement2 is 1 executed. (The 'else' clause is an extension.) 1 1 'while ( EXPRESSION ) STATEMENT' 1 The while statement will execute the statement while the expression 1 is non-zero. It evaluates the expression before each execution of 1 the statement. Termination of the loop is caused by a zero 1 expression value or the execution of a 'break' statement. 1 1 'for ( [EXPRESSION1] ; [EXPRESSION2] ; [EXPRESSION3] ) STATEMENT' 1 The 'for' statement controls repeated execution of the statement. 1 EXPRESSION1 is evaluated before the loop. EXPRESSION2 is evaluated 1 before each execution of the statement. If it is non-zero, the 1 statement is evaluated. If it is zero, the loop is terminated. 1 After each execution of the statement, EXPRESSION3 is evaluated 1 before the reevaluation of expression2. If EXPRESSION1 or 1 EXPRESSION3 are missing, nothing is evaluated at the point they 1 would be evaluated. If EXPRESSION2 is missing, it is the same as 1 substituting the value 1 for EXPRESSION2. (The optional 1 expressions are an extension. POSIX 'bc' requires all three 1 expressions.) The following is equivalent code for the 'for' 1 statement: 1 1 expression1; 1 while (expression2) { 1 statement; 1 expression3; 1 } 1 1 'break' 1 This statement causes a forced exit of the most recent enclosing 1 'while' statement or 'for' statement. 1 1 'continue' 1 The 'continue' statement (an extension) causes the most recent 1 enclosing 'for' statement to start the next iteration. 1 1 'halt' 1 The 'halt' statement (an extension) is an executed statement that 1 causes the 'bc' processor to quit only when it is executed. For 1 example, "if (0 == 1) halt" will not cause 'bc' to terminate 1 because the 'halt' is not executed. 1 1 'return' 1 Return the value 0 from a function. (⇒Functions.) 1 1 'return ( EXPRESSION )' 11 Return the value of the expression from a function. (⇒ Functions.) As an extension, the parenthesis are not required. 1