iPhone Development: Objective C Pt 4

Objective C LoopingLooping, Decision Making & Arithmetic Operation’s

In this article, I’ll review how to perform action’s repetitively with looping. I’ll then cover how to program decision making into your Objective C code. Finally, I’ll go over all of the arithmetic operation’s that are built into Objective C.

Looping

Every modern day programming language provides a way for you to perform action’s through looping. They also do it in very similar ways. The most common looping tool is called the for loop.

The For Loop

Let’s say you wanted a program that printed out the numbers 1 to 100. If so, you are in luck because I made one just for you, using the for loop.

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int i;

for (i = 1; i <= 100; i += 1)
{
NSLog (@”%i \n”, i );
}

[pool drain]
return 0;
}

I’ve gone over most of this code in previous tutorial’s so I’ll just explain how the for loop works. The basic format of a for loop looks like this:

for ( initial variable; lopping condition; iterator )
{
code…
}

After the for keyword you define the starting value for the variable that you are going to iterate, after the opening brace (. Then you define under what condition’s you will continue looping. To finish off this loop you define by how much you will increment the variable.

In the example above, I’m stating I want to continue looping while i is less than or equal to 100. The code that will be executed lies between the curly braces. In this case I’m printing the value of I to the screen. By including the newline statement \n, each number will be printed on individual line’s.
All the Relational Operator’s

Here are the different ways you can compare value’s in Objective C:

  • > : Greater than
  • >= : Greater than or Equal to
  • < : Less than
  • <= : Less than or Equal to
  • == : Equal to
  • != : Not Equal to

Logical Operator’s

You can create even more complicated comparison statement’s by using a logical operator. If we wanted to loop as long as i is less than or equal to 100 and i is not equal to 50, do this:

(( i <= 100 ) && ( i != 50 ))

Here are the other logical operator’s:

  • && : And
  • || : Or
  • ! : Not

Explaining the Iterator

You might also be wondering what this statement means: i += 1. It means that you want the current value of i to be added to 1 and then reassigned to the variable i. Here’s another shorthand way to do the same thing i++.

You could use the same shorthand code with -, *, and /.

Random Thing’s About For

You don’t have to stick to this for statement definition exactly. If you want to define the iterated variable outside of the for statement, the for statement would look like this:

for ( ; i <= 100; i += 1)

You could also define multiple variable’s in the for loop like this:

for ( i = 1, x = 1; i <= 100; i++ )

The While Loop

Another tool available for looping is the while loop. It is used, when your initial variable, lopping condition variables, or iterator variable are different from each other. It’s basic syntax is:

while ( looping condition )
{
code…
iterator;
}

Here is how you would perform the same act with the while loop, as you did with for:

int i = 1;
while( i <= 100 )
{
NSLog (@”%i \n”, i );
i++;
}

The Do While Loop

If you want to make sure the code in the body of the loop is executed at least once, use the do while loop. It follow’s this syntax:

do{
code…
iterator;
}
while( condition );

Make sure you don’t forget to put a semicolon after the while statement. This is a common error. Here I’ll use the do while statement, to perform the same loop as above:

int i = 1;
do {
NSLog (@”%i \n”, i );
i++;
}
while ( i <= 100 );

The Break Statement

If you ever want to immediately leave the loop, when a certain condition is met, use the break statement. It look’s like this ( break; ). Remember that if you nest loop’s inside of each other, that break will only jump you out of the current loop it is in.

The Continue Statement

The continue statement ( continue; ), will jump you back to the beginning of the loop. All statement’s that follow it in the loop are skipped. It is used to by-pass certain statement’s under certain condition’s.

Decision Making with Objective C

There are three tool’s available to you for making decision’s in Objective C:

  • If
  • Switch
  • The Conditional Operator

The If Statement

The most common way to make decision’s, is through the use of an if statement. It’s syntax is:

if ( conditional )
{
Code…
}

You can also use it with the else statement, to perform secondary action’s:

if ( conditional )
{
Code…
} else {
Code…
}

Or, you can use compounded if check’s:

if ( conditional )
{
Code…
}
else
if ( conditional )
{
Code…
} else {
Code…
}

Let’s check if someone can legally drink, vote, and drive:

if ( age > 21 )
{
NSLog (@”You can Drink, Drive and Vote \n” );
} else if ( age > 18 )
{
NSLog (@”You can Drive and Vote \n” );
}else if ( age > 16 )
{
NSLog (@”You can Drive \n” );
} else
{
NSLog (@”You can’t do anything \n” );
}

The Switch Statement

The Switch statement is commonly used for decision making, when you know ahead of time, the possible values. Here is it’s basic syntax:

switch ( variable )
{
case value1:
Code…
break;

case value2:
Code…
break;

default:
Code…
break;
}

Here I’ll show a real world switch:

switch ( numberEntered )
{
case 0:
NSLog (@”You entered a Zero \n” );
break;

case 1:
NSLog (@”You entered a One \n” );
break;

case 2:
case 3:
NSLog (@”You entered a Two or Three \n” );
break;

default:
NSLog (@”You entered a number bigger than Three \n” );
break;
}

The Conditional Operator

There is a shorthand way to perform decision’s in Objective C, and it’s call the Conditional Operator. It’s syntax is:

variable = ( condition ) ? Assign Value1 : Assign Value2;

It assigns one value if the condition is true and the other if it was false. Here is an example:

personSex = ( sex == ‘m’ ) ? ‘Male’ : ‘Female’;

Arithmetic Operation’s

Above, I already showed you the shorthand way to perform arithmetic in Objective C. If you have any experience in programming, I bet you already know how to perform artithmetic, but probably not casting. Just in case:

  • + : Is used to add 2 values like this, z= x + y
  • – : Is used to subtract values like this, z= x – y
  • * : Is used to multiply values like this, z= x * y
  • / : Is used to divide values like this, z= x / y
  • % : Is called the modulus, it returns the remainder of a division like this 5 % 2 = 1

Casting Operator

If you want to convert an integer into a double, or what have you. You need the casting operator. Here are some example’s:

intNumber = (int) (1.23 * 4.56); /* Perform’s the multiplication, then turns the result into an int */
doubleNumber = (int) 1.23 * 4.56; /* Converts 1.23 into 1, then performs the multiplication. doubleNumber would be assigned the value of 4.56 */

That’s All Folk’s

That is it for now. In the next article, I’ll completely cover Object Oriented Programming in relation to Objective C. If you have any question’s leave them below.

Here to Serve

Think Tank

Leave a Reply

Your email address will not be published.

Google+