반복문
while, do while, for, nested 전부 그대로 지원한다.
while(condition) {
statement(s);
}
do {
statement(s);
} while (condition);
for(init; condition; increment) {
statement(s);
}
//infinite loop
int main() {
for( ; ; ) {
NSLog(@"INFINITE LOOP\n");
}
//Never be executed..
return 0;
}
continue, break
#import <Foundation/Foundation.h>
int main() {
int a = 10;
while(a < 20) {
NSLog(@"value: %d\n",a);
a++;
if(a == 12) {
continue;
}
if (a > 15) {
break;
}
return 0;
}
조건문
Objective C에서 0이 아니거나, nil이 아닌 값은 true
0이나 null은 false로 간주된다.
(JS에서 truthy falthy 같은 느낌)
if - else if - else 구문
#import <Foundation/Foundation.h>
int main(void) {
int num1 = 0;
int num2 = 10;
NSString *str1 = nil;
NSString *str2 = @"Hello OBJC";
if(num1) {
NSLog(@"0 is false");
} else if (!num2) {
NSLog(@"10 is true");
} else if (str1) {
NSLog(@"nil is false");
} else if (str2) {
NSLog(str2);
}
}
nil
객체가 없음을 나타내는 특별한 '값' (null이라고 생각하면 됨)
모든 객체 타입 포인터에 할당할 수 있다. 포인터가 아무것도 가리키지 않는다!
switch - case
switch 조건문에는 정수 혹은 열거형이 들어가야한다.
break를 해주지 않으면 아래 case로 넘어가서 실행된다.(fall-through)
break를 잘 해주자.
int a = 2;
switch (a) {
case 1:
NSLog(@"a is 1");
break;
case 2:
NSLog(@"a is 2");
//fall-through
case 3:
NSLog(@"a is 3");
break;
default:
NSLog(@"DEFAULT");
break;
}
삼항 연산자
조건문 ? 참일 때 : 거짓일 때 ;
NSString *myString = nil;
myString = myString ? myString : @"DEFAULT";
NSLog(@"%@",myString);
'iOS Developer > Objective-C' 카테고리의 다른 글
8. Objective-C Blocks 블록 (0) | 2024.05.07 |
---|---|
7. Objective-C 함수(call by value, call by reference) (0) | 2024.05.06 |
4. Objective-C 변수/상수 (0) | 2024.05.06 |
3. Objective-C 자료형 (0) | 2024.05.05 |
2. Objective-C / 기본 구문 및 구조 (0) | 2024.05.05 |