ASP入门(四)-VBScript的流程控制语句

VBScript条件语句

我们可以使用四种条件语句

  • If … Then … 语句
  • If … Then … Else 语句
  • If … Then … ElseIf 语句
  • Select … Case 语句

通过下面这段代码(ConditionalStatement.asp)我们演示所有的条件语句用法:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit %> 
<!-- '如果使用 Option Explicit,该语句必须出现在脚本的任何其他语句之前。-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>VBScript的条件语句</title>
</head>
<body>
<h1>VBScript的条件语句</h1>
<%
Dim Num1, Num2
Num1 = 10
Num2 = 20
'If ... Then ...当条件为true时执行的语句只有一条
Response.Write("<p><h3>If ... Then ... 演示</h3>")
If Num1 < Num2 Then Response.Write("Num1 < Num2" & "</p><hr>")

'If ... Then ... End If 当条件为true时执行的语句不止一条时
If Num1 < Num2 Then
  Response.Write("<p><h3>If ... Then ... End If 演示</h3>")
  Response.Write("Num1 < Num2</p><hr>")
End If

'If ... Then ... Else ... End If 演示
Response.Write("<p><h3>If ... Then ... Else ... End If 演示</h3>")
If Num1 > Num2 Then 
  Response.Write("Num1 > Num2</p><hr>")
Else
  Response.Write("Num1 < Num2</p><hr> ")
End If

'If ... Then ... ElseIf ... Else ...演示
Response.Write("<p><h3>If ... Then ... ElseIf ... Else ... End If 演示</h3>")
If Num1 = Num2 Then
  Response.Write("Num1 = Num2</p><hr>")
ElseIf Num1 > Num2 Then
  Response.Write("Num1 > Num2</p><hr>")
Else
  Response.Write("Num1 < Num2</p><hr>")
End If

'Select Case ... End Select演示返回当天星期几
Response.Write("<p><h3>Select Case演示</h3>")
Dim Week, WeekName
Week = WeekDay(date) '获取现在是星期几
Select Case Week
Case 1
  WeekName = "Sleepy Sunday"
Case 2
  WeekName = "Monday again!"
Case 3
  WeekName = "Just Tuesday!"
Case 4
  WeekName = "Wednesday!"
Case 5
  WeekName = "Thursday..."
Case 6
  WeekName = "Finally Friday!"
Case 7
  WeekName = "Super Saturday!!!!"
End Select
Response.Write(WeekName & "</p>") 
%>
</body>
</html>

运行后结果如下显示:

20150810003

VBScript循环语句

在VBScript中,我们可以四种循环语句

  • For…Next 运行制定次数的一段代码
  • For Each…Next 针对集合中的每个项目或者数组中的每个元素来运行某段代码
  • Do…Loop 当条件为true(While)或者直到条件为true(Until
  • While…Wend 不要使用这个语句,请用Do…Loop代替

同样,本人写一段大代码(LoopStatement.asp)来展示循环语句的使用,代码的主要功能是计算1到100相加的计算。

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit %> 
<!-- '如果使用 Option Explicit,该语句必须出现在脚本的任何其他语句之前。-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>VBScript的循环语句</title>
</head>
<body>
<h1>VBScript的循环语句</h1>
<%
Dim i, Sum
'Do While ... Loop循环
i = 1
Sum = 0
'计算1到100相加的结果
Do While i <= 100  '先判断,在循环
  Sum = Sum + i
  i = i + 1
Loop
Response.Write("<b>Do While...Loop</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
'Do...Loop While循环使用
i = 1
Sum = 0
Do 
  Sum = Sum + i
  i = i + 1 
Loop While i <= 100  '先循环,再判断
Response.Write("<b>Do...Loop While</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
'Exit Do的使用
i = 1
Sum = 0 
Do 
  If i > 100 Then Exit Do '当i>100时退出Do循环 
  Sum = Sum + i
  i = i + 1
Loop
Response.Write("<b>Do...Loop中使用Exit Do控制</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
i = 1
Sum = 0
Do 
  Sum = Sum + i
  i = i + 1
Loop Until i > 100 '直到i>100,停止循环
Response.Write("<b>Do...Loop Until</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
'While ... Wend使用
i = 1
Sum = 0 
While i <=100 
  Sum = Sum + i
  i = i + 1
Wend
Response.Write("<b>While...Wend</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
'For ... Next使用
Sum = 0
For i = 1 To 100
  Sum = Sum + i
Next
Response.Write("<b>For...Next</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
'分别计算1到100的奇数和偶数的和,最后奇数和偶数相加就是最终结果
Dim OddSum '奇数和
Dim EvenSum '偶数和
OddSum = 0
EvenSum = 0
For i = 1 To 100 Step 2
  OddSum = OddSum + i
Next
For i = 0 To 1000 Step 2
  If i > 100 Then Exit For '当i>100时,退出For循环
  EvenSum = EvenSum + i
Next
Sum = EvenSum + OddSum
Response.Write("<b>For...Next...Step</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
Dim Num(100) '声明101个元素的数组Num
For i = 0 To 100
  Num(i) = i '赋值,Num(0)=0, Num(1)=1,...,Num(100)=100
Next
Sum = 0
Dim Nums 
For Each Nums In Num '遍历数组每个元素
  Sum = Sum + Nums
Next
Response.Write("<b>For Each...In...Next</b><br>")
Response.Write("1+2+3+...+99+100=" & Sum & "<hr>")
'---------------------------------------------------------
%>
</body>
</html>

运行后显示结果如下:

20150810004

示例代码下载

ASP流程控制语句.RAR

posted on 2015-08-10 22:09  pchmonster  阅读(2886)  评论(5编辑  收藏  举报

导航