Tcl expr Command
last modified April 3, 2025
The Tcl expr
command evaluates expressions in Tcl. It handles arithmetic,
logical, and relational operations. The command returns the result of the evaluated
expression.
Basic Definition
The expr
command evaluates its arguments as a mathematical or logical
expression. It supports various operators and functions. The result is returned as
a string.
Syntax: expr arg ?arg ...?
. The arguments are concatenated and evaluated
as an expression. Braces {} are recommended for complex expressions.
Basic Arithmetic Operations
This example demonstrates basic arithmetic operations using the expr
command.
set sum [expr {10 + 20}] set diff [expr {50 - 30}] set product [expr {5 * 6}] set quotient [expr {100 / 4}] puts "Sum: $sum, Difference: $diff" puts "Product: $product, Quotient: $quotient"
This code performs four basic arithmetic operations. Each expr
evaluates
a simple expression. The results are stored in variables and printed.
Using Variables in Expressions
Variables can be used in expressions with proper substitution syntax.
set x 15 set y 3 set result1 [expr {$x + $y}] set result2 [expr {$x * $y}] set result3 [expr {$x ** $y}] ;# exponentiation puts "Results: $result1, $result2, $result3"
This shows how to use variables in expressions. The $ symbol performs variable substitution. The ** operator performs exponentiation (15 to the power of 3).
Mathematical Functions
Tcl's expr
supports various mathematical functions.
set angle [expr {3.14159 / 4}] ;# 45 degrees in radians set sine [expr {sin($angle)}] set cosine [expr {cos($angle)}] set sqrt [expr {sqrt(2)}] puts "Sine: $sine, Cosine: $cosine" puts "Square root of 2: $sqrt"
This demonstrates trigonometric functions and square root. The angle is converted to radians first. The results show the sine and cosine of 45 degrees.
Logical and Relational Operators
The expr
command supports logical and relational operations.
set a 10 set b 20 set comparison [expr {$a < $b}] set logical [expr {($a == 10) && ($b > 15)}] puts "Is a less than b? $comparison" puts "Logical test result: $logical"
This shows relational (<) and logical (&&) operators. The first expression checks if a is less than b. The second combines two conditions with logical AND.
Conditional Expressions
The ternary operator allows conditional expressions in expr
.
set score 85 set result [expr {$score >= 60 ? "Pass" : "Fail"}] puts "The student's result is: $result"
This uses the ternary operator to evaluate a condition. If score is 60 or higher, it returns "Pass", otherwise "Fail". The result is stored in the result variable.
Complex Expressions with Braces
Braces help create complex expressions and improve performance.
set a 5 set b 10 set c 2 set res [expr { ($a * $b) + ($b / $c) - (($a + $b) * ($b - $c)) / 2.0 }] puts "Complex expression result: $res"
This demonstrates a complex mathematical expression. Braces group operations and ensure proper evaluation order. The expression combines multiplication, division, addition, and subtraction.
Best Practices
- Braces: Always use braces {} around expressions.
- Variables: Prefix variables with $ in expressions.
- Precision: Use decimal points for floating-point division.
- Readability: Break complex expressions into parts.
- Functions: Use built-in math functions when available.
This tutorial covered the Tcl expr
command with practical examples
showing its usage in different scenarios.
Author
List all Tcl Tutorials.