Difference between revisions of "Objective-C Looping with do and while Statements"

From Techotopia
Jump to: navigation, search
m (Text replacement - "<google>BUY_OBJC_BOTTOM</google>" to "<htmlet>objc</htmlet>")
m (Text replacement - "<table border="0" cellspacing="0" width="100%">" to "<table border="0" cellspacing="0">")
Line 1: Line 1:
<table border="0" cellspacing="0" width="100%">
+
<table border="0" cellspacing="0">
 
<tr>
 
<tr>
 
<td width="20%">[[Objective-C Looping - The for Statement|Previous]]<td align="center">[[Objective-C 2.0 Essentials|Table of Contents]]<td width="20%" align="right">[[An Overview of Objective-C Object Oriented Programming|Next]]</td>
 
<td width="20%">[[Objective-C Looping - The for Statement|Previous]]<td align="center">[[Objective-C 2.0 Essentials|Table of Contents]]<td width="20%" align="right">[[An Overview of Objective-C Object Oriented Programming|Next]]</td>
Line 114: Line 114:
  
 
<hr>
 
<hr>
<table border="0" cellspacing="0" width="100%">
+
<table border="0" cellspacing="0">
 
<tr>
 
<tr>
 
<td width="20%">[[Objective-C Looping - The for Statement|Previous]]<td align="center">[[Objective-C 2.0 Essentials|Table of Contents]]<td width="20%" align="right">[[An Overview of Objective-C Object Oriented Programming|Next]]</td>
 
<td width="20%">[[Objective-C Looping - The for Statement|Previous]]<td align="center">[[Objective-C 2.0 Essentials|Table of Contents]]<td width="20%" align="right">[[An Overview of Objective-C Object Oriented Programming|Next]]</td>

Revision as of 14:42, 5 May 2016

PreviousTable of ContentsNext
Objective-C Looping - The for StatementAn Overview of Objective-C Object Oriented Programming

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print

The Objective-C for loop described previously works well when you know in advance how many times a particular task needs to be repeated in a program. There will, however, be instances where code needs to be repeated until a certain condition is met, with no way of knowing in advance how many repetitions are going to be needed to meet that criteria. To address this need, Objective-C provides the while loop (yet another construct inherited by Objective-C from the C Programming Language).


Contents


The Objective-C while Loop

Essentially, the while loop repeats a set of tasks until a specified condition is met. The while loop syntax is defined follows:


while (''condition'')
{
      // Objective-C statements go here
}

In the above syntax, condition is an expression that will return either true (1) or false (0) and the // Objective-C statements go here comment represents the code to be executed while the condition expression is true. For example:

int myCount = 0;

while ( myCount < 100 )
{
      myCount++;
}

In the above example, the while expression will evaluate whether the myCount variable is less than 100. If it is already greater than 100 the code in the braces is skipped and the loop exits without performing any tasks.

If, on the other hand, myCount is not greater than 100 the code in the braces is executed and the loop returns to the while statement and repeats the evaluation of myCount. This process repeats until the value of myCount is greater than 100, at which point the loop exits.

Objective-C do ... while loops

It is often helpful to think of the do ... while loop as an inverted while loop. The while loop evaluates an expression before executing the code contained in the body of the loop. If the expression evaluates to false on the first check then the code is not executed. The do .. while loop, on the other hand, is provided for situations where you know that the code contained in the body of the loop will always need to be executed at least once. For example, you may want to keep stepping through the items in an array until a specific item is found. You know that you have to at least check the first item in the array to have any hope of finding the entry you need. The syntax for the do ... while loop is as follows:

do
{
       // Objective-C statements here
} while (''conditional expression'')

In the do ... while example below the loop will continue until the value of a variable named i equals 0:

int i = 10;
do
{
       i--;
} while (i > 0)

Breaking from Loops

As with the for loop, it is also possible to exit from a while or do while loop at any time through the use of the break statement. When the execution path encounters a break statement the looping will stop and execution will proceed to the code immediately following the loop. In the following example, the loop is coded to exit when the value of i matches the value of j:

int i = 0;

int j = 5;

while (i < 100)
{
     i++;

     if (i == j)
           break;
}

<google>IOSBOX</google> It is important to note that in the case of nested loops the break statement only exits the current loop leaving the outer loop to continue executing (and most likely once again executing the inner loop).

The continue Statement

The continue statement causes all remaining code statements in a loop to be skipped, and execution to be returned to the top of the loop. In the following example, the NSLog function is only called when the value of variable i is an even number (i.e divisible by 2 with no remainder):

int i = 1;

while (i < 20)
{
        i++;
 	if ((i % 2) != 0)
		continue;

	NSLog (@"i = %i", i);	
}

The continue statement in the above example will cause the NSLog call to be skipped unless the value of i can be divided by 2 with no remainder. If the continue statement is triggered, execution will skip to the top of the while loop and the statements in the body of the loop will be repeated (until the value of i exceeds 19), resulting in the following output:

i = 2
i = 4
i = 6
i = 8
i = 10
i = 12
i = 14
i = 16
i = 18
i = 20

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print


PreviousTable of ContentsNext
Objective-C Looping - The for StatementAn Overview of Objective-C Object Oriented Programming