Você está na página 1de 4

9/23/2016

TclOperators

TclOperators
https://www.tutorialspoint.com/tcltk/tcl_operators.htm
Copyrighttutorialspoint.com
Anoperatorisasymbolthattellsthecompilertoperformspecificmathematicalorlogicalmanipulations.Tcl
languageisrichinbuiltinoperatorsandprovidesthefollowingtypesofoperators
ArithmeticOperators
RelationalOperators
LogicalOperators
BitwiseOperators
TernaryOperator

Thischapterwillexplainthearithmetic,relational,logical,bitwise,andternaryoperatorsonebyone.

ArithmeticOperators
FollowingtableshowsallthearithmeticoperatorssupportedbyTcllanguage.AssumevariableAholds10and
variableBholds20,then
ShowExamples
Operator
+

*
/

Description
Addstwooperands
Subtractssecondoperandfromthefirst
Multipliesbothoperands
Dividesnumeratorbydenumerator

https://www.tutorialspoint.com/cgibin/printpage.cgi

Example
A+Bwillgive30
ABwillgive10
A*Bwillgive200
B/Awillgive2
1/4

9/23/2016

TclOperators

ModulusOperatorandremainderofafteraninteger
division

B%Awillgive0

RelationalOperators
FollowingtableshowsalltherelationaloperatorssupportedbyTcllanguage.AssumevariableAholds10and
variableBholds20,then
ShowExamples
Operator
==
!=
>
<
>=
<=

Description
Checksifthevaluesoftwooperandsareequalornot,ifyes
thenconditionbecomestrue.
Checksifthevaluesoftwooperandsareequalornot,if
valuesarenotequalthenconditionbecomestrue.
Checksifthevalueofleftoperandisgreaterthanthevalueof
rightoperand,ifyesthenconditionbecomestrue.
Checksifthevalueofleftoperandislessthanthevalueof
rightoperand,ifyesthenconditionbecomestrue.
Checksifthevalueofleftoperandisgreaterthanorequalto
thevalueofrightoperand,ifyesthenconditionbecomes
true.
Checksifthevalueofleftoperandislessthanorequaltothe
valueofrightoperand,ifyesthenconditionbecomestrue.

Example
(A==B)isnottrue.
(A!=B)istrue.
(A>B)isnottrue.
(A<B)istrue.
(A>=B)isnottrue.
(A<=B)istrue.

LogicalOperators
FollowingtableshowsallthelogicaloperatorssupportedbyTcllanguage.AssumevariableAholds1and
variableBholds0,then
ShowExamples
Operator
&&
||
!

Description
CalledLogicalANDoperator.Ifboththeoperandsarenon
zero,thenconditionbecomestrue.
CalledLogicalOROperator.Ifanyofthetwooperandsis
nonzero,thenconditionbecomestrue.
CalledLogicalNOTOperator.Usetoreversesthelogical
stateofitsoperand.IfaconditionistruethenLogicalNOT
operatorwillmakefalse.

Example
(A&&B)isfalse.
(A||B)istrue.
!(A&&B)istrue.

BitwiseOperators
Bitwiseoperatorworksonbitsandperformbitbybitoperation.Thetruthtablesfor&,|,and^areasfollows
p q p&q p|q p^q
000
0
0
010
1
1
111
1
0
https://www.tutorialspoint.com/cgibin/printpage.cgi

2/4

9/23/2016

100

TclOperators

AssumeifA=60andB=13nowinbinaryformattheywillbeasfollows
A=00111100
B=00001101

A&B=00001100
A|B=00111101
A^B=00110001
TheBitwiseoperatorssupportedbyTcllanguagearelistedinthefollowingtable.AssumevariableAholds60
andvariableBholds13,then
ShowExamples
Operator
&
|
^
<<
>>

Description
BinaryANDOperatorcopiesabittotheresultifitexistsin
bothoperands.

Example
(A&B)willgive12,whichis0000
1100
(A|B)willgive61,whichis0011
BinaryOROperatorcopiesabitifitexistsineitheroperand.
1101
BinaryXOROperatorcopiesthebitifitissetinoneoperand (A^B)willgive49,whichis0011
butnotboth.
0001
BinaryLeftShiftOperator.Theleftoperandsvalueismoved A<<2willgive240,whichis1111
leftbythenumberofbitsspecifiedbytherightoperand.
0000
BinaryRightShiftOperator.Theleftoperandsvalueis
A>>2willgive15,whichis0000
movedrightbythenumberofbitsspecifiedbytheright
1111
operand.

TernaryOperator
ShowExamples
Operator Description
Example
?:
Ternary
IfConditionistrue?ThenvalueX:OtherwisevalueY

OperatorsPrecedenceinTcl
Operatorprecedencedeterminesthegroupingoftermsinanexpression.Thisaffectshowanexpressionis
evaluated.Certainoperatorshavehigherprecedencethanothersforexample,themultiplicationoperatorhas
higherprecedencethantheadditionoperator.
Forexample:x=7+3*2here,xisassigned13,not20becauseoperator*hashigherprecedencethan+,so
itfirstgetsmultipliedwith3*2andthenaddsinto7.
Here,operatorswiththehighestprecedenceappearatthetopofthetable,thosewiththelowestappearatthe
bottom.Withinanexpression,higherprecedenceoperatorswillbeevaluatedfirst.
https://www.tutorialspoint.com/cgibin/printpage.cgi

3/4

9/23/2016

TclOperators

ShowExamples
Category Operator
Unary
+
Multiplicative */%
Additive
+
Shift
<<>>
Relational
<<=>>=
BitwiseAND &
BitwiseXOR ^
BitwiseOR |
LogicalAND &&
LogicalOR ||
Ternary
?:

Associativity
Righttoleft
Lefttoright
Lefttoright
Lefttoright
Lefttoright
Lefttoright
Lefttoright
Lefttoright
Lefttoright
Lefttoright
Righttoleft

https://www.tutorialspoint.com/cgibin/printpage.cgi

4/4

Você também pode gostar