TCL算術運算子


下表列出了所有Tcl語言支援的算術運算子。假設變數A=10,變數B=20,則:

運算子 描述 例子
+ 兩個數相加 A + B = 30
- 第一個運算元減去第二個運算元 A - B = -10
* 兩個運算元相乘 A * B = 200
/ 分子(B)除以分母(A) B / A = 2
% 模運算和整數除法後的餘數 B % A = 0

例子

試試下面的例子就明白了所有Tcl語言中可用的算術運算子:

#!/usr/bin/tclsh

set a 21
set b 10
set c [expr $a + $b]
puts "Line 1 - Value of c is $c\n"
set c [expr $a - $b]
puts "Line 2 - Value of c is $c\n"
set c [expr $a * $b]
puts "Line 3 - Value of c is $c\n"
set c [expr $a / $b]
puts "Line 4 - Value of c is $c\n"
set c [expr $a % $b]
puts "Line 5 - Value of c is $c\n"

當編譯和執行上面的程式,它會產生以下結果:

Line 1 - Value of c is 31

Line 2 - Value of c is 11

Line 3 - Value of c is 210

Line 4 - Value of c is 2

Line 5 - Value of c is 1