Home page
 
 Home 
 ASP 
 PHP 
 SQL 
 HTML 
 JavaScript 
 Search 
 Contact 
 
Search
or browse popular tags
ASP Tutorial
Subscription

Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button.

Privacy Policy

RSS Twitter

ASP: Conditional Statements

Print

Sometimes when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. Conditional statements are the set of commands used to perform different actions based on different conditions. VBScript has 2 conditional statement constructions, available for you to use in your ASP pages: If...then...else and Select Case. Both perform in saw way the same task.

If ... Then ... Else Statement

The If Statement is a way to make decisions based on a variable or some other type of data. For example, you might have a script that checks if Boolean value is true or false or if variable contains number or string value.

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. The syntax for If statement looks as follows:

if condition then 
   statements_1
else 
   statements_2
end if

Condition can be any expression that evaluates to true or false. If condition evaluates to true, statements_1 are executed; otherwise, statements_2 are executed. statement_1 and statement_2 can be any statement, including further nested if statements.

You may also compound the statements using elseif to have multiple conditions tested in sequence. You should use this construction if you want to select one of many sets of lines to execute.

if condition_1 then
   statement_1
[elseif condition_2 then
   statement_2]
...
[elseif condition_n_1 then
   statement_n_1]
[else
   statement_n]
end if

Let's have a look at the examples. The first example decides whether a student has passed an exam with a pass mark of 57:

<%@ language="vbscript"%>
<%
Dim Result
Result = 70

if Result >= 57 then
    response.write("Pass <br />")
else
    response.write("Fail <br />")
end if
%>

Next example use the elseif variant on the if statement. This allows us to test for other conditions if the first one wasn't true. The program will test each condition in sequence until:

  • It finds one that is true. In this case it executes the code for that condition.
  • It reaches an else statement. In which case it executes the code in the else statement.
  • It reaches the end of the if ... elseif ... else structure. In this case it moves to the next statement after the conditional structure.
<%@ language="vbscript"%>
<%
Dim Result
Result = 70

if Result >= 75 then
    response.write("Passed: Grade A <br />")
elseif Result >= 60 then
    response.write("Passed: Grade B <br />")
elseif Result >= 45 then
    response.write("Passed: Grade C <br />")
else
    response.write("Failed <br />")
end if
%>

Back to top

Select Case Statement

The Select statements work the same as if statements. However the difference is that they can check for multiple values. Of course you do the same with multiple if..else statements, but this is not always the best approach.

The Select statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. The syntax for the Select statement as follows:

select case expression 
   case label_1
      statements_1
   case label_2
      statements_2
   ...
   case else
     statements_n
end select

The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional Case Else clause, and if found, transfers control to that clause, executing the associated statements. If no Case Else clause is found, the program continues execution at the statement following the end of Select. Use break to prevent the code from running into the next case automatically.

Let's consider an example:

<%@ language="vbscript">
<%
Dim Flower
Flower = "rose"

select case flower
  case "rose" 
     response.write(flower & " costs $2.50")
  case "daisy"
     response.write(flower & " costs $1.25")
  case "orchild" 
     response.write(flower & " costs $1.50")
  case else 
     response.write("There is no such flower in our shop")
end select
%>

Back to top

 
 



Tags:

Add To: Add to dzone dzone | Digg this digg | Add to del.icio.us del.icio.us | Stumble it stumbleupon

  • Comments





Copyright © 2005-2012             www.WebCheatSheet.com All Rights Reserved.