Open In App

Do While loop in Objective-C

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Just like other programming languages Objective-C also supports do..while loop. do..while loop is also known as an inverted while loop because, in a while loop, the expression is evaluated before executing the code present inside the body of the while loop, if the expression is false, then this loop doesn’t execute the body of the while loop. Whereas in the do..while loop, the expression is evaluated at the end of the loop body. In the do..while loop, the body of the loop will execute at least once irrespective of the test expression. 

Syntax:

do

{

    // Loop Body

    // Update Statement

}while(expression)

do..while loop contains the following parts:

  • Loop Body: The loop body is a collection of statements such as variables, expressions, functions, etc. 
  • Update Statement: This statement works after executing the loop body. It updates the loop variable either by incrementing or decrementing its value.
  • Expression: It is a test condition. If the value of the expression is true, then the body of the loop will execute and update the value of the update statement. If the value of the expression is false, then the controls come out from the do..while loop. 

Working of the do..while loop

The following steps will show the working of the do..while loop:

Step 1: Control encounter do..while loop. 

Step 2: Execute the statements present inside the do..while loop.

Step 3: Updation takes place.

Step 4: Controls reach the test expression.

Step 5: Test the given expression:

  1. If the expression returns true, the control flow goes back to Step 2. 
  2. If the expression returns false, the control flow goes outside the do..while loop.

Step 6: The do..while loop has been ended and the control flow has gone outside the loop.

Flow chart

Following is the flow chart of do..while loop:

do...while flow chart

 

Example 1:

ObjectiveC




// Objective-C program to demonstrate do..while loop
#import <Foundation/Foundation.h>
  
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      
    int num = 8;
      
    // Do while loop
    do{
      
        // Body of the loop
        NSLog(@"GeeksforGeeks\n");
          
        // Update statement
        num++;
    }
      
    // Test condition
    while (num < 10);
      
    [pool drain];
    return 0;
}


Output:

GeeksforGeeks
GeeksforGeeks

Here, in the above program, the num is initialized with 8. Now control enters into the do..while loop and print “GeeksforGeeks”. Also, update num = 9. Now check the condition present in the while(num < 10) statement. Here the condition is true and controls again enter into the loop and print “GeeksforGeeks” and update num = 10. Now again check the condition(num < 10). This time condition is false and control comes out from the loop.  

Example 2:

ObjectiveC




// Objective-C program to demonstrate do..while loop
#import <Foundation/Foundation.h>
  
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      
    int num = 1;
    int res;
  
    // Printing multiplication table of 16 till 16
    // Using do..while loop
    do{
      
        // Loop body
        res = 16 * num;
        NSLog(@"16 * %d = %d\n", num, res);
          
        // Update statement
        num++;
          
    }while(num <= 16);
      
    [pool drain];
    return 0;
}


Output:

16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160
16 * 11 = 176
16 * 12 = 192
16 * 13 = 208
16 * 14 = 224
16 * 15 = 240
16 * 16 = 256
 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads