运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 Tcl语言具有丰富的内置运算符,并提供以下类型的运算符-
本章将逐一说明算术,关系,逻辑,按位和三元运算符。
下表显示了Tcl语言支持的所有算术运算符。假设变量A=10,变量 B=20,则-
Operator | Remark | Example |
---|---|---|
+ | 加 | 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
下表显示了Tcl语言支持的所有关系运算符。假设变量 A=10,变量 B=20,则-
链接:https://www.learnfk.comhttps://www.learnfk.com/tcl-tk/tcl-operators.html
来源:LearnFk无涯教程网
Operator | Remark | Example |
---|---|---|
== | 相等 | (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. |
尝试以下示例以了解Tcl语言中可用的所有关系运算符-
#!/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
下表显示了Tcl语言支持的所有逻辑运算符。假设变量 A=1,变量 B=0,则-
Operator | Remark | Example |
---|---|---|
&& | 逻辑and | (A && B) is false. |
|| | 逻辑 or | (A || B) is true. |
! | 逻辑非 | !(A && B) is true. |
尝试以下示例以了解Tcl语言中可用的所有逻辑运算符-
#!/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
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
假设A=60;和B=13;现在以二进制格式,它们将如下所示-
A=0011 1100 B=0000 1101 ---------------------- A&B=0000 1100 A | B=0011 1101 A ^ B=0011 0001
下表列出了Tcl语言支持的按位运算符。假设变量 A 保持60,变量 B 保持13,则-
Operator | Remark | Example |
---|---|---|
& | 按位 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 | Remark | Example |
---|---|---|
? : | 三元组 | 如果条件为真?然后取值X:否则取值Y |
尝试以下示例以了解Tcl语言中可用的三元运算符-
#!/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
运算符优先级确定表达式中术语的分组。
Category | Operator | Associativity |
---|---|---|
Unary | + - | Right to left |
Multiplicative | */% | Left to right |
Additive | + - | 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 |
尝试以下示例以了解Tcl语言中可用的运算符优先级-
#!/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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Python实战 · 从0到1搭建直播视频平台 -〔Barry〕