ASP 반복문

ASP 반복문


1 . For ~ Next 구문

  • For 카운트 변수 = 초기값 To 종료값 [Step 카운트 변수의 증가값]
    “반복 실행될 코드”
    Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<% @Language=VBS-ript %><html><head><title>For ~ Next 구문</title></head><body><%
Dim i
For i = 1 To 10
Response.Write "현재 i 변수의 값은 "
Response.Write i
Response.Write " 입니다.<br>"
If i = 5 Then Exit For<- Exit For(for문을 빠져나오는 구문)
Next
%></body></html>
2 . For Each ~ Next 구문
- 앞서 살펴본 For ~ Next 구문은 루프 카운터 변수를 이용하여 임의의 회수만큼 코드를
반복 실행할수 있는 구문 이였으나 이와 유사한 For Each ~ Next구문은 배열이나 컬랙션에서 각 원소를
자동으로 순환해 주는 구문이다
- For Each 변수 in 배열 또는 컬랙션
"반복 실행될 구문"
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<% @Language=VBS-ript %><html><head><title>For Each ~ Next 구문</title></head><body><%
Dim myArray(4)
myArray(0) = "바람의 파이터"
myArray(1) = "역도산"
myArray(2) = "해리포터"
myArray(3) = "반지의 제왕"
myArray(4) = "스파이더맨"
Response.Write "내가 좋아하는 영화<br><br>"
For Each item In myArray
Response.Write item&"<br>"
Next
%></body></html>
3 . Whie ~ Wend 구문
- While ~ Wend 구문은 주어진 조건식에 따라 조건식이 True인 동안 계속해서 코드를 반복실행
할수 있는 구문이다 . For ~ Next구문도 그렇지만 이구문역시 다른 구문들과 달리 End While로
끝나지 않는것에 주의해야 함
- While 조건
"반복 실행될 구문
Wend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<% @Language=VBS-ript %><html><head><title>While ~ Wend 구문</title></head><body><%
Dim i
i = 1
While i<= 5
Response.Write "현재 i 변수의 값은 "
Response.Write i
Response.Write " 입니다.<br>"
i = i + 1
Wend
%></body></html>
4 . Do ~ Loop 구문
- Do ~ Loop 구문은 방금 살펴본 While ~ Wend구문에 비해 더욱 유연한 루프를 처리할 수 있도록 해 준다
Do ~ Loop 구문은 While ~ Wend구문과 비슷하지만 다음과 같이 두가지 형식으로 사용될수 있다
- Do While 조건식
"조건식이 True인 동안 반복 실행될 구문
Loop
- Do Until 조건식
"조건식이 True 가 될때까지 반복 실행될 구문
Loop
  • Do ~ Loop구문은 While키워드나 Until키워드 둘중 하나를 사용할 수 있으며 While키워드와
    Until 키워드의 위치에 따라 또 다시 다음과 같은 두가지 사용형식을 가집니다.

1 . Do While ~ Loop 구문

  • Do While ~ Loop 구문은 조건식이 참인동안 반복해서 코드를 실행하는 구문입니다.
1
2
3
4
5
6
7
8
9
10
<% @Language=VBS-ript %><html><head><title>Do While ~ Loop 구문</title></head><body><%
Dim i
i = 10
Do While i<5
Response.Write "현재 i 변수의 값은 "
Response.Write i
Response.Write " 입니다.<br>"
i = i + 1
Loop
%></body></html>

2 . Do ~ Loop While 구문

  • Do While ~ Loop 구문과 Do ~ Loop While구문의 차이점은
    먼저 Do While ~ Loop 구문은 While구문에 지정된 조건식을 판단한 후 이 조건식이
    True이면 Do ~ Loop 구문내의 코드를 반복실행 합니다. 이에반해 Do ~ Loop While구문은 일단
    Do ~ Loop 구문내의 코드를 한번 실행한 뒤 While구문의 조건식을 검사하여 이 조건식이 True이면
    다시 Do ~ Loop구문내의 코드를 실행하게 됩니다.
1
2
3
4
5
6
7
8
9
10
<% @Language=VBS-ript %><html><head><title>Do ~ Loop While 구문</title></head><body><%
Dim i
i = 10
Do
Response.Write "현재 i 변수의 값은 "
Response.Write i
Response.Write " 입니다.<br>"
i = i + 1
Loop While i<5
%></body></html>

4 . Do Until ~ Loop 구문

  • Until 키워드를 사용하는 Do Until ~ Loop 구문은 조건식이 True인 동안이 아니라
    True가 될때까지 코드를 반복 실행 합니다.
1
2
3
4
5
6
7
8
9
10
<% @Language=VBS-ript %><html><head><title>Do Until ~ Loop 구문</title></head><body><%
Dim i
i = 1
Do Until i>5
Response.Write "현재 i 변수의 값은 "
Response.Write i
Response.Write " 입니다.<br>"
i = i + 1
Loop
%></body></html>
Share