Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested If Statements

Status
Not open for further replies.

GRonken

Programmer
Oct 30, 2003
26
0
0
US
Can I do a nested if staement in tcl?

if {[string compare $LookForSTDRPTTEXT $Compare3] == 0} {
set temp_1 [append temp_1 $inline]

# Ok this is where if want to test it agin if it was true in the above statement - and WHERE do those {} go <grin>

} else {
set temp_1 ttt
}
puts $fidout temp_1

}
 
Code:
  if {cond}   {
    previous statement
    if {cond2}     {
      if {cond3} { then }
      if {cond4} { then } else {else }
    }
    next statement
  }   else   {
    else script
  }
The if command has the following syntax:
[bold]if[/bold] cond-expression then-script ??[bold]elseif[/bold]? elseif-script?... ??[bold]else[/bold]? else-script

Some variations:
[bold]if[/bold] cond then-script
[bold]if[/bold] cond then-script else-script
[bold]if[/bold] cond then-script [bold]else[/bold] else-script

All scripts are sequences of commands:
{
command
...
}
And a command can be a [bold]if[/bold] command.

A final example:
Code:
if {$a} {
  if {$b} { do_a_b } else { do_a_!b }
} else {
  if {$b} { do_!a_b } else { do_!a_!b }
}
For more on if:
HTH

ulis
 
puts &quot;Enter one character&quot;
gets stdin input

set length [string length $input]

if {$length == &quot;1&quot; } {
if {$input >= &quot;0&quot; && $input < &quot;10&quot;} {
puts &quot;Integer received&quot;
} else {
puts &quot;Non integer received&quot;
}
} elseif {$length < 3} {
puts &quot;Small string&quot;
} else {
puts &quot;Big string&quot;
}

Hope this helps
Rgds
Koti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top