Tcl - Instruction de commutateur imbriqué

Il est possible d'avoir un switchdans le cadre de la séquence d'instructions d'un commutateur externe. Même si les constantes de cas du commutateur interne et externe contiennent des valeurs communes, aucun conflit ne surviendra.

Syntaxe

La syntaxe d'un nested switch la déclaration est la suivante -

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

Exemple

#!/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"

Lorsque le code ci-dessus est compilé et exécuté, il produit le résultat suivant -

This is part of outer switch
This is part of inner switch!
Exact value of a is : 100
Exact value of a is : 200