home *** CD-ROM | disk | FTP | other *** search
- EXPRESSIONS
- A Tcl expression has C-like syntax and evaluates to an integer
- result. Expressions may contain integer values, variable
- names in $ notation (the variables' values must be integer
- strings), commands (embedded in brackets) that produce
- integer string results, parentheses for grouping, and
- operators. Numeric values, whether they are passed directly
- or through variable or command substitution, may be
- specified either in decimal (the normal case), in octal (if
- the first character of the value is 0), or in hexadecimal
- (if the first two characters of the value are 0x). The
- valid operators are listed below, grouped in decreasing
- order of precedence:
-
- - ~ ! Unary minus, bit-wise NOT, logical NOT.
-
- * / % Multiply, divide, remainder.
-
- + - Add and subtract.
-
- << >> Left and right shift.
-
- < > <= >= Boolean less, greater, less than or
- equal, and greater than or equal. Each
- operator produces 1 if the condition is
- true, 0 otherwise.
-
- == != Boolean equal and not equal. Each
- operator produces a zero/one result.
-
- & Bit-wise AND.
-
- ^ Bit-wise exclusive OR.
-
- | Bit-wise OR.
-
- && Logical AND. Produces a 1 result if
- both operands are non-zero, 0 otherwise.
-
- || Logical OR. Produces a 0 result if both
- operands are zero, 1 otherwise.
-
- See the C manual for more details on the results produced by
- each operator. All of the binary operators group left-to-
- right within the same precedence level. For example, the
- expression
-
- (4*2) < 7
-
- evaluates to 0. Evaluating the expression string
-
- ($a + 3) < [set b]
-
- will cause the values of the variables a and b to be
- examined; the result will be 1 if b is greater than a by at
- least 3; otherwise the result will be 0.
-
- In general it is safest to enclose an expression in braces
- when entering it in a command: otherwise, if the expression
- contains any white space then the Tcl interpreter will split
- it among several arguments. For example, the command
-
- expr $a + $b
-
- results in three arguments being passed to expr: $a, +, and
- $b. In addition, if the expression isn't in braces then the
- Tcl interpreter will perform variable and command
- substitution immediately (it will happen in the command
- parser rather than in the expression parser). In many cases
- the expression is being passed to a command that will
- evaluate the expression later (or even many times if, for
- example, the expression is to be used to decide when to exit
- a loop). Usually the desired goal is to re-do the variable
- or command substitutions each time the expression is
- evaluated, rather than once and for all at the beginning.
- For example, the command
-
- for {set i 1} $i<=10 {set i [expr $i+1]} {...}
-
- is probably intended to iterate over all values of i from 1
- to 10. After each iteration of the body of the loop, for
- will pass its second argument to the expression evaluator to
- see whether or not to continue processing. Unfortunately,
- in this case the value of i in the second argument will be
- substituted once and for all when the for command is parsed.
- If i was 0 before the for command was invoked then for's
- second argument will be 0<=10 which will always evaluate to
- 1, even though i's value eventually becomes greater than 10.
- In the above case the loop will never terminate. By placing
- the expression in braces, the substitution of i's value will
- be delayed; it will be re-done each time the expression is
- evaluated, which is probably the desired result.
-
-