프로그래밍/D
if ... else ... 조건문 사용 예제 for D
Scripter
2008. 3. 8. 17:35
D 언어의 if ... else ... 조건문 구문은 C/C++/Java/Groovy 의 것과 같다.
단, 한글이 포함된 소스파일은 UTF-8 인코딩으로 저장해야 한다.
또 C/C++ 언어에서 쓰이는 exit() 함수를 D 언어에서 쓰기 위해서는
import std.c.stdlib;
구문을 소스의 앞부분에 미리 써 놓아야 한다.
소스 파일명: testIfD.d
- import std.c.stdio; // printf 함수 사용을 위해
- import std.c.stdlib; // exit 함수 사용을 위해
- import std.conv; // to! 변환함수 사용을 위해
- // 사용법을 컨솔에 보여주는 함수
- void printUsing() {
- printf("Using: testIfD [number]\n");
- printf("This determines whether the number is positive or not.\n");
- }
- int main (string[] args) {
- float val;
- if (args.length != 2) {
- printUsing();
- exit(1);
- return 1;
- }
- // 명령행 인자의 스트링을 가져와서
- // 배정밀도 부동소수점수로 변환하여
- // 변수 val에 저장한다.
- val = to!(float)(args[1]);
- // 변수 val에 저장된 값이 양수인지 음수인지 0인지를
- // 판단하는 if...else... 조건문
- if (val > 0.0)
- printf("%g is a positive number.\n", val);
- else if (val < 0.0)
- printf("%g is a negative number.\n", val);
- else
- printf("%g is zero.\n", val);
- return 0;
- }
컴파일> dmd testIfD.d
실행> testIfD
Using: testIfD [number]
This determines whether the number is positive or not.
실행> testIfD 1.234
1.234 is a positive number.
실행> testIfD 1.234
-1.234 is a negative number.
실행> testIfD 0
0 is zero.