文章摘要
这篇文章介绍了几种常见的循环结构及其在Web开发中的应用,重点展示了如何在Server-side脚本中使用这些循环控制结构。文章详细解释了以下几种循环结构的用法:
1. **do...while循环**:先执行循环体,再检查条件。例如:
```html
Do
response.Write(i & "<br/>");
i = i + 1
Loop until i < 10
```
2. **While循环**:先检查条件,如果条件满足再执行循环体。例如:
```html
i = 6
While i < 10
response.Write(i & "<br/>");
i = i + 1
Wend
```
3. **For循环**:包含初始化、条件和递增三个部分,通常用于遍历数值范围或数组。例如:
```html
For i = 0 to 10
response.Write(i & "<br/>");
If i = 5 Then
Exit For
End If
Next
```
4. **For...each循环**:用于遍历数组或对象,逐个处理每个元素。例如:
```html
Dim array(3)
array(1) = "A"
array(2) = "B"
array(3) = "C"
For Each a In array
response.Write(a & "<br/>")
Next
```
文章通过代码示例说明了这些循环结构在生成动态网页内容中的作用,特别是在处理数据遍历时的高效性。
<body>
do while….loop<br />
<%
‘do while….loop 类似.net中的while(){}
i=6
Do while i < 10
response.Write(i&”<br>”)
i=i+1
Loop
%>
do ….loop until<br />
<%
‘do while….loop 类似.net中的while(){}
i=6
Do
response.Write(i&”<br>”)
i=i+1
Loop until i < 10
%>
while….wend <br />
<%
i=10
while i<20
response.Write(i&”<br>”)
i=i+1
wend
%>
For…Next<br />
<%
for i=0 to 10 ‘ 包括0到10
response.Write(i&”<br>”)
if i=5 then
exit for
end if
next
%>
For…..each….next<br />
<%
dim array(3)
array(1)=”A”
array(2)=”B”
array(3)=”C”
for each a in array
response.Write(a&”<br>”)
next
%>
</body>
</html>