You are on page 1of 2

9/23/2016

TclDecisions

TclDecisions
https://www.tutorialspoint.com/tcltk/tcl_decisions.htm
Copyrighttutorialspoint.com
Decisionmakingstructuresrequirethattheprogrammerspecifiesoneormoreconditionstobeevaluatedor
testedbytheprogram,alongwithastatementorstatementstobeexecutediftheconditionisdeterminedtobe
true,andoptionally,otherstatementstobeexecutediftheconditionisdeterminedtobefalse.
Followingisthegeneralformofatypicaldecisionmakingstructurefoundinmostoftheprogramming
languages

Tcllanguageusestheexprcommandinternallyandhenceitsnotrequiredforustouseexprstatement
explicitly.
Tcllanguageprovidesfollowingtypesofdecisionmakingstatements
S.No.

Statement&Description
ifstatement

An'if'statementconsistsofaBooleanexpressionfollowedbyoneormorestatements.
if...elsestatement

An'if'statementcanbefollowedbyanoptional'else'statement,whichexecuteswhentheBoolean
expressionisfalse.
nestedifstatements

Youcanuseone'if'or'elseif'statementinsideanother'if'or'elseif'statement(s).

https://www.tutorialspoint.com/cgibin/printpage.cgi

1/2

9/23/2016

TclDecisions

switchstatement
4

Aswitchstatementallowsavariabletobetestedforequalityagainstalistofvalues.
nestedswitchstatements

Youcanuseoneswitchstatementinsideanotherswitchstatement(s).

The?:Operator
Wehavecoveredconditionaloperator?:inpreviouschapter,whichcanbeusedtoreplaceif...elsestatements.
Ithasthefollowinggeneralform
Exp1?Exp2:Exp3;

WhereExp1,Exp2,andExp3areexpressions.Noticetheuseandplacementofthecolon.
Thevalueofa'?expression'isdeterminedlikethis:Exp1isevaluated.Ifitistrue,thenExp2isevaluatedand
becomesthevalueoftheentire'?expression.'IfExp1isfalse,thenExp3isevaluatedanditsvaluebecomesthe
valueoftheexpression.Anexampleisshownbelow.
#!/usr/bin/tclsh
seta10;
setb[expr$a==1?20:30]
puts"Valueofbis$b\n"
setb[expr$a==10?20:30]
puts"Valueofbis$b\n"

Whenyoucompileandexecutetheaboveprogram,itproducesthefollowingresult
Valueofbis30
Valueofbis20

https://www.tutorialspoint.com/cgibin/printpage.cgi

2/2

You might also like