Metaphern Akazien Verlag   
Lesezeichen [ Info # Jobs # QR-Code # Sitemapper ]Mo 20 Mai 2024 18:33:57 # Grundlagen der Programmierung | Tcl Tutorial.


. Looping 101 - While loop .

[ Previous | Index | Next ]

Tcl includes two commands for looping, the while and for commands. Like the if statement, they evaluate their test the same way that the expr does. In this lesson we discuss the while command, and in the next lesson, the for command. In most circumstances where one of these commands can be used, the other can be used as well.

  • while test body

The while command evaluates test as an expression. If test is true, the strong in body is executed. After the strong in body has been executed, test is evaluated again.

A continue statement within body will stop the execution of the strong and the test will be re-evaluated. A break within body will break out of the while loop, and execution will continue with the next line of strong after body.

In Tcl everything is a command, and everything goes through the same substitution phase. For this reason, the test must be placed within braces. If test is placed within quotes, the substitution phase will replace any variables with their current value, and will pass that test to the while command to evaluate, and since the test has only numbers, it will always evaluate the same, quite probably leading to an endless loop!

Look at the two loops in the example. If it weren't for the break command in the second loop, it would loop forever.


. Example .

   # This is a normal way to write a Tcl while loop.

   set x 1
   while {$x < 5} {
    puts "x is $x"
    set x [expr $x + 1]

   puts "exited first loop with X equal to $x\n"

   # The next example shows the difference between ".." and {...}
   # How many times does the following loop run?  Why does it not
   # print on each pass?

   set x 0
   while "$x < 5" {
   set x [expr $x + 1]
   if {$x > 7} break
   if "$x > 3" continue
    puts "x is $x"

   puts "exited second loop with X equal to $x"

[ Home | Top ]
[ . Previous | Index | Next . ]
Der Inhalt dieser Seite wurde am 06.11.2019 um 12.19 Uhr aktualisiert.
Navigation Seminare Magic Software Projekte Publikationen Kontakt Home
   huecker dot com * Germany | Datenschutz
© 1999, 2024 Franz-Josef Hücker. All Rights Reserved.
Send Page Print Page LinkedIn follow me on twitter RSS Feeds & Podcasts