Lesson 6: Conditions
Conditions are used to execute part of a script only if some predefined requirements (conditions) are fulfilled. For example, a condition could be that a date must be after 1/1 2012 or that a variable is greater than 7.
If... Then ... Else
The first type of condition we will look at is the If-Then-Else condition, which has the following syntax:
If condition Then statment Else statement End If
Again, the syntax is very close to ordinary English: If a condition is met Then execute something or Else execute something else.
In lesson 4, you learned how to find the number of a month. In the following example, we will use the month number in If-Then-Else condition to find out what season it is:
<html> <head> <title>Loops</title> </head> <body> <% If Month (Date) = 3 Then Response.Write "<p>Now it's spring!</p>" Else Response.Write "<p>I do not know what season it is!</p>" End If %> </body> </html>
As you can see, it's not a particularly smart condition - it only works when it's March!
However, there are plenty of ways to improve the condition and make it more precise. Below are listed comparison operators that can be used in the statement:
= Equals
< Less than
> Greater than
<= Less than or equal to
> = Greater than or equal to
<> Not equal to
In addition, there are some logical operators:
AND
OR
NOT
The operators can be used to develop more precise conditions, so now we can expand the above example to include all the spring months:
<html> <head> <title>Loops</title> </head> <body> <% If Month(Date)> = 3 AND Month(Date) <= 5 Then Response.Write "<p>Now it's spring!</p>" Else Response.Write "<p>Now it's either winter, summer or autumn!</p>" End If %> </body> </html>
Let us take a closer look at the extended condition:
Month(Date)> = 3 AND Month(Date) <= 5
The condition can be translated into:
Smart, eh? Operators play a significant role in many different parts of ASP.
But it still only works with March, April and May. All other months are not yet covered by the condition. Let us try to develop the condition a little more.
If ... Then ... ElseIf ... Else
Using ElseIf, we can expand the condition and make it work for all months:
<html> <head> <title>Loops</title> </head> <body> <% If Month(Date)> = 3 AND Month(Date) <= 5 Then Response.Write "<p>Now it's spring!</p>" Elseif Month(Date)> = 6 AND Month(Date) <= 8 Then Response.Write "<p>Now it's summer!</p>" Elseif Month(Date)> = 9 AND Month(Date) <= 11 Then Response.Write "<p>Now it's autumn!</p>" Else Response.Write "<p>Now is the winter!</p>" End If %> </body> </html>
To write conditions is all about thinking logically and being methodical. The example above is pretty straightforward, but conditions can get very complex.
Select ... Case
Another way of writing conditions is to use the Select Case method:
Select Case Expression Case 1 statement Case 2 statement Case Else statement End Select
This method is based on an expression and then list different "answers" or "values" with related statements. The easiest way to explain the method is to show an example.
As you may remember from lesson 4, the function Weekday returns the current weekday. This can be used in an example where we write the name of the day (instead of a number):
<html> <head> <title>Loops </title> </head> <body> <% Select Case Weekday(Now,vbMonday) Case 1 Response.Write "Monday" Case 2 Response.Write "Tuesday" Case 3 Response.Write "Wednesday" Case 4 Response.Write "Thursday" Case 5 Response.Write "Friday" Case 6 Response.Write "Saturday" Case Else Response.Write "Sunday" End Select %> </body> </html>
Often Select Case can be a good alternative to If-Then-Else. What you should use in a given situation depends on which method you find easiest and most logical. Making your scripts logical and clear can be a great challenge.
In the next lesson, we will look at how you can add comments to your scripts to explain how they work. Good comments can be crucial if you or somebody else has to make changes in your codes at a later stage.