Expressions in Tcl
In this part of the Tcl tutorial, we will talk about expressions. In Tcl language the expressions are not built into the core language. Expressions are evaluated with theexpr
command. Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators.
An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator.
The following table shows a set of operators used in the Tcl language.
Category | Symbol |
---|---|
Sign, bit-wise, logical NOT | - + ~ ! |
Exponentiation | ** |
Arithmetic | + - * / % |
Shift | << >> |
Relational | == != < > <= >= |
String comparison | eq ne |
List | in ni |
Bitwise | & | ^ |
Boolean | && || |
Ternary | ?: |
An operator usually has one or two operands. Those operators that work with only one operand are called unary operators. Those which work with two operands are called binary operators. There is also one ternary operator (?:), which works with three operands.
Basic operators
Basic operators are commonly used operators. These are sign operators, arithmetic operators, modulo and exponentiation operators.#!/usr/bin/tclshThe above example shows usage of common operators in Tcl.
puts [expr +2]
puts [expr -2]
puts [expr -(-2)]
puts [expr 2+2]
puts [expr 2-2]
puts [expr 2*2]
puts [expr 2/2]
puts [expr 2/2.0]
puts [expr 2 % 2]
puts [expr 2 ** 2]
puts [expr +2]In this code line we use the plus sign operator. It has no effect on the number. It merely indicates that the number is positive. It can be omitted and most of the time it is.
puts [expr -2]The minus operator is compulsory. It says that the number is negative. The minus operator changes the sign of the number. In the second line, the minus operator changes the -2 to positive 2.
puts [expr -(-2)]
puts [expr 2+2]The above lines show common arithmetic operators in use.
puts [expr 2-2]
puts [expr 2*2]
puts [expr 2/2]
puts [expr 2 % 2]The % is the modulo or remainder operator. It finds the remainder of division of one number by another. 2 % 2, 2 modulo 2 is 0 because 2 goes into 2 once with the remainder of 0. So the code line prints zero to the console.
puts [expr 2 ** 2]This is the exponentiation operator. The code line prints 4 to the console.
$ ./exp.tclOutput of the example.
2
-2
2
4
0
4
1
1.0
0
4
Division operator
Beginning programmers are often confused by division operation. In many programming languages there are two kinds of division operations. Integer and non-integer. This applies for the Tcl as well.% expr 3/2Note the difference between the integer and floating point division. When at least one of the operands is a floating point number, the result is a floating point value too. The result is more exact. If both operands are integers, the result is an integer too.
1
% expr 3/2.0
1.5
Assignment operator, increment operator
There is no assignment operator (=) and no increment/decrement (++/--) operators in Tcl. These operators are common in other computer languages. Instead of that, Tcl has commands.% set a 5The above code shows what commands are used to implement the missing operators.
5
% incr a
6
% incr a
7
% incr a -1
6
% set a 5In Python, we would do
a = 5
. In Tcl, we set a value to a variable using the set
command. % incr aIn C, Java and many other languages, we would increment a variable by one this way:
6
a++;
. In Tcl, we use the incr
command. By default, the value is incremented by 1. % incr a -1The above code shows how to decrement a variable by one, which is accomplished commonly by the (--) decrement operator in other languages.
6
Boolean operators
In Tcl, we have the following logical operators. Boolean operators are also called logical.Symbol | Name |
---|---|
&& | logical and |
|| | logical or |
! | negation |
#!/usr/bin/tclshMany expressions result in a boolean value. Boolean values are used in conditional statements.
set x 3
set y 8
puts [expr $x == $y]
puts [expr $y > $x]
if {$y > $x} {
puts "y is greater than x"
}
puts [expr $x == $y]Relational operators always result in a boolean value. These two lines print 0 and 1.
puts [expr $y > $x]
if {$y > $x} {The body of the
puts "y is greater than x"
}
if
command is executed only if the condition inside the parentheses is met. The $y > $x returns true, so the message "y is greater than x" is printed to the terminal. #!/usr/bin/tclshExample shows the logical and (&&) operator. It evaluates to true only if both operands are true.
puts [expr 0 && 0]
puts [expr 0 && 1]
puts [expr 1 && 0]
puts [expr 1 && 1]
$ ./andoperator.tcl
0
0
0
1
The logical or (||) operator evaluates to true, if either of the operands is true.
#!/usr/bin/tclshIf one of the sides of the operator is true, the outcome of the operation is true.
puts [expr 0 || 0]
puts [expr 0 || 1]
puts [expr 1 || 0]
puts [expr 1 || 1]
$ ./oroperator.tcl
0
1
1
1
The negation operator (!) makes true false and false true.
#!/usr/bin/tclshThe example shows the negation operator in action.
puts [expr ! 0]
puts [expr ! 1]
puts [expr ! (4<3)]
$ ./not.tcl
1
0
1
The
||
, and &&
operators are short circuit evaluated. Short circuit evaluation means that the second argument is only evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of the logical and evaluates to false, the overall value must be false; and when the first argument of logical or evaluates to true, the overall value must be true. (wikipedia) Short circuit evaluation is used mainly to improve performance. An example may clarify this a bit more.
#!/usr/bin/tclshWe have two procedures in the example. (Procedures and conditionals will be described later.) They are used as operands in boolean expressions. We will see, if they are called or not.
proc One {} {
puts "Inside one"
return false
}
proc Two {} {
puts "Inside two"
return true
}
puts "Short circuit"
if { [One] && [Two] } {
puts "Pass"
}
puts "###################"
if { [Two] || [One] } {
puts "Pass"
}
if { [One] && [Two] } {The One procedure returns false. The short circuit && does not evaluate the second procedure. It is not necessary. Once an operand is false, the result of the logical conclusion is always false. Only "Inside one" is only printed to the console.
puts "Pass"
}
puts "###################"In the second case, we use the || operator and use the Two procedure as the first operand. In this case, "Inside two" and "Pass" strings are printed to the terminal. It is again not necessary to evaluate the second operand, since once the first operand evaluates to true, the logical or is always true.
if { [Two] || [One] } {
puts "Pass"
}
$ ./shortcircuit.tclResult of the shorcircuit.tcl script.
Short circuit
Inside one
###################
Inside two
Pass
Relational Operators
Relational operators are used to compare values. These operators always result in boolean value. In Tcl 0 stands for false and 1 for true. Relational operators are also called comparison operators.Symbol | Meaning |
---|---|
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal to |
!= | not equal to |
#!/usr/bin/tclshIn Tcl we use the == to compare numbers. Some languages like Ada, Visual Basic, or Pascal use = for comparing numbers.
puts [expr 3 < 4]
puts [expr 3 == 4]
puts [expr 4 >= 3]
puts [expr 4 != 3]
$ ./rel.tclThe example prints four boolean values.
1
0
1
1
Bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal or hexadecimal symbols are only notations of the same number. Bitwise operators work with bits of a binary number. Bitwise operators are seldom used in higher level languages like Tcl.Symbol | Meaning |
---|---|
~ | bitwise negation |
^ | bitwise exclusive or |
& | bitwise and |
| | bitwise or |
% puts [expr ~7]The operator reverts all bits of a number 7. One of the bits also determines, whether the number is negative or not. If we negate all the bits one more time, we get number 7 again.
-8
% puts [expr ~-8]
7
The bitwise and operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 only if both corresponding bits in the operands are 1.
00110The first number is a binary notation of 6. The second is 3. The result is 2.
& 00011
= 00010
% puts [expr 6 & 3]The bitwise or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if either of the corresponding bits in the operands is 1.
2
% puts [expr 3 & 6]
2
00110The result is
| 00011
= 00111
00110
or decimal 7. % puts [expr 6 | 3]The bitwise exclusive or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if one or the other (but not both) of the corresponding bits in the operands is 1.
7
% puts [expr 3 | 6]
7
00110The result is
^ 00011
= 00101
00101
or decimal 5. % puts [expr 6 ^ 3]
5
% puts [expr 3 ^ 6]
5
Operator precedence
The operator precedence tells us which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions.What is the outcome of the following expression? 28 or 40?
3 + 5 * 5Like in mathematics, the multiplication operator has a higher precedence than addition operator. So the outcome is 28.
(3 + 5) * 5To change the order of evaluation, we can use parentheses. Expressions inside parentheses are always evaluated first.
The following table shows common Tcl operators ordered by precedence (highest precedence first):
Category | Symbol | Associativity |
---|---|---|
Sign, bit-wise, logical NOT | - + ~ ! | Left |
Exponentiation | ** | Left |
Arithmetic | + - * / % | Left |
Shift | << >> | Left |
Relational | == != < > <= >= | Left |
String comparison | eq ne | Left |
List | in ni | Left |
Bitwise | & | ^ | Left |
Boolean | && || | Left |
Ternary | ?: | Right |
!/usr/bin/tclshIn this code example, we show some common expressions. The outcome of each expression is dependent on the precedence level.
puts [expr 3 + 5 * 5]
puts [expr (3 + 5) * 5]
puts [expr ! 1 || 1]
puts [expr ! (1 || 1)]
puts [expr 3 + 5 * 5]This line prints 28. The multiplication operator has a higher precedence than addition. First the product of 5*5 is calculated. Then 3 is added.
puts [expr (3 + 5) * 5]Round brackets can be used to change the precedence level. In the above expression, number 3 is added to 5 and the result is multiplied by 5.
puts [expr ! 1 || 1]In this case, the negation operator has a higher precedence. First, the first true (1) value is negated to false (0), than the || operator combines false and true, which gives true in the end.
$ ./precedence.tclOutput.
28
40
1
0
Associativity
Sometimes the precedence is not satisfactory to determine the outcome of an expression. There is another rule called associativity. The associativity of operators determines the order of evaluation of operators with the sameprecedence level.9 / 3 * 3What is the outcome of this expression? 9 or 1? The multiplication, deletion and the modulo operator are left to right associated. So the expression is evaluated this way:
(9 / 3) * 3
and the result is 9. Arithmetic, boolean, relational and bitwise operators are all left to right associated.
The ternary operator is right associated.
The ternary operator
The ternary operator (?:) is a conditional operator. It is a convenient operator for cases, where we want to pick up one of two values, depending on the conditional expression.cond-exp ? exp1 : exp2If cond-exp is true, exp1 is evaluated and the result is returned. If the cond-exp is false, exp2 is evaluated and its result is returned.
#!/usr/bin/tclshIn most countries the adulthood is based on your age. You are adult if you are older than a certain age. This is a situation for a ternary operator.
set age 32
set adult [expr $age >= 18 ? true : false]
puts "Adult: $adult"
set adult [expr $age >= 18 ? true : false]First the expression on the right side of the assignment operator is evaluated. The first phase of the ternary operator is the condition expression evaluation. So if the age is greater or equal to 18, the value following the ? character is returned. If not, the value following the : character is returned. The returned value is then assigned to the adult variable.
$ ./ternary.tclA 32 years old person is adult.
Adult: true
Calculating prime numbers
We are going to calculate prime numbers. Some of the features (lists, loops) will be covered later in the tutorial.#!/usr/bin/tclshIn the above example, we deal with many various operators. A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. We pick up a number and divide it by numbers, from 1 up to the picked up number. Actually, we don't have to try all smaller numbers, we can divide by numbers up to the square root of the chosen number. The formula will work. We use the remainder division operator.
set nums { 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24
}
puts "Prime numbers"
foreach num $nums {
if { $num==1 || $num==2 || $num==3 } {
puts -nonewline "$num "
continue
}
set i [expr int(sqrt($num))]
set isPrime true
while { $i > 1 } {
if { $num % $i == 0 } {
set isPrime false
}
incr i -1
}
if { $isPrime } {
puts -nonewline "$num "
}
}
puts ""
set nums { 1 2 3 4 5 6 7 8 9 10 11 12 13 14We will calculate primes from this list of numbers.
15 16 17 18 19 20 21 22 23 24
}
if { $num==1 || $num==2 || $num==3 } {We skip the calculations for the 1, 2, 3 numbers. They are primes. Note the usage of the equality and conditional or operators. The == has a higher precedence than the || operator. So we don't need to use parentheses.
puts -nonewline "$num "
continue
}
set i [expr int(sqrt($num))]We are OK if we only try numbers smaller than the square root of a number in question.
while { $i > 1 } {This is a while loop. The i is the calculated square root of the number. We use the
if { $num % $i == 0 } {
set isPrime false
}
incr i -1
}
incr
command to to decrease the i by one each loop cycle. When the i is smaller than 1, we terminate the loop. For example, we have number 9. The square root of 9 is 3. We will divide the 9 number by 3 and 2. This is sufficient for our calculation. if { $isPrime } {This is the core of the algorithm. If the remainder division operator returns 0 for any of the i values, than the number in question is not a prime.
puts -nonewline "$num "
}
$ ./prime.tclOutput of the script.
Prime numbers
1 2 3 5 7 11 13 17 19 23
In this part of the Tcl tutorial, we covered expressions.
0 comments:
Post a Comment