Tcl - Nested Switch Statement


Advertisements

It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

Syntax

The syntax for a nested switch statement is as follows −

switch switchingString {
   matchString1 {
      body1
      switch switchingString {
         matchString1 {
            body1
         }
         matchString2 {
            body2
         }
         ...
         matchStringn {
            bodyn
         }
      }
   }
   matchString2 {
      body2
   }
...
   matchStringn {
      bodyn
   }
}

Example

#!/usr/bin/tclsh

set a 100
set b 200

switch $a {
   100 {
      puts "This is part of outer switch"
      switch $b {
         200 {
            puts "This is part of inner switch!"
         }
      }
   }   
}
puts "Exact value of a is : $a"
puts "Exact value of a is : $b"

When the above code is compiled and executed, it produces the following result −

This is part of outer switch
This is part of inner switch!
Exact value of a is : 100
Exact value of a is : 200
tcl_decisions.htm
Advertisements