- iPhone SDK: Objective-C Condition
- iPhone SDK Components & Requirements
- Overview of iOS Platform
- Objective-C Tutorial with xCode
- iPhone SDK: Objective-C Creating Custom Class
- iPhone SDK: Using Existing Objective-C Classes
- iPhone SDK: Your First iPhone Application
- iPhone SDK: MVC in iPhone Application Development
- iOS: Working with Actions & Outlets
Because of Objective-C is C with extra stuff added, we can use same if condition as in regular C Language. Let’s see an example of it. Create New Project in Xcode or use the previous one (Demo1) we created in last example. I’m using same one.
Objective-C If Condition
Step 1. Update the main method as follow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
int main (int argc, const char * argv[]) { @autoreleasepool { int productPrice = 3000; int discount = 10; //in percent int age = 40; int netAmount = productPrice; if(age >= 50) { NSLog(@"Senior Citizen Discount calculated"); netAmount = netAmount – ((float)discount/100)*productPrice; } NSLog(@"You have to pay %i to Purchase this item",netAmount); } return 0; } |
Now hit run and see the result it will be: You have to pay 3000 to purchase this item
Because we created a variable called age and we assign 40 to it and put a condition to check age. This is the basic if condition. That checks whether age is greater than or equal to 50? If yes then it enter in block and executes it which sets netAmount variable with discount substracted from productPrice. But in our case it’s not greater than or equal to 50 so will not execute that block and outputs the netAmount variable which is 3000. But set age variable to 50 or above and sure you will get output like this:
Senior Citizen discount calculated
You have to pay 2700 to purchase this item
Step 2. So far so good. Now change the main method again like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
int main (int argc, const char * argv[]) { @autoreleasepool { int productPrice = 3000; int discount = 10; //in percent int age = 40; int netAmount = productPrice; if(age >= 50 && productPrice >= 3000) { NSLog(@"Senior Citizen Large Discount calculated"); netAmount = netAmount – ((float)20/100)*productPrice; } else if(age >=50) { NSLog(@"Senior Citizen Mini Discount calculated"); netAmount = netAmount – ((float)discount/100)*productPrice; } else { NSLog(@"Normal Discount calculated"); netAmount = netAmount – ((float)5/100)*productPrice; } NSLog(@"You have to pay %i to Purchase this item",netAmount); } return 0; } |
In this code we used 2 conditions in if part for checking whether age is greater than or equal to 50 and productPrice is greater than or equal to 3000, then we consider 20% discount. But if not then we check for whether age greater than or equal to 50? If yes then we consider 10% discount. But if both conditions are not true then default 5% discount will be considered. For our case age is 40 so both conditions are false so result will be:
Normal Discount Calculated
You have to pay 2850 to purchase this item
But if we put age to 50 then we will get:
Senior Citizen Large Discount Calculated
You have to pay 2400 to purchase this item
Objective-C Switch Condition
Just like we use if condition, we can also use switch condition for some purposes. Let’s try in an example.
Step 1. Update the main method as follow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
int main (int argc, const char * argv[]) { @autoreleasepool { int status = 404; NSString *str = @""; switch (status) { case 100: str = @"Continue"; break; case 200: str = @"OK"; break; case 400: str = @"Bad Request"; break; case 401: str = @"Unauthorized Access"; break; case 404: str = @"Not Found"; break; default: str = @"Unknown Error Occured"; break; } NSLog(@"Error: %@",str); } return 0; } |
In this code we use NSString Class’s object str and assigned @”” which is blank string. Then check for the status match in switch condition when the break statement at the last of that case block will complete status match with any case that will be executed and switch. If no case matches then default case will be executed. In our case case 404 will be true so str object will get new string which is Not Found and switch will be terminated by the break statement. In NSLog statement we used %@ which is used for any class’s object and the result will be:
Error: Not Found
Note: As you can see we can use C Language conditions in Objective-C also. Same way we can also use loops & functions. I will not cover that topics separately here but we will see that in action while learning other Objective-C Concepts.