Advertisement

iOS

iPhone SDK: Objective-C Condition

Condition | Foundation | If | IOS | IPhone | Objective-C | Programming | Xcode

iPhone Application Development

Advertisement

This entry is part 1 of 9 in the series iPhone Application Development

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.

 

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

Objective-C Condition

Step 2. So far so good. Now change the main method again like this:

 

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

Objective-C Condition

 

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.

 

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. 

Series NavigationiPhone SDK Components & Requirements >>Overview of iOS Platform >>

Bookmark Link using: bookmark at folkd