Вы находитесь на странице: 1из 4

1.

set a 10
2. puts $a
3. set a 10
set b 20
puts " c = [expr $a + $b]"
-, *, /, %
4. get stdin a - to get the variable in the a
10
puts $a
5. if {condition} { - decision statement
statement
}
ex: set a 10
if {$a ==10} {
puts "output $a"
} else {
puts " false "
}
6. while {condition} { loop definitions.
statement
}
incr a / incr -a
}
ex: a. increment set a 10
while {$a < 20} {
puts " $a "
incr a
}
b. for decreament
set a 100
while {$a > 1} {
puts " $a "
incr a -1/incr a -2
}
7. for{initialization} {condition} {operation} {
statement
}

ex: a. for increment for {set i 10} {$i < 20} {incr i +2} {
puts $i
}
b. for decrement for {set a 10} {$a > 1} {incr a -1} {
puts $a
}
incr a - increment by once
incr a +2 - increment by twice , similarly vary the valve 2, 3 , 4 etc..
incr a -1 - decrement a by once
incr a -2 - decrement a by twice , , similarly vary the valve 2, 3 , 4 etc..
8. proc - procedure
- based on variables assigning globally

proc name {variable} {


statement
}
name variable

ex: proc add {a b c} {


puts " c = [expr $a + $b + $c] "
}
add 4 5 6

try this -, * , / , %
9. list operations
list file
a. lappend set a hello
lappend a world
b. lindex set a " this is tcl class"
puts " c = [lindex $a 2]"
c. llength puts [llength $a]
d. linsert puts [linsert $a 2 a b]
e. lsort puts [lsort $a]
puts [lsort -decreasing $a]
f. lassign lassign $a 1a 2a 3a
puts $1a
puts $2a
puts $3a
g. lreplace puts [lreplace $a 2 3 a b]
h. lset lset a 0 it
puts $a
10. foreach - it display the array of list in the variable
- content of a displays one below another.
set a " 4 5 6 7"
foreach b $a {
puts [lsort $b]
}
Program
- set a " 2 7 2 5" , large number in this array, sum of these
numbers.
set a "2 3 4 5" - assign variables
set large 0 - initialize large 0
set sum 0 - initialize sum 0
foreach b $a {
if {$a > $large} { [try this $b > $large]
set large $b
set sum [expr $large + $sum]
}
puts " $large $sum"
}
. list operations

Вам также может понравиться