ASP.NET Basics (part 3): Hard Choices - Switching Things Around (Page 7 of 8 )
Finally, C# rounds out its basket of conditional expressions with the "switch" statement, which offers an alternative method of transferring control from one program block to another. Here's what it looks like:
switch (decision-variable)
{
case first-condition:
// do this!
break;
case second-condition:
// do this!
break;
case third-condition:
// do this!
break;
// ... and so on...
default:
// do this by default!
break;
}
The "switch" statement can best be demonstrated by rewriting the previous example using "switch" instead of "if-else if-else".
<script language="c#" runat="server">
void Page_Load()
{
DateTime date = System.DateTime.Now;
string day = date.DayOfWeek.ToString();
// set the day of the week label
dayoftheweek.Text = day;
switch(day) {
case "Monday":
fortune.Text = "Adam met Eve and turned over a new leaf.";
break;
case "Tuesday":
fortune.Text = "Always go to other people's funerals, otherwise they won't come to yours.";
break;
case "Wednesday":
fortune.Text = "An unbreakable toy is useful for breaking other toys.";
break;
case "Thursday":
fortune.Text = "Be alert - the world needs more lerts.";
break;
case "Friday":
fortune.Text = "Crime doesn't pay, but the hours are good.";
break;
default:
fortune.Text = "Sorry, closed on the weekend.";
break;
}
}
</script>
<html>
<head><title>Fortune Cookies, Anyone?</title></head>
<body>
Today is <asp:label id="dayoftheweek" runat="server" /> and your fortune cookie says <asp:label id="fortune" runat="server" /> </body> </html>
Here's the output:

The "switch" statement is followed by a decision variable, which serves as the foundation of the construct. Depending on the value of the decision variable, the various "case" blocks are evaluated and the one matching the decision variable (if available) is executed.
The "switch" statement is followed by a list of "case" blocks, which sets up the different code branches for each possible value of the conditional expression embedded in the "switch" statement. Each block begins with the keyword "case" and a constant value. The "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it.
It's interesting also to note that the decision variable at the beginning of the "switch" statement can evaluate different data types such as integers, strings, chars or even Booleans (remember these from the previous article?). A "boolean" data type can have only two values, true or false, and are declared using the "bool" keyword. However, if you have only two options available, it makes more sense to use the vanilla "if-else" statement or the ? conditional operator.
You can also evaluate multiple possible values within the same "case" block, simply by clubbing them together. This variant demonstrates, by having one "case" block execute for Mondays, Wednesdays and Fridays and a second "case" block for Tuesdays and Thursdays:
<script language="c#" runat="server">
void Page_Load()
{
DateTime date = System.DateTime.Now;
string day = date.DayOfWeek.ToString();
// set the day of the week label
dayoftheweek.Text = day;
switch(day) {
case "Monday":
case "Wednesday":
case "Friday":
fortune.Text = "Adam met Eve and turned over a new leaf.";
break;
case "Tuesday":
case "Thursday":
fortune.Text = "Always go to other people's funerals, otherwise they won't come to yours.";
break;
default:
fortune.Text = "Sorry, closed on the weekend";
break;
}
}
</script>
<html>
<head><title>Fortune Cookies, Anyone?</title></head>
<body>
Today is <asp:label id="dayoftheweek" runat="server" /> and your fortune cookie says "<asp:label id="fortune" runat="server" />". </body> </html>
A number of other keywords are available in the context of a "switch" conditional statement:
* The "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it.
* The "default" keyword is used to execute a default set of statements when the variable passed to "switch" does not satisfy any of the conditions listed within the block.
* The "continue" keyword can be used to move program execution back to the beginning of the "switch" block (not recommended as you will end up in a messy endless loop if you're careless)
* The "goto case" construct can be used to jump to another "case" block, while the "goto default" keyword moves program execution to the default "case" block.
Next: Endgame >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire