While loop in Objective-C
Just like other programming languages Objective-C also supports while loop. While loop is a loop that is used to repeat a statement or a group of statements till the given condition is true. Every time while loop checks the condition before executing its body. Or we can say that we can use a while loop when we don’t know the exact number of iterations of the loop beforehand. While loop always works according to condition, if the given condition is true, then it executes its body otherwise not.
Syntax:
while(condition){
body
}
Here, the condition can be an expression, whose result can be true or false. And body contains either single or multiple statements.
Working of While loop
Following is the working of the while loop:
Step 1: Control encounter while loop.
Step 2: The flow jumps to the condition present in the while loop.
Step 3: Tested the condition:
- When the condition is true, the flow goes in the body of the while loop.
- When the condition is false, then the flow goes outside the while loop.
Step 4: Condition is true, so the controls flow inside the body of the while loop and execute all the statements.
Step 5: Updation takes place.
Step 6: Now controls flow back to step 2. This step is repeated till the condition is true.
Step 7: Condition is false, so the loop is ended and the control flow is gone outside the while loop.
Flow Chart
Following is the flow chart of the while loop:

Example 1:
ObjectiveC
// Objective-C program to demonstrate while loop #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init]; int num = 8; // while loop execution while (num < 10) { NSLog ( @"GeeksforGeeks\n" ); num++; } [pool drain]; return 0; } |
Output:
GeeksforGeeks GeeksforGeeks
The working of the above example is:
1. Program start.
2. num is initialized with 8.
3. Control enter into the while loop and check the condition (8 < 10) which is true.
4. So print “GeeksforGeeks”. And update the value of num by 1. so now num = 9.
5. Control again enters into the while loop and checks the condition (9 < 10) which is true.
6. So print “GeeksforGeeks”. And update the value of num by 1. so now num = 10.
7. Control again enters into the while loop and checks the condition (10 < 10) which is false.
8. Now control flow goes outside the while loop to return 0.
9. Program end.
Example 2:
ObjectiveC
// Objective-C program to demonstrate while loop #import <Foundation/Foundation.h> int main ( int argc, const char * argv[]) { NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init]; int num = 1; int res; // while loop execution while (num <= 15) { // Printing multiplication table of 15 till 15 res = 15 * num; NSLog ( @"15 * %d = %d\n" , num, res); num++; } [pool drain]; return 0; } |
Output:
15 * 1 = 15 15 * 2 = 30 15 * 3 = 45 15 * 4 = 60 15 * 5 = 75 15 * 6 = 90 15 * 7 = 105 15 * 8 = 120 15 * 9 = 135 15 * 10 = 150 15 * 11 = 165 15 * 12 = 180 15 * 13 = 195 15 * 14 = 210 15 * 15 = 225