Lesson 2.2.4 The Switch statement
These are used for looking at a value and conveniently executing something based on it. It doesn’t handle complex conditions in the same way as an IF statement but can be much more readable.
A sample switch statement is shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchStatement1
{
class Program
{
static void Main(string[] args)
{
int number = 1;
switch (number)
{
case 1:
System.Console.WriteLine("Number 1");
break;
case 2:
System.Console.WriteLine("Number 2");
break;
case 3:
System.Console.WriteLine("Number 3");
break;
default:
System.Console.WriteLine("Unrecognised");
break;
}
System.Console.ReadKey(true);
}
}
}
If you run it, it will show a simple readout of “Number 1”. Change the value of the variable called “number” at the top to illicit a different response.
The switch statement is made up of three parts, one of them optional.
- The switch declaration
- The case handlers
- A default handler
The switch declaration marks the start of the block.
It takes a value, almost always a variable. This is the value the switch decision is being made on.
The case handlers are a set of blocks of logic associated with a potential value from the switch statement.
case 1:
System.Console.WriteLine("Number 1");
break;
For instance, the case handler above has been written to handle the eventuality of the “number” variable equalling 1. It executes a line of logic to write “Number 1” and then breaks out of the block of code to handle the value. An important note, the break command is obligatory. If you don’t include it C# will complain about falling through from one case label to another. A case label being the case 1, case 2 or whatever else you have a setup.
The third part of case statement is not needed but is neat to have. Generally, if you think you don’t need one it is a sign you may have written some smelly code.
default:
System.Console.WriteLine("Unrecognised");
break;
This is fairly similar to the ELSE block in the IF statement. It is a handler for picking up what happens if nothing else evaluates to true. It is a bucket to catch those conditions that slip through. It also needs a break statement.
The switch statement is not just limited to integer values. The switch block will work with characters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchStatement2
{
class Program
{
static void Main(string[] args)
{
char letter = 'a';
switch (letter)
{
case 'a':
System.Console.WriteLine("Letter a");
break;
case 'b':
System.Console.WriteLine("Letter b");
break;
case 'c':
System.Console.WriteLine("Letter c");
break;
default:
System.Console.WriteLine("Unrecognised");
break;
}
System.Console.ReadKey(true);
}
}
}
And string values too.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchStatement3
{
class Program
{
static void Main(string[] args)
{
string word = "hello";
switch (word)
{
case "hello":
System.Console.WriteLine("Well hello to you");
break;
case "goodbye":
System.Console.WriteLine("Tatty bye");
break;
case "pardon":
System.Console.WriteLine("I didn't say anything");
break;
default:
System.Console.WriteLine("Unrecognised");
break;
}
System.Console.ReadKey(true);
}
}
}
In fact it will work with any whole number, string or character type value.