Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

while(3tcl) [debian man page]

while(3tcl)						       Tcl Built-In Commands						       while(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
while - Execute script repeatedly as long as a condition is met SYNOPSIS
while test body _________________________________________________________________ DESCRIPTION
The while command evaluates test as an expression (in the same way that expr evaluates its argument). The value of the expression must a proper boolean value; if it is a true value then body is executed by passing it to the Tcl interpreter. Once body has been executed then test is evaluated again, and the process repeats until eventually test evaluates to a false boolean value. Continue commands may be exe- cuted inside body to terminate the current iteration of the loop, and break commands may be executed inside body to cause immediate termi- nation of the while command. The while command always returns an empty string. Note: test should almost always be enclosed in braces. If not, variable substitutions will be made before the while command starts execut- ing, which means that variable changes made by the loop body will not be considered in the expression. This is likely to result in an infinite loop. If test is enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each loop iter- ation), so changes in the variables will be visible. For an example, try the following script with and without the braces around $x<10: set x 0 while {$x<10} { puts "x is $x" incr x } EXAMPLE
Read lines from a channel until we get to the end of the stream, and print them out with a line-number prepended: set lineCount 0 while {[gets $chan line] >= 0} { puts "[incr lineCount]: $line" } SEE ALSO
break(3tcl), continue(3tcl), for(3tcl), foreach(3tcl) KEYWORDS
boolean value, loop, test, while Tcl while(3tcl)

Check Out this Related Man Page

for(3tcl)						       Tcl Built-In Commands							 for(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
for - 'For' loop SYNOPSIS
for start test next body _________________________________________________________________ DESCRIPTION
For is a looping command, similar in structure to the C for statement. The start, next, and body arguments must be Tcl command strings, and test is an expression string. The for command first invokes the Tcl interpreter to execute start. Then it repeatedly evaluates test as an expression; if the result is non-zero it invokes the Tcl interpreter on body, then invokes the Tcl interpreter on next, then repeats the loop. The command terminates when test evaluates to 0. If a continue command is invoked within body then any remaining commands in the current execution of body are skipped; processing continues by invoking the Tcl interpreter on next, then evaluating test, and so on. If a break command is invoked within body or next, then the for command will return immediately. The operation of break and continue are similar to the corresponding statements in C. For returns an empty string. Note: test should almost always be enclosed in braces. If not, variable substitutions will be made before the for command starts execut- ing, which means that variable changes made by the loop body will not be considered in the expression. This is likely to result in an infinite loop. If test is enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each loop iter- ation), so changes in the variables will be visible. See below for an example: EXAMPLES
Print a line for each of the integers from 0 to 10: for {set x 0} {$x<10} {incr x} { puts "x is $x" } Either loop infinitely or not at all because the expression being evaluated is actually the constant, or even generate an error! The actual behaviour will depend on whether the variable x exists before the for command is run and whether its value is a value that is less than or greater than/equal to ten, and this is because the expression will be substituted before the for command is executed. for {set x 0} $x<10 {incr x} { puts "x is $x" } Print out the powers of two from 1 to 1024: for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} { puts "x is $x" } SEE ALSO
break, continue, foreach, while KEYWORDS
for, iteration, looping Tcl for(3tcl)
Man Page