4. Expressions

Expressions combine values (or operands) with operators to produce new values. For example, the expression 4+2 contains two operands, 4 and 2, and one operator, +; it evaluates to 6. Many Tcl commands expect one or more of their arguments to be expressions. The simplest such command is expr, which just evaluates its arguments as an expression and returns the result as a string:

    expr (8+4) * 6.2 ⇒ 74.4

Another example is if, which evaluates its first argument as an expression and uses the result to determine whether or not to evaluate its second argument as a Tcl script:

if {$x < 2} {set x 2}

This chapter uses the expr command for all of its examples, but the same syntax, substitution, and evaluation rules apply to all other ...

Get Tcl and the Tk Toolkit, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.