## Tcl8.6.17/Tk8.6.17 Documentation > Tcl Commands > if

### 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

if — 条件に従って記述譜を実行する Execute scripts conditionally

### 構文 SYNOPSIS

if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN?

### 説明 DESCRIPTION

if 命令は expr1 を式( expr が引数を評価する方法と同じ方法で)として評価する｡式の値は､論理値(数値か特定の文字列､数値の場合 0 は偽値､それ以外はどれも真値であり､文字列の場合､真値を意味する true か yes ､偽値を意味する false か no)でなければならない; もし､それが真値であれば､ Tcl 解釈実行機能に body1 が渡され実行される ｡そうでない場合､ expr2 が式として評価され､もし､それが真値であれば body2 が実行される､以下同様である｡もしどの表現の評価も真値でなければ､ bodyN が実行される｡ then と else 引数は､命令の可読性をよくする為の､任意追加の “余分な単語” である｡ elseif 句は幾つあっても好く､無くても良い｡bodyN は else が省略されるときは一緒に省略しても構わない｡命令から返る値は､実行された本体の結果か､どの式も非零でなく､且つ bodyN も無い時は空文字になる｡[訳註､Tclの論理値の詳細は Tcl_GetBoolean を参照されたい]　 The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument). The value of the expression must be a boolean (a numeric value, where 0 is false and anything is true, or a string value such as true or yes for true and false or no for false); if it is true then body1 is executed by passing it to the Tcl interpreter. Otherwise expr2 is evaluated as an expression and if it is true then body2 is executed, and so on. If none of the expressions evaluates to true then bodyN is executed. The then and else arguments are optional “noise words” to make the command easier to read. There may be any number of elseif clauses, including zero. BodyN may also be omitted as long as else is omitted too. The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN.

### 用例 EXAMPLES

単純な条件句: A simple conditional:

    if {$vbl == 1} { puts "vbl is one" }

else句付き: With an else-clause:

    if {$vbl == 1} {
        puts "vbl is one"
    } else {
        puts "vbl is not one"
    }

elseif-句も付いた: With an elseif-clause too:

    if {$vbl == 1} {
        puts "vbl is one"
    } elseif {$vbl == 2} {
        puts "vbl is two"
    } else {
        puts "vbl is not one or two"
    }

覚えておくべきは､多行記述も可能であるが､その場合､明瞭性を保つ為に任意附加的な用語の then を使うのが好ましい: Remember, expressions can be multi-line, but in that case it can be a good idea to use the optional then keyword for clarity:

    if {
        $vbl == 1
        || $vbl == 2
        || $vbl == 3
    } then {
        puts "vbl is one, two or three"
    }

### SEE ALSO

expr, for, foreach

### KEYWORDS

boolean, conditional, else, false, if, true
Copyright © 1993 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.