VBScript条件语句

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

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

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

<%@LANGUAGE=“VBSCRIPT” CODEPAGE=“65001”%><%OptionExplicit %><!– 如果使用 Option Explicit,该语句必须出现在脚本的任何其他语句之前。–><!doctype html><html><head><metacharset=“utf-8”><title>VBScript的条件语句</title></head><body><h1>VBScript的条件语句</h1><%DimNum1,Num2Num1=10Num2=20If … Then …当条件为true时执行的语句只有一条Response.Write(“<p><h3>If … Then … 演示</h3>”)If Num1 < Num2 Then Response.Write(“Num1 < Num2” & “</p><hr>”)IfThenEndIf当条件为true时执行的语句不止一条时IfNum1<Num2ThenResponse.Write(“<p><h3>If … Then … End If 演示</h3>”)Response.Write(“Num1 < Num2</p><hr>”)EndIfIf … 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 IfIfThenElseIfElse…演示Response.Write(“<p><h3>If … Then … ElseIf … Else … End If 演示</h3>”)IfNum1=Num2ThenResponse.Write(“Num1 = Num2</p><hr>”)ElseIfNum1>Num2ThenResponse.Write(“Num1 > Num2</p><hr>”)ElseResponse.Write(“Num1 < Num2</p><hr>”)EndIfSelect Case … End Select演示返回当天星期几Response.Write(“<p><h3>Select Case演示</h3>”)Dim Week, WeekNameWeek = WeekDay(date) 获取现在是星期几SelectCaseWeekCase1WeekName=“Sleepy Sunday”Case2WeekName=“Monday again!”Case3WeekName=“Just Tuesday!”Case4WeekName=“Wednesday!”Case5WeekName=“Thursday…”Case6WeekName=“Finally Friday!”Case7WeekName=“Super Saturday!!!!”EndSelectResponse.Write(WeekName&“</p>”)%></body></html>

运行后结果如下显示:

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

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 %>

VBScript的循环语句

VBScript的循环语句

<%

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(“Do While…Loop”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

Do…Loop While循环使用

i = 1

Sum = 0

Do

Sum = Sum + i

i = i + 1

Loop While i <= 100 先循环,再判断

Response.Write(“Do…Loop While”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

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(“Do…Loop中使用Exit Do控制”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

i = 1

Sum = 0

Do

Sum = Sum + i

i = i + 1

Loop Until i > 100 直到i>100,停止循环

Response.Write(“Do…Loop Until”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

While … Wend使用

i = 1

Sum = 0

While i <=100

Sum = Sum + i

i = i + 1

Wend

Response.Write(“While…Wend”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

For … Next使用

Sum = 0

For i = 1 To 100

Sum = Sum + i

Next

Response.Write(“For…Next”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

分别计算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(“For…Next…Step”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

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(“For Each…In…Next”)

Response.Write(“1+2+3+…+99+100=” & Sum & “

“)

———————————————————

%>

</html>

运行后显示结果如下:

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

示例代码下载

ASP流程控制语句.RAR

发表回复

您的电子邮箱地址不会被公开。