프로그래밍/Objective-C
if...else... 조건문 사용 예제 for Objective-C
Scripter
2012. 4. 29. 23:50
소스 파일명: testIfMain.m
- #import <stdio.h>
- #import <stdlib.h>
- // 사용법 표시 함수
- void printUsing() {
- printf("Using: testIf [number]\n");
- printf("This determines whether the number is positive or not.\n");
- }
- // main 함수
- int main(int argc, char *argv[]) {
- float val;
- if (argc != 2) {
- printUsing();
- exit(1);
- return 1;
- }
- // 명령행 인자의 스트링을 가져와서
- // 배정밀도 부동소수점수로 변환하여
- // 변수 val에 저장한다.
- val = atof(argv[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;
- }
컴파일은 Dev-C++ 에서
실행> testIf
Using: testIf [number]
This determines whether the number is positive or not.
실행> testIf 1.234
1.234 is a positive number.
실행> testIf -1.234
-1.234 is a negative number.
실행> testIf 0
0 is zero.