3 Statements are very much like C statements. Most statements act
4 identically to those in C, but there are minor differences and
5 some additions. The following is a list of the statement types,
6 with explanation of the non-C statements.
8 Statements are generally terminated with semicolons or { ... }.
13 { statement; ... statement }
19 if (expr) statement else statement
20 for (optionalexpr ; optionalexpr ; optionalexpr) statement
21 while (expr) statement
22 do statement while (expr)
24 These all work like in normal C.
26 IMPORTANT NOTE: When statement is of the form { ... },
27 the leading { must be on the same line as the if, for,
30 This works as expected:
36 However this WILL NOT WORK AS EXPECTED:
43 because calc will parse the if being terminated by
44 an empty statement followed by a
51 In the same way, use these forms:
53 for (optionalexpr ; optionalexpr ; optionalexpr) {
65 where the initial { is on the SAME LINE as the if, while,
68 See 'help expression' for details on expressions.
69 See 'help builtin' for details on calc builtin functions.
70 See 'help unexpanded' for things C programmers do not expect.
71 See also 'help todo' and 'help bugs'.
79 These all work like in normal C.
81 See 'help expression' for details on expressions.
82 See 'help builtin' for details on calc builtin functions.
90 This returns a value from a function. Functions always
91 have a return value, even if this statement is not used.
92 If no return statement is executed, or if no expression
93 is specified in the return statement, then the return
94 value from the function is the null type.
99 switch (expr) { caseclauses }
100 Switch statements work similarly to C, except for the
101 following. A switch can be done on any type of value,
102 and the case statements can be of any type of values.
103 The case statements can also be expressions calculated
104 at runtime. The calculator compares the switch value
105 with each case statement in the order specified, and
106 selects the first case which matches. The default case
107 is the exception, and only matches once all other cases
113 mat variable [dimension] [dimension] ...
114 mat variable [dimension, dimension, ...]
115 mat variable [] = { value, ... }
116 This creates a matrix variable with the specified dimensions.
117 Matrices can have from 1 to 4 dimensions. When specifying
118 multiple dimensions, you can use either the standard C syntax,
119 or else you can use commas for separating the dimensions.
120 For example, the following two statements are equivalent,
121 and so will create the same two dimensional matrix:
126 By default, each dimension is indexed starting at zero,
127 as in normal C, and contains the specified number of
128 elements. However, this can be changed if a colon is
129 used to separate two values. If this is done, then the
130 two values become the lower and upper bounds for indexing.
131 This is convenient, for example, to create matrices whose
132 first row and column begin at 1. Examples of matrix
135 mat x[3] one dimension, bounds are 0-2
136 mat foo[4][5] two dimensions, bounds are 0-3 and 0-4
137 mat a[-7:7] one dimension, bounds are (-7)-7
138 mat s[1:9,1:9] two dimensions, bounds are 1-9 and 1-9
140 Note that the MAT statement is not a declaration, but is
141 executed at runtime. Within a function, the specified
142 variable must already be defined, and is just converted to
143 a matrix of the specified size, and all elements are set
144 to the value of zero. For convenience, at the top level
145 command level, the MAT command automatically defines a
146 global variable of the specified name if necessary.
148 Since the MAT statement is executed, the bounds on the
149 matrix can be full expressions, and so matrices can be
150 dynamically allocated. For example:
155 allocates a matrix which can be indexed from 0 to 39.
157 Initial values for the elements of a matrix can be specified
158 by following the bounds information with an equals sign and
159 then a list of values enclosed in a pair of braces. Even if
160 the matrix has more than one dimension, the elements must be
161 specified as a linear list. If too few values are specified,
162 the remaining values are set to zero. If too many values are
163 specified, a runtime error will result. Examples of some
166 mat table1[5] = {77, 44, 22};
167 mat table2[2,2] = {1, 2, 3, 4};
169 When an initialization is done, the bounds of the matrix
170 can optionally be left out of the square brackets, and the
171 correct bounds (zero based) will be set. This can only be
172 done for one-dimensional matrices. An example of this is:
174 mat fred[] = {99, 98, 97};
176 The MAT statement can also be used in declarations to set
177 variables as being matrices from the beginning. For example:
180 static mat strtable[] = {"hi", "there", "folks");
185 obj type { elementnames } optionalvariables
187 These create a new object type, or create one or more
188 variables of the specified type. For this calculator,
189 an object is just a structure which is implicitly acted
190 on by user defined routines. The user defined routines
191 implement common operations for the object, such as plus
192 and minus, multiply and divide, comparison and printing.
193 The calculator will automatically call these routines in
194 order to perform many operations.
196 To create an object type, the data elements used in
197 implementing the object are specified within a pair
198 of braces, separated with commas. For example, to
199 define an object will will represent points in 3-space,
200 whose elements are the three coordinate values, the
201 following could be used:
205 This defines an object type called point, whose elements
206 have the names x, y, and z. The elements are accessed
207 similarly to structure element accesses, by using a period.
208 For example, given a variable 'v' which is a point object,
209 the three coordinates of the point can be referenced by:
215 A particular object type can only be defined once, and
216 is global throughout all functions. However, different
217 object types can be used at the same time.
219 In order to create variables of an object type, they
220 can either be named after the right brace of the object
221 creation statement, or else can be defined later with
222 another obj statement. To create two points using the
223 second (and most common) method, the following is used:
227 This statement is executed, and is not a declaration.
228 Thus within a function, the variables p1 and p2 must have
229 been previously defined, and are just changed to be the
230 new object type. For convenience, at the top level command
231 level, object variables are automatically defined as being
232 global when necessary.
234 Initial values for an object can be specified by following
235 the variable name by an equals sign and a list of values
236 enclosed in a pair of braces. For example:
238 obj point pt = {5, 6};
240 The OBJ statement can also be used in declarations to set
241 variables as being objects from the beginning. If multiple
242 variables are specified, then each one is defined as the
243 specified object type. Examples of declarations are:
245 local obj point temp1;
246 static obj point temp2 = {4, 3};
247 global obj point p1, p2, p3;
255 For interactive expression evaluation, the values of all
256 typed-in expressions are automatically displayed to the
257 user. However, within a function or loop, the printing of
258 results must be done explicitly. This can be done using
259 the 'printf' or 'fprintf' functions, as in standard C, or
260 else by using the built-in 'print' statement. The advantage
261 of the print statement is that a format string is not needed.
262 Instead, the given values are simply printed with zero or one
263 spaces between each value.
265 Print accepts a list of expressions, separated either by
266 commas or colons. Each expression is evaluated in order
267 and printed, with no other output, except for the following
268 special cases. The comma which separates expressions prints
269 a single space, and a newline is printed after the last
270 expression unless the statement ends with a colon. As
273 print 3, 4; prints "3 4" and newline.
274 print 5:; prints "5" with no newline.
275 print 'a' : 'b' , 'c'; prints "ab c" and newline.
276 print; prints a newline.
278 For numeric values, the format of the number depends on the
279 current "mode" configuration parameter. The initial mode
280 is to print real numbers, but it can be changed to other
281 modes such as exponential, decimal fractions, or hex.
283 If a matrix or list is printed, then the elements contained
284 within the matrix or list will also be printed, up to the
285 maximum number specified by the "maxprint" configuration
286 parameter. If an element is also a matrix or a list, then
287 their values are not recursively printed. Objects are printed
288 using their user-defined routine. Printing a file value
289 prints the name of the file that was opened.
292 Also see the help topic:
294 help command top level commands
295 help expression calc expression syntax
296 help builtin calc builtin functions
297 help usage how to invoke the calc command and calc -options
299 You may obtain help on individual builtin functions. For example:
307 for a list of builtin functions.
309 Some calc operators have their own help pages:
323 ## Copyright (C) 1999-2007 Landon Curt Noll
325 ## Calc is open software; you can redistribute it and/or modify it under
326 ## the terms of the version 2.1 of the GNU Lesser General Public License
327 ## as published by the Free Software Foundation.
329 ## Calc is distributed in the hope that it will be useful, but WITHOUT
330 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
331 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
332 ## Public License for more details.
334 ## A copy of version 2.1 of the GNU Lesser General Public License is
335 ## distributed with calc under the filename COPYING-LGPL. You should have
336 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
337 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
339 ## @(#) $Revision: 30.1 $
340 ## @(#) $Id: statement,v 30.1 2007/03/16 11:10:42 chongo Exp $
341 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/statement,v $
343 ## Under source code control: 1991/07/21 04:37:23
344 ## File existed as early as: 1991
346 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
347 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/