Making Your Mind Up

In nearly every program you write, you'll need to make decisions. This tutorial shows you how to do just that!

In real life, we're always having to make decisions - do we eat sandwiches for lunch, or a bowl of noodles; shall we dress up or down for the party? Life is made up of lots of little decisions, and programming is like this too. This tutorial explains how to make decisions when you're writing ASP pages in VBScript.

Logically speaking

One of the most useful statements you'll come across when writing ASP pages, is the If...Then statement. Let's see an example:


<% If age > 12 Then Response.Write "You are a teenager!" %>

This line of code compares a variable called age to the number 12. If the age is 13 or more, then the browser would display the message "You are a teenager!". Great! But what about if I'm 45? This code doesn't quite do the job. We can rewrite it like this:


<% 
If age > 12 And age < 20 Then 
  Response.Write "You are a teenager!" 
End If
%>

This code uses the And operator to combine the results of two conditional statements. When the interpreter reaches the code fragment age > 12 it evaluates it to see if it's true or false. It then does the same with age < 20. Finally it evaluates the two values using And to see whether the combined value is true or false. If it gets a True value, you'll see the message "You are a teenager!", while a False result will result in the interpreter skipping to the next statement of code. VBScript also provides two other logical operators - Or and Not. The following tables shows how these work:

a b a And b
True True True
True False False
False True False
False False False

a And b is true, only if both values are true.

a b a Or b
True True True
True False True
False True True
False False False

a Or b is true whenever either value is true.

a Not a
True False
False True

Not a is the opposite of the value.

We can combine more than one of the logical operators to build up more complicated tests, as the next example shows. See if you can work out what it does.


<% If (gender = "Female" Or gender = "Male") 
 And age < 10 Then ... %>

This condition will result in a true for a female or male under the age of 10. Easy!

Conditional operators

We've already seen most of the conditional operators that you can use, but here's a complete list for you to refer to:

a = b
True if a is equal to b
a < b
True if a is less than b
a > b
True if a is greater than b
a <= b
True if a is less than or equal to b
a >= b
True if a is greater than or equal to b
a <> b
True if a is not equal to b
a And b
True if a and b are true
a Or b
True if either a or b are true
Not a
True if a is false

More on If

In our examples above, we were using the If statement to do something if a condition was true. We can use the Else part of this statement to also do something if the condition was false.


<%
If age >= 13 and age <= 19 Then 
  Response.Write "Hurrah! You are a teenager.<br>"
  If name = "Charles" Then
    Response.Write "Hi Charles!"
  Else
    Response.Write "Hi there!"
  End If
End If
%>

This time, we check the age variable, and if it's between 13 and 19, we then check the name variable. If the name variable contains "Charles", then we write a personalised message to Charles. Otherwise, we write a general greeting.

The other thing to note here is that we've introduced complex statements. These are statements which run over more than one line.

Notice the End If statements that appear in this example - these tell the interpreter when it's reached the end of a statement block.

In this example, we've nested an If...Else...End If block inside an outer If...End If block. The first If age >=13 ... statement runs on until it reaches a matching End If. However in the meantime, another If is opened that spans over more than one line - this one needs to be closed first. So, the final End If is the one that closes the first If that we opened.

This is shown visually in the diagram below, using blocks of colour to indicate the two complex statement blocks:



If age >= 13 and age <= 19 Then 
  Response.Write "Hurrah! You are a teenager.<br>"

If name = "Charles" Then
  Response.Write "Hi Charles!"
Else
  Response.Write "Hi there!"
End If

End If


Sometimes we need to check a number of conditions one after another. VBScript provides us with a couple of ways we can do this. Firstly, we'll show an example using ElseIf, then we'll show you another way to do this using a new statement.


<%
If name = "Charles" Then
  Response.Write "Good to see you Charles!"
ElseIf name = "Elisabeth" Then
 Response.Write "How's it going, Elisabeth?"
ElseIf name = "David" Then
  Response.Write "David, long time no see."
Else
  Response.Write "Hi " & name & "!"
End If
%>

Case by case

The code in our last example does the job, but it's a little bit hard to read. Luckily VBScript also has an alternative - the Select statement. We can rewrite our example above like this:


<%
Select Case name
  Case "Charles"
    Response.Write "Good to see you Charles!"
  Case "Elisabeth"
    Response.Write "How's it going, Elisabeth?"
  Case "David"
    Response.Write "David, long time no see."
  Case Else
    Response.Write "Hi " & name & "!"
End Select
%>

Much easier to follow! The Select statement is great when we want to do a lot of different actions based on different outcomes of the same condition. However, the Select statement is a bit limited in that it can only test for different values of a single variable (our name variable in the above example).

This tutorial has explained how to use the If and Select statements to execute code based on decisions. This is one of the most useful things you'll learn to do in ASP. In our next tutorial, we'll explain how to execute bits of code more that once using loops. Once you know how to build conditional code and how to use loops, you'll be well on your way to proficiency in ASP.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page