# Tcl - 运算符

• 算术运算符
• 关系运算符
• 逻辑运算符
• 按位运算符
• 三元运算符

## 算术运算符

OperatorRemarkExample
+A + B=30
-A - B=-10
*A * B=200
/B/A=2
%取模B % A=0
```#!/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```

## 关系运算符

OperatorRemarkExample
==相等(A == B) is not true.
!=不相等(A != B) is true.
>大于(A > B) is not true.
<小于(A < B) is true.
>=大于或等于(A >= B) is not true.
<=小于或等于(A <= B) is true.

```#!/usr/bin/tclsh

set a 21
set b 10

if { \$a == \$b } {
puts "Line 1 - a is equal to b\n"
} else {
puts "Line 1 - a is not equal to b\n"
}
if { \$a < \$b } {
puts "Line 2 - a is less than b\n"
} else {
puts "Line 2 - a is not less than b\n"
}
if { \$a > \$b } {
puts "Line 3 - a is greater than b\n"
} else {
puts "Line 3 - a is not greater than b\n"
}
# Lets change value of a and b
set a 5
set b 20
if { \$a <= \$b } {
puts "Line 4 - a is either less than or equal to  b\n"
}
if { \$b >= \$a } {
puts "Line 5 - b is either greater than  or equal to a\n"
}```

```Line 1 - a is not equal to b

Line 2 - a is not less than b

Line 3 - a is greater than b

Line 4 - a is either less than or equal to  b

Line 5 - b is either greater than  or equal to a```

## 逻辑运算符

OperatorRemarkExample
&&逻辑and(A && B) is false.
||逻辑 or (A || B) is true.
!逻辑非!(A && B) is true.

```#!/usr/bin/tclsh

set a  5
set b 20

if { \$a && \$b } {
puts "Line 1 - Condition is true\n"
}
if { \$a || \$b } {
puts "Line 2 - Condition is true\n"
}
# lets change the value of  a and b
set a  0
set b 10
if { \$a && \$b } {
puts "Line 3 - Condition is true\n"
} else {
puts "Line 3 - Condition is not true\n"
}
if { !(\$a && \$b) } {
puts "Line 4 - Condition is true\n"
}```

```Line 1 - Condition is true

Line 2 - Condition is true

Line 3 - Condition is not true

Line 4 - Condition is true```

## 按位运算符

pqp & qp | qp ^ q
00000
01011
11110
10011

```A=0011 1100

B=0000 1101

----------------------

A&B=0000 1100

A | B=0011 1101

A ^ B=0011 0001```

OperatorRemarkExample
&按位 and(A & B)=12, which is 0000 1100
|按位 or(A | B)=61, which is 0011 1101
^按位 异域(A ^ B)=49, which is 0011 0001
<<左移A << 2 =240, which is 1111 0000
>>右移A >> 2 =15, which is 0000 1111
```#!/usr/bin/tclsh

set a 60  ;# 60=0011 1100
set b 13  ;# 13=0000 1101

set c [expr \$a & \$b] ;# 12=0000 1100
puts "Line 1 - Value of c is \$c\n"

set c [expr \$a | \$b;] ;# 61=0011 1101
puts "Line 2 - Value of c is \$c\n"

set c [expr \$a ^ \$b;] ;# 49=0011 0001
puts "Line 3 - Value of c is \$c\n"

set c [expr \$a << 2] ;# 240=1111 0000
puts "Line 4 - Value of c is \$c\n"

set c [expr \$a >> 2] ;# 15=0000 1111
puts "Line 5 - Value of c is \$c\n"```

```Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is 240

Line 5 - Value of c is 15```

## 三元运算符

Operator
RemarkExample
? :三元组如果条件为真?然后取值X:否则取值Y

```#!/usr/bin/tclsh

set a 10;
set b [expr \$a == 1 ? 20: 30]
puts "Value of b is \$b\n"
set b [expr \$a == 10 ? 20: 30]
puts "Value of b is \$b\n" ```

```Value of b is 30

Value of b is 20```

## Tcl优先级

CategoryOperatorAssociativity
Unary+ -Right to left
Multiplicative*/%Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Ternary?:Right to left

```#!/usr/bin/tclsh

set a 20
set b 10
set c 15
set d 5

set  e [expr [expr \$a + \$b] * \$c/\$d ]     ;# ( 30 * 15 )/5
puts "Value of (a + b) * c/d is : \$e\n"

set  e [expr  [expr [expr \$a + \$b] * \$c]/\$d]   ;#  (30 * 15 )/5]
puts "Value of ((a + b) * c)/d is  : \$e\n"

set  e  [expr [expr \$a + \$b] * [expr \$c/\$d] ]   ;# (30) * (15/5)
puts "Value of (a + b) * (c/d) is  : \$e\n"

set  e  [expr \$a + [expr \$b * \$c ]/\$d ] ;#  20 + (150/5)
puts "Value of a + (b * c)/d is  :  \$e\n" ```

```Value of (a + b) * c/d is : 90

Value of ((a + b) * c)/d is  : 90

Value of (a + b) * (c/d) is  : 90

Value of a + (b * c)/d is  :  50```

## 相关推荐

Java性能调优实战 -〔刘超 - 〕