SoFunction
Updated on 2025-03-04

How to use if statement, select, and while loop in ASP

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title><%="Page Title"%></title>
</head>

<body>
<%="Multiple conditional judgment"%>
<%
("<br />")
("Use the Write method of the response object to output the content!")

%>
<br />
<%

dim a,b
a=200
b=300
("The sum of the two numbers is:")
(a+b)
("<br />")
if a=100 then
("a=100,The first condition is met。")
elseif a=200 then
("a=200,The second condition is met。")
elseif a=300 then
("a=300,The third condition is met。")
else
("None of the three conditions are met.")
end if
%>
<%
("<br />")
dim textnumber
textnumber=200
select case textnumber
 case 100
 ("The value is 100, the conditions are satisfied!")
 case 200
 ("The value is 200, the condition is satisfied!")
 case 300
 ("The value is 300, the conditions are satisfied!")
 case else
 ("The above conditions are not met!")
 end select


%>
<%
("<br />")
dim i,sum
i=0
sum=0
 while i<=100
 sum=sum+i
 i=i+1
wend
("The sum of all integers within 100 is:")
(sum)
%>
<%
("<br />")
dim w,s
w=0
s=0
do 
s=s+w
w=w+1
loop while w<=100
("The sum of all integers within 100 is:")
(s)
("<br/>")
("The value of w is:")
(w)
%>

</body>
</html>

I won't talk about the specific introduction, just look at the examples

asp if statement

①if A then B

②if A then

B

end if

③if A then

B

else

C

end if

④if A then

B

elseif C then

D

end if

⑤if A then

B

elseif C then

D

else

E

end if

If....Then.....Else

In the following case, you can use the If...Then...Else statement:
Execute a certain piece of code when the condition is true
When selecting one of two pieces of code to execute
If you need to execute only one line of statements when the condition is true, you can write the code as one line:
if i=10 Then msgbox "Hello"
In the above code, there is no .else.. statement. We just let the code perform one operation when the condition is true (when i is 10).
If we need to execute more than one statement when the condition is true, we must write a statement on one line and then use the keyword "End If" to end the statement:

if i=10 Then
  msgbox "Hello"
  i = i+1
end If

In the above code, there is also no .else.. statement. We just let the code perform multiple operations when the condition is true.
If we want to execute a statement when the condition is true and execute another statement when the condition is not true, we must add the keyword "Else":

if i=10 then
  msgbox "Hello"
else
  msgbox "Goodbye"
end If

The first piece of code is executed when the condition is true, and the second piece of code is executed when the condition is not true (when i is not equal to 10).
If....Then.....Elseif
If you want to choose one of multiple sets of codes to execute, you can use the if...then...elseif statement:

if payment="Cash" then
  msgbox "You are going to pay cash!"
 elseif payment="Visa" then
  msgbox "You are going to pay with visa."
 elseif payment="AmEx" then
  msgbox "You are going to pay with American Express."
 else
  msgbox "Unknown method of payment."
end If

Select Case
If you want to choose one of multiple sets of codes to execute, you can use the SELECT statement:

select case payment
 case "Cash"
  msgbox "You are going to pay cash"
 case "Visa"
  msgbox "You are going to pay with visa"
 case "AmEx"
  msgbox "You are going to pay with American Express"
 case Else
  msgbox "Unknown method of payment"
end select

How the above code works: First, we need a simple expression (often a variable), and this expression will be evaluated once. The value of the expression will then be compared with the value in each case, and if it matches, the code corresponding to the matched case will be executed.