프로그래밍/Lua
if...else... 조건문 사용 예제 for Lua
Scripter
2008. 2. 19. 17:33
소스 파일명: testIf.lua
- --[[
- Filename: testIf.lua
- Purpose: Example using the conditional control structure syntax
- if .... else ...
- Execute: lua testIf.lua [number]
- --]]
- -- 사용법을 보여주는 함수
- function printUsing()
- print("Using: ruby testIf.rb [number]")
- print("This determines whether the number is positive or not.")
- end
- -- 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
- if #arg ~= 1 then
- printUsing()
- os.exit(1)
- end
- -- 명령행 인자의 스트링을 가져와서
- -- 배정밀도 부동소수점수로 변환하여
- -- 변수 val에 저장한다.
- val = tonumber(arg[1])
- -- 변수 val에 저장된 값이 양수인지 음수인지 0인지를
- -- 판단하는 if...else... 조건문
- if (val > 0.0) then
- print(val .. " is a positive number.")
- elseif (val < 0.0) then
- print(val .. " is a negative number.")
- else
- print(val .. " is zero.")
- end
실행> lua testIf.lua
Using: lua testIf.lua [number]
This determines whether the number is positive or not.
실행> lua testIf.lua 1.21
1.21 is a positive number.
실행> lua testIf.lua -1.21
-1.21 is a negative number.
실행> lua testIf.lua 0
0 is zero.
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.