## Tcl9.0.3/Tk9.0.3 Documentation > Tcl Commands > while

### Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API

### NAME

while — 条件が満たされている限り､記述譜を繰り返し実行する Execute script repeatedly as long as a condition is met

### 構文 SYNOPSIS

while test body

### 説明 DESCRIPTION

この while 命令は 試験 test を式として評価( expr がその引数を評価する方法と同じ方法で)する｡式の値は適切な論理値でなければならない；もし真値であれば本体 body が Tcl 解釈実行機構に手渡され実行に附される｡本体 body が一度実行された後､試験 test が再度評価され､最終的には試験の評価が論理偽値に成ってしまう迄､手順が繰り返される｡ continue 命令が本体の中で実行されることがあり､繰返し反復の､その回の実行を終了させる､又 break 命令が本体内で実行されたときは､ while 命令の即座の終了を引起す｡ while 命令は常に空文字列を返す｡ 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 executed inside body to terminate the current iteration of the loop, and break commands may be executed inside body to cause immediate termination of the while command. The while command always returns an empty string.

註意: 試験 test は殆どの場合波括弧で括ることが望ましい｡括らない時､ while 命令が実行を開始する前に､変数置換が行われ､その結果､本体の繰返しによる式の中の変数の変化が考慮されなくなる｡これは無限繰返しの結果を招き易い｡もし､試験が波括弧で括られていると､変数置換は､(毎回の繰返し課程の前の)式の評価迄遅延され､その変数置換が見える様になる｡ 譬えば､次の記述譜を､$x<10 の箇所を波括弧で括って､と括らずに､とで実行してみるとよい: Note that test should almost always be enclosed in braces. If not, variable substitutions will be made before the while command starts executing, 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 iteration), 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, continue, for, foreach

### KEYWORDS

boolean, loop, test, while
Copyright © 1993 The Regents of the University of California.
Copyright © 1994-1997 Sun Microsystems, Inc.