ASP.NET Basics (part 3): Hard Choices - Cookie-Cutter Code (Page 4 of 8 )
The "if-else" construct certainly offers a smidgen more flexibility than the basic "if" construct, but still limits you to only two possible courses of action. If your script needs to be capable of handling more than two possibilities, you should reach for the "if-else if-else" construct, which is a happy combination of the two constructs you've just been reading about.
if (first condition is true)
{
do this!
}
else if (second condition is true)
{
do this!
}
else if (third condition is true)
{
do this!
}
... and so on ...
else
{
do this!
}
Take a look at it in action:
<script language="c#" runat="server">
void Page_Load()
{
// temperature
int temp = 5;
if(temp >= 35)
{
message.Text = "Man, it's hot out there!";
}
else if (temp < 35 && temp >= 10)
{
message.Text = "Well, at least it isn't as hot as it could be!";
}
else if (temp < 10)
{
message.Text = "Man, it's freezing out there!";
}
// this is redundant, included for illustrative purposes
else
{
message.Text = "Huh? Somebody screwed up out there!";
}
}
</script>
<html>
<head><title>Weather Forecast</title></head>
<body>
<asp:label id="message" runat="server" />
</body>
</html>
In this case, depending on the value of the "temp" variable, the appropriate code branch is executed, thereby making it possible to write scripts, which allow for multiple possibilities.
One important point to be noted here: control is transferred to successive "if" branches only if the preceding condition(s) turn out to be false. Or, in English, once a specific conditional expression is satisfied, all subsequent conditional expressions are ignored.
Here's another example, this one using the day of the week to decide which fortune cookie to display.
<script language="c#" runat="server">
void Page_Load()
{
// get the current day of the week
DateTime date = System.DateTime.Now;
string day = date.DayOfWeek.ToString();
// set the day of the week label
dayoftheweek.Text = day;
// check day and set fortune
if (day == "Monday")
{
fortune.Text = "Adam met Eve and turned over a new leaf.";
}
else if (day == "Tuesday")
{
fortune.Text = "Always go to other people's funerals, otherwise they won't come to yours.";
}
else if (day == "Wednesday")
{
fortune.Text = "An unbreakable toy is useful for breaking other toys.";
}
else if (day == "Thursday")
{
fortune.Text = "Be alert - the world needs more lerts.";
}
else if (day == "Friday")
{
fortune.Text = "Crime doesn't pay, but the hours are good.";
}
else
{
fortune.Text = "Sorry, closed on the weekend.";
}
}
</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:

A quick note on the following lines of code:
// snip
DateTime date = System.DateTime.Now;
string day = date.DayOfWeek.ToString();
// set the day of the week label
dayoftheweek.Text = day;
// snip
That was an off-the-cuff intro to the DateTime data type in C#. I shall talk about this little data type at a later date (pun intended!). For the moment, just assume that the above code snippet returns the current day of the week.
Next: Three In One >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire