Lua 조건문, 반복문

2015. 9. 19. 23:48프로그래밍/Script

728x90
728x90
종류

if, while, repeat, for


if 조건문

내가 아는 그 이프문이 맞겠지.


1
2
3
4
5
6
7
8
if condition then
    statement1
elseif another condition then
    statement2
...
else
    statementEnd
end
cs


elseif <- 붙여서 쓰는걸 주의


while과 repeat 반복문

repeat은 do~while과 비슷하다.


1
2
3
4
5
6
7
8
while condition do
    statement
end
 
repeat
    statment
until condition
 
cs


break를 이용하면 루프문을 탈출 할 수 있다.





숫자형 for 문


1
2
3
for value = initValue, compareValue [,incrementValue] do
    statment
end
cs


간단한 형태 이해하기 어렵지 않다.

generic for 문

기존 C/C++에서 사용하던거와는 조금 다른 형태


1
2
3
for value1[, value2...] in repeatFunction do
    statment
end
cs


repeatFunction

pairs(tablle)

key-value 형태의 쌍으로 이뤄진 반복함수.


ipairs(table)

index-value 형태의 쌍으로 index가 숫자인 것만 가져온다.


next()

next는 helper function으로 pairs에 사용되는 함수로 테이블에서 현재 인덱스의

다음에 오는 키와 값의 쌍을 반환한다.


io.lines()

파일에서 한 라인씩 읽어오기


file:lines()

역시 한 줄 씩 읽는건데 C/C++ 파일 입출력과 비슷한 형태로 관리


Custom iteratos

사용자 정의 반복함수를 생성하여 사용한다.








728x90
반응형

'프로그래밍 > Script' 카테고리의 다른 글

Lua와 C 바인딩  (0) 2015.09.20
Lua 메타테이블  (0) 2015.09.20
Lua 5.x와 4.0 호환성  (0) 2015.09.20
Lua 연산자  (0) 2015.09.19
Lua 함수  (0) 2015.09.19
Lua 자료형  (0) 2015.09.19
Lua script 개요  (1) 2015.09.19