Objective C Conditional Statements by Example
If statement in Objective C
Example for simple if statement is shown below.
int main(int argc, const char * argv[])
{
@autoreleasepool {
int a = 3;
if(a > 2){
NSLog(@"Greater than 2");
}
}
return 0;
}
If Else statement in Objective C
Example for simple if else statement is shown below.
int main(int argc, const char * argv[])
{
@autoreleasepool {
int a = 3;
if(a > 2){
NSLog(@"Greater than 2");
}
else{
NSLog(@"Lesser than or equal to 2");
}
}
return 0;
}
Else If ladder in Objective C
Example for else if ladder is shown below.
int main(int argc, const char * argv[])
{
@autoreleasepool {
int a = 3;
if(a == 2){
NSLog(@"Is equal to 2");
}
else if(a > 2){
NSLog(@"Greater than 2");
}
else if(a< 2){
NSLog(@"Lesser than or equal to 2");
}
}
return 0;
}
Switch statement in Objective C
Example for switch statement is shown below.
int main(int argc, const char * argv[])
{
@autoreleasepool {
int a = 3;
switch(a)
{
case 1:
{
NSLog(@"Is equal to 1");
}
break;
case 2:
{
NSLog(@"Is equal to 2");
}
break;
default:
{
NSLog(@"Some other number");
}
}
}
return 0;
}