Objective C Tutorial - Objective C while Loop






while loop repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

do...while loop is like a while statement, except that it tests the condition at the end of the loop body.

Syntax

The syntax of a while loop in Objective-C programming language is:

while(condition)
{
   statement(s);
}

statement(s) may be a single statement or a block of statements.

condition may be any expression, and true is any nonzero value.

The loop iterates while the condition is true.

The syntax of a do...while loop in Objective-C programming language is:

do
{
   statement(s);

}while( condition );




Example

#import <Foundation/Foundation.h>
 
int main ()
{
   int a = 10;

   while( a < 20 )
   {
      NSLog(@"value of a: %d\n", a);
      a++;
   }
 
   return 0;
}

The code above generates the following result.





Example 2

#import <Foundation/Foundation.h>
 
int main ()
{
   /* local variable definition */
   int a = 10;

   /* do loop execution */
   do
   {
       NSLog(@"value of a: %d\n", a);
       a = a + 1;
   }while( a < 20 );
 
   return 0;
}

The code above generates the following result.