Objective-C Programming For Dummies
Book image
Explore Book Buy On Amazon

In programming, as in life, you have to make decisions and act on them. Objective-C provides control statements and loops to help your program take action. You may want to repeat a set of instructions based on some condition or state, for example, or even change the program execution sequence. Here is the basic syntax for Objective-C control statements and loops.

if else

<b>if</b> (condition) {
  statement(s) if the condition is true;
  }
<b>else</b> {
  statement(s) if the condition is not true;
  }

for

<b>for</b> (counter; condition; update counter) {
  statement(s) to execute while the condition is true;
  }

for in

<b>for</b> (Type newVariable <b>in</b> expression ) {
<b> </b> statement(s); 
  }
or
Type existingVariable ;
for (existingVariable in expression) {
  statement(s);
  }
  • *Expression is an object that conforms to the NSFastEnumeration protocol.

  • An NSArray and NSSet enumeration is over content.

  • An NSDictionary enumeration is over keys.

  • An NSManagedObjectModel enumeration is over entities.

while

while (condition) { 
  statement(s) to execute while the condition is true 
  } 

do while

do {
  statement(s) to execute while the condition is true 
  } while (condition);

Jump statements

return ;

Stop execution and return to the calling function.

break;

Leave a loop.

continue;

Skip the rest of the loop and start the next iteration.

goto labelName;
...
labelName: 

An absolute jump to another point in the program (don't use it).

exit();

Terminates your program with an exit code.

About This Article

This article is from the book:

About the book author:

Neal Goldstein is a veteran programmer and trusted instructor of iOS programming topics. He is the author of all editions of iPhone Application Development For Dummies. Karl Kowalski is a programmer who specializes in security and mobile apps and author of Mac Application Development For Dummies.

This article can be found in the category: