English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Lua 언어의 goto 문은 무조건적으로 표시된 문장으로 제어 흐름을 전환할 수 있습니다.
문법 형식은 다음과 같습니다:
goto Label
Label의 형식은 다음과 같습니다:
:: Label ::
다음 예제에서 goto를 사용하여 조건문에 적용해 보겠습니다:
local a = 1 ::label:: print("--- goto label ---) a = a+1 if a < 3 then goto label -- a 보다 작다 3 때는 레이블 label로 이동 end출력 결과는 다음과 같습니다:
--- goto label --- --- goto label ---
출력 결과에서 볼 수 있듯이, 추가로 출력되었습니다 --- goto label ---。
다음 예제에서는 레이블에 여러 문장을 설정할 수를 보여줍니다:
i = 0 ::s1:: do print(i) i = i+1 end if i>3 then os.exit() -- i 보다 크다 3 시간이 지나면 탈출 end goto s1
출력 결과는 다음과 같습니다:
0 1 2 3
goto를 사용하면 continue 기능을 구현할 수 있습니다:
for i=1, 3 do if i <= 2 then print(i, "yes continue") goto continue end print(i, " no continue") ::continue:: print([[i'm end]]) end
출력 결과는 다음과 같습니다:
1 yes continue i'm end 2 yes continue i'm end 3 no continue i'm end