Next
While Loop 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
For Loop

The Control Flow Loops

For Loop

The most commonly used loop - the for loop. This is extremely useful when you have to execute a given set of instruction a limited number of times with little or no changes.

Syntax:
for init test next body

I like this syntax better...
for { init } { test } { next } {
body
}

Now, for the first time we write a script to do something useful - make a multiplication table. Wondering how that is useful? You will find it very useful when you have forgotten mathematics - like me.


#Multiplication table...
set n 4 ;# We want the multiplication table of 4
set table ""
for { set i 1 } { $i <= 10 } { incr i } {
# This will append all multiples of all numbers for the number n 
#		into a variable called table
	set table "$table $i x $n = [expr $i \* $n]\n"
}

label .mul -text "Multiplication table for $n\n\n$table"
pack .mul

Foreach

Syntax:
foreach varName list body
This is a loop to make programming more easier. Primarily used for lists. Example...

#Make the list
set list [list "Item 1" {Item 2} Last]
set text ""
foreach item $list {
	#Append the every item to $text
	set text "$text\n$item"
}
#Shows the result
label .lab -text "$text"
pack .lab
The same can be achieved by a regular for loop like this.

#Make the list
set list [list "Item 1" {Item 2} Last]
set text ""
for { set i 0 } { $i <= [llength $list] } { incr i } {
	#Append the every item to $text
	set text "$text\n[lindex $list $i]"
}
label .lab -text "$text"
pack .lab
Next
While Loop 

Comments

Kulbhushan at 04 Oct, 2007 03:04
brilliant site...!! Its really helpful for me from school to college....!! thanks for those who is responsible for this site...!! may god bless you all.... Kuchh jayda hi ho gya na.....
Reply to this.
fiel at 28 May, 2008 08:35
i love man...
you really helped me a lot on my work
Reply to this.
Anonymous at 12 Mar, 2009 12:46
Shouldn't the statement:
set table "$table $i x $n = [expr $i \* $n]\n"
read:
set table "$table $i x $n = [expr $i * $n]\n"
instead?
Reply to this.
Anonymous at 16 Jun, 2009 08:40
Both \* and * give the same result. I suppose that the '\' is acting to suppress the interpretation of the '*' during expansion, but because the '*' doesn't expand it really doesn't have any effect so the '\' isn't necessary, but nor is it harmful, just confusing...
Reply to this.
Karen at 11 Nov, 2009 08:19
sorry..im just a beginner..i dont follow this statement:

set table ""
#what is the meaning of ""? is it that the $table dont have any value? or what?

set table "$table $i x $n = [expr $i \* $n]\n
#when i remove the $table, the output will only be 4x10=40, i cant relate the " set table "" ", " $table " and #the output..can anyone please explain it to me..
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed