API Basic For ko - LIHACHTETAN/ClassicUO-BadNewbie-BasicIDE GitHub Wiki
For / To / Step / Next / Exit For
ClassicUO โข Basic
For๋ ๋๋ฌ ๊ฐ๋ฅํ ๋๊ฐ์ ํฌํจํ๋ ์ซ์ ๋ฒ์์์ ๋ธ๋ก์ ๋ฐ๋ณตํฉ๋๋ค. ๋ฐฐ์ด ์ธ๋ฑ์ค๋ ์ ํด์ง ํ์์ ์์ ์ ์ ํฉํ๋ฉฐ For Each๋ ์์ ๊ฐ์ ์ํํฉ๋๋ค.
์ ํํ ๊ตฌ๋ฌธ
For [VAR] counter = start To limit [Step increment]
statements
Next [counter]
Continue For
Exit For
Break
๋งค๊ฐ๋ณ์
counter / VARโ ์ฐ๊ธฐ ๊ฐ๋ฅํ ์ค์นผ๋ผ ์นด์ดํฐ์ ๋๋ค. VAR๋ ํ๋ก์์ ์์ ์ ์ธํ๋ฉฐ ์๋ตํ๋ฉด ๊ธฐ์กด ๋ณ์๋ฅผ ์ฌ์ฉํฉ๋๋ค. Option Explicit On์์๋ ๋ฏธ๋ฆฌ ์ ์ธํ๊ฑฐ๋ For Var๋ฅผ ์ฌ์ฉํ์ธ์. ํ์์ DIM counter AS Integer๋ก ๋จผ์ ์ ์ธํฉ๋๋ค. ์ซ์ For ํค๋ ์์ AS๋ ์ง์ํ์ง ์์ต๋๋ค.startโ ์์ ์ซ์ ์์ ๋๋ค. ํ ๋ฒ ํ๊ฐํ๊ณ ๋์ ํ ๋ค์ limit์ increment๋ฅผ ํ๊ฐํฉ๋๋ค.limitโ ๋ฒ์์ ํฌํจ๋๋ ๋๊ฐ์ผ๋ก ์ง์ ์ ํ ๋ฒ ํ๊ฐํฉ๋๋ค. ์์ ๋จ๊ณ๋ counter <= limit, ์์ ๋จ๊ณ๋ counter >= limit๋ฅผ ํ์ธํฉ๋๋ค.incrementโ ์ ํ์ ์ซ์ ๋จ๊ณ๋ก ๊ธฐ๋ณธ๊ฐ์ 1์ ๋๋ค. ์์๋ ์์๋ ๊ฐ๋ฅํ์ง๋ง 0์ ์ก์ ์ ์๋ ์ค๋ฅ์ ๋๋ค. ์นด์ดํฐ ํ์๊ณผ ๋จ๊ณ๊ฐ ์ค์ ๋ก ์งํํ ์ ์์ด์ผ ํฉ๋๋ค.statements / Next / exitโ ๋ณธ๋ฌธ๊ณผ Next๋ ๋ณ๋ ์ค์ ๋๋ค. Next ๋ค ์ด๋ฆ์ ์๋ต ๊ฐ๋ฅํ์ง๋ง ์ฐ๋ฉด ์นด์ดํฐ์ ๊ฐ์์ผ ํฉ๋๋ค. Continue For๋ ๋ค์ ๋จ๊ณ๋ก, Exit For๋ ๊ฐ์ฅ ๊ฐ๊น์ด For/For Each ๋ฐ์ผ๋ก ์ด๋ํฉ๋๋ค. Break๋ ์ข ๋ฅ์ ๊ด๊ณ์์ด ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฐ๋ณต๋ฌธ์ ๋๋ ๋๋ค.
๋ฐํ๊ฐ
For, Next, Exit For๋ ๊ฐ์ ๋ฐํํ์ง ์์ต๋๋ค. ์นด์ดํฐ๋ ์ซ์์ด๋ฉฐ ์๋ ์์ดํ ID๊ฐ ์๋๋๋ค. ์ด ์์ง์ ์ ์ ์ข ๋ฃ ํ ๋ฒ์ ๋ฐ ๊ฐ ๋์ ๋ง์ง๋ง์ผ๋ก ์คํํ ๊ฐ์ ์ ์งํฉ๋๋ค. ๋ณธ๋ฌธ์ ๊ฑด๋๋ฐ๋ฉด start, ์กฐ๊ธฐ ์ข ๋ฃํ๋ฉด ํ์ฌ ๊ฐ์ ์ ์งํฉ๋๋ค. ์์ ๋ Main์์ Integer 12, 28, 395๋ฅผ ๋ฐํํฉ๋๋ค.
๋์
- ์ง์ ์์: start ๋์ , ๋๊ฐ๊ณผ ๋จ๊ณ ์ ์ฅ, 0 ๊ฑฐ๋ถ, ์ฒซ ๊ฐ ๊ฒ์ฌ์ ๋๋ค. ๋ฐฉํฅ์ด ๋ง์ง ์์ผ๋ฉด ๋ณธ๋ฌธ์ ๊ฑด๋๋ฐ๋ฉฐ start=limit์ด๋ฉด ํ ๋ฒ ์คํํฉ๋๋ค.
- Next๋ counter+step์ ๊ฒ์ฌํ๊ณ ๋ค์ ๋ฐ๋ณต์ด ๋ฒ์ ์์ผ ๋๋ง ๋์ ํฉ๋๋ค. 1 To 5 Step 3์ 1๊ณผ 4๋ฅผ ๋ฐฉ๋ฌธํฉ๋๋ค. ๋๊ฐยท๋จ๊ณ์ ์๋ ๋ณ์๋ฅผ ๋ฐ๊ฟ๋ ์ ์ฅ๋ ๊ฐ์ ๋ฐ๋์ง ์์ง๋ง ์นด์ดํฐ ์์ฒด๋ฅผ ๋ฐ๊พธ๋ฉด ๋ค์ ๋จ๊ณ์ ์ํฅ์ ์ค๋๋ค.
- ์คํ ์ ๊ตฌ์กฐ์ Next ์ด๋ฆ์ ๊ฒ์ฌํ๋ฉฐ ๊ตฌ์กฐ ์ค๋ฅ๋ SC020์ ๋๋ค. ์ค์ฒฉ ๋ฐ๋ณต๋ฌธ์๋ ์๋ก ๋ค๋ฅธ ์นด์ดํฐ๋ฅผ ์ฌ์ฉํ์ธ์. Try๋ฅผ ๋ ๋๋ฉด Finally๋ฅผ ์คํํฉ๋๋ค. ์ผ์ ์ค์ง์ ์ ์ง๋ ์ ์ง๋์ง๋ง ์๋ ์ง์ฐ์ด๋ ์๊ฐ ์ ํ์ ์์ต๋๋ค.
์์
1. ๋ฐฐ์ด ์ ํฉ๊ณ
# values[2]๋ ์ธ๋ฑ์ค 0,1,2์ 2,4,6์ ๋ด์ต๋๋ค. Sum์ ๋ฐฐ์ด์ ByVal๋ก ๋ฐ๊ณ index=0์์ ์์ํด length-1=2๋ฅผ ์ ์ฅํฉ๋๋ค. ๊ธฐ๋ณธ ๋จ๊ณ 1๋ก ์ธ ์
์ ๋ฐฉ๋ฌธํ๊ณ total=12๋ฅผ Main์ ๋ฐํํฉ๋๋ค.
Option Explicit On
Function Sum(ByVal items)
Var total = 0
For Var index = 0 To GetArrayLength(items) - 1
total += items[index]
Next index
Return total
End Function
Sub Main()
Dim values[2]
values[0] = 2
values[1] = 4
values[2] = 6
Return Sum(values)
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
values[2]๋ ์ธ๋ฑ์ค 0,1,2์ 2,4,6์ ๋ด์ต๋๋ค. Sum์ ๋ฐฐ์ด์ ByVal๋ก ๋ฐ๊ณ index=0์์ ์์ํด length-1=2๋ฅผ ์ ์ฅํฉ๋๋ค. ๊ธฐ๋ณธ ๋จ๊ณ 1๋ก ์ธ ์ ์ ๋ฐฉ๋ฌธํ๊ณ total=12๋ฅผ Main์ ๋ฐํํฉ๋๋ค.
2. ๋์์๋ถํฐ ์ญ์
# items์๋ -1,3,-2,5๊ฐ ์์ต๋๋ค. ์์ Count()-1=3, ๋ 0, ๋จ๊ณ -1์
๋๋ค. ์์ ์์๋ฅผ ์ญ์ ํ๋ฉด ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ธ๋ฑ์ค๋ง ์ด๋ํ๋ฏ๋ก ๋จ์ ์์๋ฅผ ๋์น์ง ์์ต๋๋ค. 3๊ณผ 5๊ฐ ๋จ์ Count()*10+3+5๋ 28์ ๋ฐํํฉ๋๋ค.
Option Explicit On
Sub Main()
Var items = List()
items.Add(-1)
items.Add(3)
items.Add(-2)
items.Add(5)
For Var index = items.Count() - 1 To 0 Step -1
If items[index] < 0 Then
items.RemoveAt(index)
End If
Next index
Return items.Count() * 10 + items[0] + items[1]
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
items์๋ -1,3,-2,5๊ฐ ์์ต๋๋ค. ์์ Count()-1=3, ๋ 0, ๋จ๊ณ -1์ ๋๋ค. ์์ ์์๋ฅผ ์ญ์ ํ๋ฉด ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ธ๋ฑ์ค๋ง ์ด๋ํ๋ฏ๋ก ๋จ์ ์์๋ฅผ ๋์น์ง ์์ต๋๋ค. 3๊ณผ 5๊ฐ ๋จ์ Count()*10+3+5๋ 28์ ๋ฐํํฉ๋๋ค.
3. ์ ์ฅ๋ ๊ฒฝ๊ณ์ ์ต์ข ์นด์ดํฐ
# ReadLimit๋ ByRef calls๋ฅผ ์ฆ๊ฐ์ํค๊ณ value๋ฅผ ๋ฐํํฉ๋๋ค. ์์ 1, ๋ 5, ๋จ๊ณ 2๋ฅผ ๊ฐ๊ฐ ํ ๋ฒ ํ๊ฐํด calls=3์
๋๋ค. ๋ณธ๋ฌธ์ upper=99, stride=1์ ์ด ๋ฐ๋ณต์ ๋ฐ๊พธ์ง ์์ต๋๋ค. 1,3,5๋ฅผ ๋ฐฉ๋ฌธํ์ฌ total=9, index๋ 5๋ก ๋จ์ต๋๋ค. Main์ 395๋ฅผ ๋ฐํํฉ๋๋ค.
Option Explicit On
Function ReadLimit(ByRef calls, ByVal value)
calls += 1
Return value
End Function
Sub Main()
Var calls = 0
Var upper = 5
Var stride = 2
Var total = 0
For Var index = ReadLimit(calls, 1) To ReadLimit(calls, upper) Step ReadLimit(calls, stride)
total += index
upper = 99
stride = 1
Next index
Return calls * 100 + total * 10 + index
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
ReadLimit๋ ByRef calls๋ฅผ ์ฆ๊ฐ์ํค๊ณ value๋ฅผ ๋ฐํํฉ๋๋ค. ์์ 1, ๋ 5, ๋จ๊ณ 2๋ฅผ ๊ฐ๊ฐ ํ ๋ฒ ํ๊ฐํด calls=3์ ๋๋ค. ๋ณธ๋ฌธ์ upper=99, stride=1์ ์ด ๋ฐ๋ณต์ ๋ฐ๊พธ์ง ์์ต๋๋ค. 1,3,5๋ฅผ ๋ฐฉ๋ฌธํ์ฌ total=9, index๋ 5๋ก ๋จ์ต๋๋ค. Main์ 395๋ฅผ ๋ฐํํฉ๋๋ค.