BSD 3 development
[unix-history] / usr / doc / bc
CommitLineData
2074ceed
BJ
1.RP
2.TL
3BC \- An Arbitrary Precision Desk-Calculator Language
4.AU
5Lorinda Cherry
6.AU
7Robert Morris
8.AI
9.MH
10.AB
11BC is a language and a compiler for doing arbitrary precision arithmetic
12on the PDP-11 under the
13.UX
14time-sharing
15system. The output of the compiler is interpreted and executed by
16a collection of routines which can input, output, and do
17arithmetic on indefinitely large integers and on scaled fixed-point
18numbers.
19.PP
20These routines are themselves based on a dynamic storage allocator.
21Overflow does not occur until all available core storage
22is exhausted.
23.PP
24The language has a complete control structure as well as immediate-mode
25operation. Functions can be defined and saved for later execution.
26.PP
27Two five hundred-digit numbers can be multiplied to give a
28thousand digit result in about ten seconds.
29.PP
30A small collection of library functions is also available,
31including sin, cos, arctan, log, exponential, and Bessel functions of
32integer order.
33.PP
34Some of the uses of this compiler are
35.IP \-
36to do computation with large integers,
37.IP \-
38to do computation accurate to many decimal places,
39.IP \-
40conversion of numbers from one base to another base.
41.AE
42.PP
43.SH
44Introduction
45.PP
46BC is a language and a compiler for doing arbitrary precision
47arithmetic on the
48.UX
49time-sharing system [1].
50The compiler was written to make conveniently available a
51collection of routines (called DC [5]) which are capable of doing
52arithmetic on integers of arbitrary size. The compiler
53is by no means intended to provide a complete programming
54language.
55It is a minimal language facility.
56.PP
57There is a scaling provision that permits the
58use of decimal point notation.
59Provision is made for input and output in bases other than
60decimal. Numbers can be converted from decimal to octal by
61simply setting the output base to equal 8.
62.PP
63The actual limit on the number of digits that can
64be handled depends on the amount of storage available on the machine.
65Manipulation of numbers with many hundreds of digits
66is possible even on the smallest versions of
67.UX .
68.PP
69The syntax of BC has been deliberately selected to agree
70substantially with the C language [2]. Those who
71are familiar with C will find few surprises in this language.
72.SH
73Simple Computations with Integers
74.PP
75The simplest kind of statement is an arithmetic expression
76on a line by itself.
77For instance, if you type in the line:
78.DS
79142857 + 285714
80.DE
81the program responds immediately with the line
82.DS
83428571
84.DE
85The operators \-, *, /, %, and ^ can also be used; they
86indicate subtraction, multiplication, division, remaindering, and
87exponentiation, respectively. Division of integers produces an
88integer result truncated toward zero.
89Division by zero produces an error
90comment.
91.PP
92Any term in an expression may be prefixed by a minus sign to
93indicate that it is to be negated (the `unary' minus sign).
94The expression
95.DS
967+\-3
97.DE
98is interpreted to mean that \-3 is to be added to 7.
99.PP
100More complex expressions with several operators and with
101parentheses are interpreted just as in
102Fortran, with ^ having the greatest binding
103power, then * and % and /, and finally + and \-.
104Contents of parentheses are evaluated before material
105outside the parentheses.
106Exponentiations are
107performed from right to left and the other operators
108from left to right.
109The two expressions
110.DS
111a^b^c and a^(b^c)
112.DE
113are equivalent, as are the two expressions
114.DS
115a*b*c and (a*b)*c
116.DE
117BC shares with Fortran and C the undesirable convention that
118.DS
119a/b*c is equivalent to (a/b)*c
120.DE
121.PP
122Internal storage registers to hold numbers have single lower-case
123letter names. The value of an expression can be assigned to
124a register in the usual way. The statement
125.DS
126x = x + 3
127.DE
128has the effect of increasing by three the value of the contents of the
129register named x.
130When, as in this case, the outermost operator is an =, the
131assignment is performed but the result is not printed.
132Only 26 of these named storage registers are available.
133.PP
134There is a built-in square root function whose
135result is truncated to an integer (but see scaling below).
136The lines
137.DS
138x = sqrt(191)
139x
140.DE
141produce the printed result
142.DS
14313
144.DE
145.SH
146Bases
147.PP
148There are special internal quantities, called `ibase' and `obase'.
149The contents of `ibase', initially set to 10,
150determines the base used for interpreting numbers read in.
151For example, the lines
152.DS
153ibase = 8
15411
155.DE
156will produce the output line
157.DS
1589
159.DE
160and you are all set up to do octal to decimal conversions.
161Beware, however of trying to change the input base back
162to decimal by typing
163.DS
164ibase = 10
165.DE
166Because the number 10 is interpreted as octal, this statement will
167have no effect.
168For those who deal in hexadecimal notation,
169the characters A\-F are permitted in numbers
170(no matter what base is in effect)
171and are
172interpreted as digits having values 10\-15 respectively.
173The statement
174.DS
175ibase = A
176.DE
177will change you back to decimal input base no matter what the
178current input base is.
179Negative and large positive input bases are
180permitted but useless.
181No mechanism has been provided for the input of arbitrary
182numbers in bases less than 1 and greater than 16.
183.PP
184The contents of `obase', initially set to 10, are used as the base for output
185numbers. The lines
186.DS
187obase = 16
1881000
189.DE
190will produce the output line
191.DS
1923E8
193.DE
194which is to be interpreted as a 3-digit hexadecimal number.
195Very large output bases are permitted, and they are sometimes useful.
196For example, large numbers can be output in groups of five digits
197by setting `obase' to 100000.
198Strange (i.e. 1, 0, or negative) output bases are
199handled appropriately.
200.PP
201Very large numbers are split across lines with 70 characters per line.
202Lines which are continued end with \\.
203Decimal output conversion is practically instantaneous, but output
204of very large numbers (i.e., more than 100 digits) with other bases
205is rather slow.
206Non-decimal output conversion of
207a one hundred digit number takes about
208three seconds.
209.PP
210It is best to remember that `ibase' and `obase' have no effect
211whatever on the course of internal computation or
212on the evaluation of expressions, but only affect input and
213output conversion, respectively.
214.SH
215Scaling
216.PP
217A third special internal quantity called `scale' is
218used to determine the scale of calculated
219quantities.
220Numbers may have
221up to 99 decimal digits after the decimal point.
222This fractional part is retained in further computations.
223We refer to the number of digits after the decimal point of
224a number as its scale.
225.PP
226When two scaled numbers are combined by
227means of one of the arithmetic operations, the result
228has a scale determined by the following rules. For
229addition and subtraction, the scale of the result is the larger
230of the scales of the two operands. In this case,
231there is never any truncation of the result.
232For multiplications, the scale of the result is never
233less than the maximum of the two scales of the operands,
234never more than the sum of the scales of the operands
235and, subject to those two restrictions,
236the scale of the result is set equal to the contents of the internal
237quantity `scale'.
238The scale of a quotient is the contents of the internal
239quantity `scale'. The scale of a remainder is
240the sum of the scales of the quotient and the divisor.
241The result of an exponentiation is scaled as if
242the implied multiplications were performed.
243An exponent must be an integer.
244The scale of a square root is set to the maximum of the scale
245of the argument and the contents of `scale'.
246.PP
247All of the internal operations are actually carried out in terms
248of integers, with digits being discarded when necessary.
249In every case where digits are discarded, truncation and
250not rounding is performed.
251.PP
252The contents of
253`scale' must be no greater than
25499 and no less than 0. It is initially set to 0.
255In case you need more than 99 fraction digits, you may arrange
256your own scaling.
257.PP
258The internal quantities `scale', `ibase', and `obase' can be
259used in expressions just like other variables.
260The line
261.DS
262scale = scale + 1
263.DE
264increases the value of `scale' by one, and the line
265.DS
266scale
267.DE
268causes the current value of `scale' to be printed.
269.PP
270The value of `scale' retains its meaning as a
271number of decimal digits to be retained in internal
272computation even when `ibase' or `obase' are not equal to 10.
273The internal computations (which are still conducted in decimal,
274regardless of the bases) are performed to the specified number
275of decimal digits, never hexadecimal or octal or any
276other kind of digits.
277.SH
278Functions
279.PP
280The name of a function is a single lower-case letter.
281Function names are permitted to collide with simple
282variable names.
283Twenty-six different defined functions are permitted
284in addition to the twenty-six variable names.
285The line
286.DS
287 define a(x){
288.DE
289begins the definition of a function with one argument.
290This line must be followed by one or more statements,
291which make up the body of the function, ending
292with a right brace }.
293Return of control from a function occurs when a return
294statement is executed or when the end of the function is reached.
295The return statement can take either
296of the two forms
297.DS
298return
299return(x)
300.DE
301In the first case, the value of the function is 0, and in
302the second, the value of the expression in parentheses.
303.PP
304Variables used in the function can be declared as automatic
305by a statement of the form
306.DS
307auto x,y,z
308.DE
309There can be only one `auto' statement in a function and it must
310be the first statement in the definition.
311These automatic variables are allocated space and initialized
312to zero on entry to the function and thrown away on return. The
313values of any variables with the same names outside the function
314are not disturbed.
315Functions may be called recursively and the automatic variables
316at each level of call are protected.
317The parameters named in a function definition are treated in
318the same way as the automatic variables of that function
319with the single exception that they are given a value
320on entry to the function.
321An example of a function definition is
322.DS
323 define a(x,y){
324 auto z
325 z = x*y
326 return(z)
327 }
328.DE
329The value of this function, when called, will be the
330product of its
331two arguments.
332.PP
333A function is called by the appearance of its name
334followed by a string of arguments enclosed in
335parentheses and separated by commas.
336The result
337is unpredictable if the wrong number of arguments is used.
338.PP
339Functions with no arguments are defined and called using
340parentheses with nothing between them: b().
341.PP
342If the function
343.ft I
344a
345.ft
346above has been defined, then the line
347.DS
348a(7,3.14)
349.DE
350would cause the result 21.98 to be printed and the line
351.DS
352x = a(a(3,4),5)
353.DE
354would cause the value of x to become 60.
355.SH
356Subscripted Variables
357.PP
358A single lower-case letter variable name
359followed by an expression in brackets is called a subscripted
360variable (an array element).
361The variable name is called the array name and the expression
362in brackets is called the subscript.
363Only one-dimensional arrays are
364permitted. The names of arrays are permitted to
365collide with the names of simple variables and function names.
366Any fractional
367part of a subscript is discarded before use.
368Subscripts must be greater than or equal to zero and
369less than or equal to 2047.
370.PP
371Subscripted variables may be freely used in expressions, in
372function calls, and in return statements.
373.PP
374An array name may be used as an argument to a function,
375or may be declared as automatic in
376a function definition by the use of empty brackets:
377.DS
378f(a[\|])
379define f(a[\|])
380auto a[\|]
381.DE
382When an array name is so used, the whole contents of the array
383are copied for the use of the function, and thrown away on exit
384from the function.
385Array names which refer to whole arrays cannot be used
386in any other contexts.
387.SH
388Control Statements
389.PP
390The `if', the `while', and the `for' statements
391may be used to alter the flow within programs or to cause iteration.
392The range of each of them is a statement or
393a compound statement consisting of a collection of
394statements enclosed in braces.
395They are written in the following way
396.DS
397if(relation) statement
398while(relation) statement
399for(expression1; relation; expression2) statement
400.DE
401or
402.DS
403if(relation) {statements}
404while(relation) {statements}
405for(expression1; relation; expression2) {statements}
406.DE
407.PP
408A relation in one of the control statements is an expression of the form
409.DS
410x>y
411.DE
412where two expressions are related by one of the six relational
413operators <, >, <=, >=, ==, or !=.
414The relation ==
415stands for `equal to' and != stands for `not equal to'.
416The meaning of the remaining relational operators is
417clear.
418.PP
419BEWARE of using = instead of == in a relational. Unfortunately,
420both of them are legal, so you will not get a diagnostic
421message, but = really will not do a comparison.
422.PP
423The `if' statement causes execution of its range
424if and only if the relation is true.
425Then control passes to the next statement in sequence.
426.PP
427The `while' statement causes execution of its range
428repeatedly as long as the relation
429is true. The relation is tested before each execution
430of its range and if the relation
431is false, control passes to the next statement beyond the range
432of the while.
433.PP
434The `for' statement begins
435by executing `expression1'. Then the relation is tested
436and, if true, the statements in the range of the `for' are executed.
437Then `expression2' is executed. The relation is tested, and so on.
438The typical use of the `for' statement is for a controlled iteration,
439as in the statement
440.DS
441for(i=1; i<=10; i=i+1) i
442.DE
443which will print the integers from 1 to 10.
444Here are some examples of the use of the control statements.
445.DS
446define f(n){
447auto i, x
448x=1
449for(i=1; i<=n; i=i+1) x=x*i
450return(x)
451}
452.DE
453The line
454.DS
455 f(a)
456.DE
457will print
458.ft I
459a
460.ft
461factorial if
462.ft I
463a
464.ft
465is a positive integer.
466Here is the definition of a function which will
467compute values of the binomial coefficient
468(m and n are assumed to be positive integers).
469.DS
470define b(n,m){
471auto x, j
472x=1
473for(j=1; j<=m; j=j+1) x=x*(n\-j+1)/j
474return(x)
475}
476.DE
477The following function computes values of the exponential function
478by summing the appropriate series
479without regard for possible truncation errors:
480.DS
481scale = 20
482define e(x){
483 auto a, b, c, d, n
484 a = 1
485 b = 1
486 c = 1
487 d = 0
488 n = 1
489 while(1==1){
490 a = a*x
491 b = b*n
492 c = c + a/b
493 n = n + 1
494 if(c==d) return(c)
495 d = c
496 }
497}
498.DE
499.SH
500Some Details
501.PP
502There are some language features that every user should know
503about even if he will not use them.
504.PP
505Normally statements are typed one to a line. It is also permissible
506to type several statements on a line separated by semicolons.
507.PP
508If an assignment statement is parenthesized, it then has
509a value and it can be used anywhere that an expression can.
510For example, the line
511.DS
512(x=y+17)
513.DE
514not only makes the indicated assignment, but also prints the
515resulting value.
516.PP
517Here is an example of a use of the value of an
518assignment statement even when it is not parenthesized.
519.DS
520x = a[i=i+1]
521.DE
522causes a value to be assigned to x and also increments i
523before it is used as a subscript.
524.PP
525The following constructs work in BC in exactly the same manner
526as they do in the C language. Consult the appendix or the
527C manuals [2] for their exact workings.
528.DS
529.ta 2i
530x=y=z is the same as x=(y=z)
531x =+ y x = x+y
532x =\- y x = x\-y
533x =* y x = x*y
534x =/ y x = x/y
535x =% y x = x%y
536x =^ y x = x^y
537x++ (x=x+1)\-1
538x\-\- (x=x\-1)+1
539++x x = x+1
540\-\-x x = x\-1
541.DE
542Even if you don't intend to use the constructs,
543if you type one inadvertently, something correct but unexpected
544may happen.
545.PP
546WARNING! In some of these constructions, spaces are
547significant.
548There is a real difference between
549x=\-y and x= \-y.
550The first replaces x by x\-y and the second by \-y.
551.SH
552Three Important Things
553.PP
5541. To exit a BC program, type `quit'.
555.PP
5562. There is a comment convention identical to that of C and
557of PL/I. Comments begin with `/*' and end with `*/'.
558.PP
5593. There is a library of math functions which may be obtained by
560typing at command level
561.DS
562bc \-l
563.DE
564This command will load a set of library functions
565which, at the time of writing, consists of sine (named `s'),
566cosine (`c'), arctangent (`a'), natural logarithm (`l'),
567exponential (`e') and Bessel functions of integer order (`j(n,x)'). Doubtless more functions will be added
568in time.
569The library sets the scale to 20. You can reset it to something
570else if you like.
571The design of these mathematical library routines
572is discussed elsewhere [3].
573.PP
574If you type
575.DS
576bc file ...
577.DE
578BC will read and execute the named file or files before accepting
579commands from the keyboard. In this way, you may load your
580favorite programs and function definitions.
581.SH
582Acknowledgement
583.PP
584The compiler is written in YACC [4]; its original
585version was written by S. C. Johnson.
586.SH
587References
588.IP [1]
589K. Thompson and D. M. Ritchie,
590.ft I
591UNIX Programmer's Manual,
592.ft
593Bell Laboratories,
5941978.
595.IP [2]
596B. W. Kernighan and
597D. M. Ritchie,
598.ft I
599The C Programming Language,
600.ft
601Prentice-Hall, 1978.
602.IP [3]
603R. Morris,
604.ft I
605A Library of Reference Standard Mathematical Subroutines,
606.ft
607Bell Laboratories internal memorandum, 1975.
608.IP [4]
609S. C. Johnson,
610.ft I
611YACC \(em Yet Another Compiler-Compiler.
612.ft
613Bell Laboratories Computing Science Technical Report #32, 1978.
614.IP [5]
615R. Morris and L. L. Cherry,
616.ft I
617DC \- An Interactive Desk Calculator.
618.ft
619.LP
620.bp
621.ft B
622.DS C
623Appendix
624.DE
625.ft
626.NH
627Notation
628.PP
629In the following pages syntactic categories are in \fIitalics\fP;
630literals are in \fBbold\fP; material in brackets [\|] is optional.
631.NH
632Tokens
633.PP
634Tokens consist of keywords, identifiers, constants, operators,
635and separators.
636Token separators may be blanks, tabs or comments.
637Newline characters or semicolons separate statements.
638.NH 2
639Comments
640.PP
641Comments are introduced by the characters /* and terminated by
642*/.
643.NH 2
644Identifiers
645.PP
646There are three kinds of identifiers \- ordinary identifiers, array identifiers
647and function identifiers.
648All three types consist of single lower-case letters.
649Array identifiers are followed by square brackets, possibly
650enclosing an expression describing a subscript.
651Arrays are singly dimensioned and may contain up to 2048
652elements.
653Indexing begins at zero so an array may be indexed from 0 to 2047.
654Subscripts are truncated to integers.
655Function identifiers are followed by parentheses, possibly enclosing arguments.
656The three types of identifiers do not conflict;
657a program can have a variable named \fBx\fP,
658an array named \fBx\fP and a function named \fBx\fP, all of which are separate and
659distinct.
660.NH 2
661Keywords
662.PP
663The following are reserved keywords:
664.ft B
665.ta .5i 1.0i
666.nf
667 ibase if
668 obase break
669 scale define
670 sqrt auto
671 length return
672 while quit
673 for
674.fi
675.ft
676.NH 2
677Constants
678.PP
679Constants consist of arbitrarily long numbers
680with an optional decimal point.
681The hexadecimal digits \fBA\fP\-\fBF\fP are also recognized as digits with
682values 10\-15, respectively.
683.NH 1
684Expressions
685.PP
686The value of an expression is printed unless the main
687operator is an assignment.
688Precedence is the same as the order
689of presentation here, with highest appearing first.
690Left or right associativity, where applicable, is
691discussed with each operator.
692.bp
693.NH 2
694Primitive expressions
695.NH 3
696Named expressions
697.PP
698Named expressions are
699places where values are stored.
700Simply stated,
701named expressions are legal on the left
702side of an assignment.
703The value of a named expression is the value stored in the place named.
704.NH 4
705\fIidentifiers\fR
706.PP
707Simple identifiers are named expressions.
708They have an initial value of zero.
709.NH 4
710\fIarray-name\fP\|[\|\fIexpression\fP\|]
711.PP
712Array elements are named expressions.
713They have an initial value of zero.
714.NH 4
715\fBscale\fR, \fBibase\fR and \fBobase\fR
716.PP
717The internal registers
718\fBscale\fP, \fBibase\fP and \fBobase\fP are all named expressions.
719\fBscale\fP is the number of digits after the decimal point to be
720retained in arithmetic operations.
721\fBscale\fR has an initial value of zero.
722\fBibase\fP and \fBobase\fP are the input and output number
723radix respectively.
724Both \fBibase\fR and \fBobase\fR have initial values of 10.
725.NH 3
726Function calls
727.NH 4
728\fIfunction-name\fB\|(\fR[\fIexpression\fR\|[\fB,\|\fIexpression\|\fR.\|.\|.\|]\|]\fB)
729.PP
730A function call consists of a function name followed by parentheses
731containing a comma-separated list of
732expressions, which are the function arguments.
733A whole array passed as an argument is specified by the
734array name followed by empty square brackets.
735All function arguments are passed by
736value.
737As a result, changes made to the formal parameters have
738no effect on the actual arguments.
739If the function terminates by executing a return
740statement, the value of the function is
741the value of the expression in the parentheses of the return
742statement or is zero if no expression is provided
743or if there is no return statement.
744.NH 4
745sqrt\|(\|\fIexpression\fP\|)
746.PP
747The result is the square root of the expression.
748The result is truncated in the least significant decimal place.
749The scale of the result is
750the scale of the expression or the
751value of
752.ft B
753scale,
754.ft
755whichever is larger.
756.NH 4
757length\|(\|\fIexpression\fP\|)
758.PP
759The result is the total number of significant decimal digits in the expression.
760The scale of the result is zero.
761.NH 4
762scale\|(\|\fIexpression\fP\|)
763.PP
764The result is the scale of the expression.
765The scale of the result is zero.
766.NH 3
767Constants
768.PP
769Constants are primitive expressions.
770.NH 3
771Parentheses
772.PP
773An expression surrounded by parentheses is
774a primitive expression.
775The parentheses are used to alter the
776normal precedence.
777.NH 2
778Unary operators
779.PP
780The unary operators
781bind right to left.
782.NH 3
783\-\|\fIexpression\fP
784.PP
785The result is the negative of the expression.
786.NH 3
787++\|\fInamed-expression\fP
788.PP
789The named expression is
790incremented by one.
791The result is the value of the named expression after
792incrementing.
793.NH 3
794\-\-\|\fInamed-expression\fP
795.PP
796The named expression is
797decremented by one.
798The result is the value of the named expression after
799decrementing.
800.NH 3
801\fInamed-expression\fP\|++
802.PP
803The named expression is
804incremented by one.
805The result is the value of the named expression before
806incrementing.
807.NH 3
808\fInamed-expression\fP\|\-\-
809.PP
810The named expression is
811decremented by one.
812The result is the value of the named expression before
813decrementing.
814.NH 2
815Exponentiation operator
816.PP
817The exponentiation operator binds right to left.
818.NH 3
819\fIexpression\fP ^ \fIexpression\fP
820.PP
821The result is the first
822expression raised to the power of the
823second expression.
824The second expression must be an integer.
825If \fIa\fP
826is the scale of the left expression
827and \fIb\fP is the absolute value
828of the right expression,
829then the scale of the result is:
830.PP
831min\|(\|\fIa\(mub\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP\|)\|)
832.NH 2
833Multiplicative operators
834.PP
835The operators *, /, % bind left to right.
836.NH 3
837\fIexpression\fP * \fIexpression\fP
838.PP
839The result is the product
840of the two expressions.
841If \fIa\fP and \fIb\fP are the
842scales of the two expressions,
843then the scale of the result is:
844.PP
845min\|(\|\fIa+b\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP,\|\fIb\fP\|)\|)
846.NH 3
847\fIexpression\fP / \fIexpression\fP
848.PP
849The result is the quotient of the two expressions.
850The scale of the result is the value of \fBscale\fR.
851.NH 3
852\fIexpression\fP % \fIexpression\fP
853.PP
854The % operator produces the remainder of the division
855of the two expressions.
856More precisely,
857\fIa\fP%\fIb\fP is \fIa\fP\-\fIa\fP/\fIb\fP*\fIb\fP.
858.PP
859The scale of the result is the sum of the scale of
860the divisor and the value of
861.ft B
862scale
863.ft
864.NH 2
865Additive operators
866.PP
867The additive operators bind left to right.
868.NH 3
869\fIexpression\fP + \fIexpression\fP
870.PP
871The result is the sum of the two expressions.
872The scale of the result is
873the maximun of the scales of the expressions.
874.NH 3
875\fIexpression\fP \- \fIexpression\fP
876.PP
877The result is the difference of the two expressions.
878The scale of the result is the
879maximum of the scales of the expressions.
880.NH 2
881assignment operators
882.PP
883The assignment operators bind right to left.
884.NH 3
885\fInamed-expression\fP = \fIexpression\fP
886.PP
887This expression results in assigning the value of the expression
888on the right
889to the named expression on the left.
890.NH 3
891\fInamed-expression\fP =+ \fIexpression\fP
892.NH 3
893\fInamed-expression\fP =\- \fIexpression\fP
894.NH 3
895\fInamed-expression\fP =* \fIexpression\fP
896.NH 3
897\fInamed-expression\fP =/ \fIexpression\fP
898.NH 3
899\fInamed-expression\fP =% \fIexpression\fP
900.NH 3
901\fInamed-expression\fP =^ \fIexpression\fP
902.PP
903The result of the above expressions is equivalent
904to ``named expression = named expression OP expression'',
905where OP is the operator after the = sign.
906.NH 1
907Relations
908.PP
909Unlike all other operators, the relational operators
910are only valid as the object of an \fBif\fP, \fBwhile\fP,
911or inside a \fBfor\fP statement.
912.NH 2
913\fIexpression\fP < \fIexpression\fP
914.NH 2
915\fIexpression\fP > \fIexpression\fP
916.NH 2
917\fIexpression\fP <= \fIexpression\fP
918.NH 2
919\fIexpression\fP >= \fIexpression\fP
920.NH 2
921\fIexpression\fP == \fIexpression\fP
922.NH 2
923\fIexpression\fP != \fIexpression\fP
924.NH 1
925Storage classes
926.PP
927There are only two storage classes in BC, global and automatic
928(local).
929Only identifiers that are to be local to a function need be
930declared with the \fBauto\fP command.
931The arguments to a function
932are local to the function.
933All other identifiers are assumed to be global
934and available to all functions.
935All identifiers, global and local, have initial values
936of zero.
937Identifiers declared as \fBauto\fP are allocated on entry to the function
938and released on returning from the function.
939They therefore do not retain values between function calls.
940\fBauto\fP arrays are specified by the array name followed by empty square brackets.
941.PP
942Automatic variables in BC do not work in exactly the same way
943as in either C or PL/I. On entry to a function, the old values of
944the names that appear as parameters and as automatic
945variables are pushed onto a stack.
946Until return is made from the function, reference to these
947names refers only to the new values.
948.NH 1
949Statements
950.PP
951Statements must be separated by semicolon or newline.
952Except where altered by control statements, execution
953is sequential.
954.NH 2
955Expression statements
956.PP
957When a statement is an expression, unless
958the main operator is an assignment, the value
959of the expression is printed, followed by a newline character.
960.NH 2
961Compound statements
962.PP
963Statements may be grouped together and used when one statement is expected
964by surrounding them with { }.
965.NH 2
966Quoted string statements
967.PP
968"any string"
969.sp .5
970This statement prints the string inside the quotes.
971.NH 2
972If statements
973.sp .5
974\fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fR
975.PP
976The substatement is executed if the relation is true.
977.NH 2
978While statements
979.sp .5
980\fBwhile\|(\|\fIrelation\fB\|)\|\fIstatement\fR
981.PP
982The statement is executed while the relation
983is true.
984The test occurs before each execution of the statement.
985.NH 2
986For statements
987.sp .5
988\fBfor\|(\|\fIexpression\fB; \fIrelation\fB; \fIexpression\fB\|)\|\fIstatement\fR
989.PP
990The for statement is the same as
991.nf
992.ft I
993 first-expression
994 \fBwhile\|(\fPrelation\|\fB) {\fP
995 statement
996 last-expression
997 }
998.ft R
999.fi
1000.PP
1001All three expressions must be present.
1002.NH 2
1003Break statements
1004.sp .5
1005\fBbreak\fP
1006.PP
1007\fBbreak\fP causes termination of a \fBfor\fP or \fBwhile\fP statement.
1008.NH 2
1009Auto statements
1010.sp .5
1011\fBauto \fIidentifier\fR\|[\|\fB,\fIidentifier\fR\|]
1012.PP
1013The auto statement causes the values of the identifiers to be pushed down.
1014The identifiers can be ordinary identifiers or array identifiers.
1015Array identifiers are specified by following the array name by empty square
1016brackets.
1017The auto statement must be the first statement
1018in a function definition.
1019.NH 2
1020Define statements
1021.sp .5
1022.nf
1023\fBdefine(\|\fR[\fIparameter\|\fR[\fB\|,\|\fIparameter\|.\|.\|.\|\fR]\|]\|\fB)\|{\fI
1024 statements\|\fB}\fR
1025.fi
1026.PP
1027The define statement defines a function.
1028The parameters may
1029be ordinary identifiers or array names.
1030Array names must be followed by empty square brackets.
1031.NH 2
1032Return statements
1033.sp .5
1034\fBreturn\fP
1035.sp .5
1036\fBreturn(\fI\|expression\|\fB)\fR
1037.PP
1038The return statement causes termination of a function,
1039popping of its auto variables, and
1040specifies the result of the function.
1041The first form is equivalent to \fBreturn(0)\fR.
1042The result of the function is the result of the expression
1043in parentheses.
1044.NH 2
1045Quit
1046.PP
1047The quit statement stops execution of a BC program and returns
1048control to UNIX when it is first encountered.
1049Because it is not treated as an executable statement,
1050it cannot be used
1051in a function definition or in an
1052.ft B
1053if, for,
1054.ft
1055or
1056.ft B
1057while
1058.ft
1059statement.