Sha256: 56ac563cf0e6ed6af462b1fdfea5e775e653a12a5a1003d98f88a29be251fe4e

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

puts "Please enter two numbers:"

set x [expr {int([gets stdin])}]; # Force integer interpretation
set y [expr {int([gets stdin])}]; # Force integer interpretation

puts "$x + $y = [expr {$x + $y}]"
puts "$x - $y = [expr {$x - $y}]"
puts "$x * $y = [expr {$x * $y}]"
puts "$x / $y = [expr {$x / $y}]"
puts "$x mod $y = [expr {$x % $y}]"
puts "$x 'to the' $y = [expr {$x ** $y}]"

Since Tcl doesn't really know about the "type" of a variable, the "<tt>expr</tt>" command is used to declare whatever follows as an "expression". This means there is no such thing as "integer arithmetic" and hence the kludge with <tt>int([gets&amp;nbsp;stdin])</tt>.

Often, these operations would be performed in a different way from what is shown here. For example, to increase the variable "x" by the value of the variable "y", one would write

incr x $y

Also, it's important to surround the arguments to the <code>expr</code> in braces, especially when any of the parts of the expression are not literal constants.  Discussion of this is on [http://wiki.tcl.tk/10225 The Tcler's Wiki].

Version data entries

10 entries across 7 versions & 1 rubygems

Version Path
zettacode-0.1.7 files.zettacode/arithmetic.integer/tcl.txt
zettacode-0.1.6 files.zettacode/arithmetic.integer/tcl.txt
zettacode-0.1.6 files.zettacode2/arithmetic.integer/tcl.txt
zettacode-0.1.5 files.zettacode/arithmetic.integer/tcl.txt
zettacode-0.1.5 files.zettacode2/arithmetic.integer/tcl.txt
zettacode-0.1.4 files.zettacode/arithmetic.integer/tcl.txt
zettacode-0.1.4 files.zettacode2/arithmetic.integer/tcl.txt
zettacode-0.1.3 files.zettacode/arithmetic.integer/tcl.txt
zettacode-0.1.2 files.zettacode/arithmetic.integer/tcl.txt
zettacode-0.1.1 zettacode.files/arithmetic.integer/tcl.txt