34,333
edits
Changes
no edit summary
Having created a loop it is possible that under certain conditions you might want to break out of the loop before the completion criteria have been met (particularly if you have created an infinite loop). One such example might involve continually checking for activity on a network socket. Once activity has been detected it will be necessary to break out of the monitoring loop and perform some other task.
For the purpose of breaking out of a loop , Objective-C# provides the ''break'' statement which breaks out of the current loop and resumes execution at the code directly after the loop. For example:
<pre>
NSLog (@"j = %i", j);
}
</pre>
The above example will loop 100 times displaying the value of i on each iteration. In addition, for each of those iterations it will loop 10 times displaying the value of j.
== Breaking From from Nested Loops ==
An important point to be aware of when breaking out of a nested for loop is that the break only exits from the current level of loop. For example, the following Objective-C code example will exit from the current iteration of the nested loop when j is equal to 5. The outer loop will, however, continue to iterate and, in turn execute the nested loop:
</pre>
== Using for Loops with Multiple Variables ==
In the examples we have covered so far we have used a single variable within the ''for'' loop construct. Objective-C actually permits multiple variables to be modified within the looping process. In the following example, the for loop increments two variables, i and j