home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / tcl_447.lzh / TCL / tcl.lzh / tcl / help / expressions < prev    next >
Text File  |  1990-05-03  |  4KB  |  94 lines

  1. EXPRESSIONS
  2.    A Tcl expression has C-like syntax and evaluates to an integer
  3.    result.  Expressions may contain integer values, variable
  4.    names in $ notation (the variables' values must be integer
  5.    strings), commands (embedded in brackets) that produce
  6.    integer string results, parentheses for grouping, and
  7.    operators.  Numeric values, whether they are passed directly
  8.    or through variable or command substitution, may be
  9.    specified either in decimal (the normal case), in octal (if
  10.    the first character of the value is 0), or in hexadecimal
  11.    (if the first two characters of the value are 0x).  The
  12.    valid operators are listed below, grouped in decreasing
  13.    order of precedence:
  14.  
  15.    -  ~ !       Unary minus, bit-wise NOT, logical NOT.
  16.  
  17.    *  / %       Multiply, divide, remainder.
  18.  
  19.    +  -        Add and subtract.
  20.  
  21.    <<  >>       Left and right shift.
  22.  
  23.    <  > <=  >=       Boolean less, greater, less than or
  24.          equal, and greater than or equal.  Each
  25.          operator produces 1 if the condition is
  26.          true, 0 otherwise.
  27.  
  28.    ==  !=       Boolean equal and not equal.  Each
  29.          operator produces a zero/one result.
  30.  
  31.    &        Bit-wise AND.
  32.  
  33.    ^        Bit-wise exclusive OR.
  34.  
  35.    |        Bit-wise OR.
  36.  
  37.    &&        Logical AND.  Produces a 1 result if
  38.          both operands are non-zero, 0 otherwise.
  39.  
  40.    ||        Logical OR.  Produces a 0 result if both
  41.          operands are zero, 1 otherwise.
  42.  
  43.    See the C manual for more details on the results produced by
  44.    each operator.  All of the binary operators group left-to-
  45.    right within the same precedence level.  For example, the
  46.    expression
  47.  
  48.    (4*2) < 7
  49.  
  50.    evaluates to 0.  Evaluating the expression string
  51.  
  52.    ($a + 3) < [set b]
  53.  
  54.    will cause the values of the variables a and b to be
  55.    examined;  the result will be 1 if b is greater than a by at
  56.    least 3;  otherwise the result will be 0.
  57.  
  58.    In general it is safest to enclose an expression in braces
  59.    when entering it in a command:  otherwise, if the expression
  60.    contains any white space then the Tcl interpreter will split
  61.    it among several arguments.  For example, the command
  62.  
  63.    expr $a + $b
  64.  
  65.    results in three arguments being passed to expr:  $a, +, and
  66.    $b.  In addition, if the expression isn't in braces then the
  67.    Tcl interpreter will perform variable and command
  68.    substitution immediately (it will happen in the command
  69.    parser rather than in the expression parser).  In many cases
  70.    the expression is being passed to a command that will
  71.    evaluate the expression later (or even many times if, for
  72.    example, the expression is to be used to decide when to exit
  73.    a loop).  Usually the desired goal is to re-do the variable
  74.    or command substitutions each time the expression is
  75.    evaluated, rather than once and for all at the beginning.
  76.    For example, the command
  77.  
  78.    for {set i 1} $i<=10 {set i [expr $i+1]} {...}
  79.  
  80.    is probably intended to iterate over all values of i from 1
  81.    to 10.  After each iteration of the body of the loop, for
  82.    will pass its second argument to the expression evaluator to
  83.    see whether or not to continue processing.  Unfortunately,
  84.    in this case the value of i in the second argument will be
  85.    substituted once and for all when the for command is parsed.
  86.    If i was 0 before the for command was invoked then for's
  87.    second argument will be 0<=10 which will always evaluate to
  88.    1, even though i's value eventually becomes greater than 10.
  89.    In the above case the loop will never terminate.  By placing
  90.    the expression in braces, the substitution of i's value will
  91.    be delayed;  it will be re-done each time the expression is
  92.    evaluated, which is probably the desired result.
  93.  
  94.