ASP: Conditional Statements
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
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
[elseif condition_2 then
[elseif condition_n_1 then
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:
Result = 70
if Result >= 57 then
response.write("Pass <br />")
response.write("Fail <br />")
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:
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 />")
response.write("Failed <br />")
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
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:
Flower = "rose"
select case flower
response.write(flower & " costs $2.50")
response.write(flower & " costs $1.25")
response.write(flower & " costs $1.50")
response.write("There is no such flower in our shop")
|Copyright © 2005-2007 www.WebCheatSheet.com All Rights Reserved.|