
A switch can be nested. This sometimes achieves performance gains in methods that must search many values in a loop. We can search for a constant string or pattern in an array. Nested switches handle two patterns at the same time with jump tables.
This C# example program demonstrates nested switch statements. Switch improves performance.

When you apply the switch selection statement, the switch expression is evaluated and its actual value is adjusted to point to an offset in the intermediate language, essentially providing a tiny hash table implemented with jump tables. As you know hash tables often have faster lookup performance than loops.
This code searches for the four characters 'g', 'z', 'i', 'p' in that order in a case-insensitive way, using four nested switch statements in a loop. It treats the current character based on the induction variable loop index and then checks the next three characters in the same iteration.
Program that uses nested switch statements [C#]
using System;
class Program
{
static bool ContainsChars(string value)
{
//
// Uses three nested switch statements in a loop to scan character patterns.
//
for (int i = 0; i < value.Length - 3; i++)
{
switch (value[i])
{
case 'g':
case 'G':
switch (value[i + 1])
{
case 'z':
case 'Z':
switch (value[i + 2])
{
case 'i':
case 'I':
switch (value[i + 3])
{
case 'p':
case 'P':
return true;
}
break;
}
break;
}
break;
}
}
return false;
}
static void Main()
{
//
// Test the output of the nested switch method.
//
Console.WriteLine(ContainsChars("GZip"));
Console.WriteLine(ContainsChars("deflate, gzip"));
Console.WriteLine(ContainsChars("zgip"));
Console.WriteLine(ContainsChars("Gzi"));
}
}
Output
True
True
False
False
Description. The ContainsChars method returns a boolean value indicating whether the constant string was found in a case-insensitive way in the source string. The execution engine evaluates the switch expression one to four times per character checked. If all four nested expressions match a case in each switch label section, the method returns true.
Bool Methods, Return True and False Switch Char, Conditional Character TestResults. You can see that the method returns true for the first two parameters and then false for the second two parameters. This provides a basic correctness check that shows that the method works. This logic could be used in websites that must check if a browser's HTTP header requests contain a "gzip" string, which can be used as a significant optimization.

This method is not usually an ideal way to check for a substring inside another string case-insensitively. Most developers will prefer to use the IndexOf method and provide the StringComparison.OrdinalIgnoreCase parameter.
However, the method here can scan the strings encountered on the Internet from browsers in about 4.13 nanoseconds, rather than 87.09 nanoseconds, making it about 21 times faster. Also, a method that uses if-statements instead of the nested switch statements is about 0.3 nanoseconds slower per call.

We looked at a nested switch statement on the character type in the C# language. It is possible to build a method that uses several switch statements inside each other to achieve faster performance in the method body at the cost of more lines of code and more apparent complexity. This provides an example of the switch syntax and how the jump tables used in switches can change the execution of your program.
Switch Statement