if(n)
β¨ if(n) β the Tcl built-in that decides stuff β¨
okay bestie, so you know how sometimes you want your script to choose things? like, do this OR that depending on a vibe check? that's literally if. it lives in the Tcl Built-In Commands crew and it's here to run scripts conditionally. no cap. π
πΈ SYNOPSIS πΈ
if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else?
?bodyN?
π DESCRIPTION π
so here's the tea π΅ β if evaluates expr1 as an expression, exactly the same way that expr evaluates its argument. and that expression's value has got to be a boolean, okay? which means:
- a numeric value, where
0is false and literally anything else is true, or - a string value, like
trueoryesfor true, andfalseornofor false
if it comes back true, then body1 gets executed by passing it to the Tcl interpreter. main character moment for body1, honestly. π
otherwise, expr2 gets evaluated as an expression, and if that's true then body2 runs, and so on down the line. if none of the expressions evaluates to true, then bodyN runs. it's giving β¨cascading decisionsβ¨.
and listen β then and else? they're optional "noise words". they don't do anything, they just make the command easier to read for your own eyeballs. no shame in wanting clarity, we love a readable script π«Ά.
you can have any number of elseif clauses. including zero. zero is a valid number of elseifs, and honestly sometimes zero elseifs is the correct life choice.
bodyN can also be omitted β as long as else is omitted too. they're a package deal, bestie. don't split them up π.
the return value from the command is the result of whichever body script got executed. or, if none of the expressions was non-zero and there was no bodyN, you get an empty string. that's it, that's the whole deal. π¦
π EXAMPLES π
a simple conditional, we love a starter pack:
if {$vbl == 1} { puts "vbl is one" }
with an else-clause, because sometimes it's not one π:
if {$vbl == 1} {
puts "vbl is one"
} else {
puts "vbl is not one"
}
with an elseif-clause too, now we're really cooking π₯:
if {$vbl == 1} {
puts "vbl is one"
} elseif {$vbl == 2} {
puts "vbl is two"
} else {
puts "vbl is not one or two"
}
and okay, remember β expressions can be multi-line, but when they are, it can be a good idea to use the optional then keyword for clarity. iconic behaviour honestly β:
if {
$vbl == 1
|| $vbl == 2
|| $vbl == 3
} then {
puts "vbl is one, two or three"
}
π₯ SEE ALSO π₯
π KEYWORDS π
boolean, conditional, else, false, if, true
and that's the whole page, bestie!! this one comes to you straight from Tcl, filed under if(n) β go forth and conditionally execute things responsibly πβ¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms