Header Ads

Loops - Objective C

There may be situation,when you need to execute a block of code several number of times. In general statement are executed sequentially: The first statement in a function is executed first, followed by the second  and so on.

Programming language provides various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statement multiple times and  following is the general form of a loop statement in the most of the programming language:

Objective-C programming language provides the following types of loop to handle looping requirements.


Loop Types :
  • While loop : A while loop statement in Objective C programing language repeatedly executes a target statement as long as a given condition is true.
  • For loop : A for loop is a repetition control structure that allow you to efficiently write a loop that needs to execute a specific number of times.
  • Do..while loop : Unlike for and while loops,which test the loop condition at the top of the loop, the do...while loop in objective C programming language checks its condition at the bottom of the loop.
  • Nested loop : Objective C programming language allows to use one loop inside another loops.


While loop
                A while loop statement in Objective C programing language repeatedly executes a target statement as long as a given condition is true.

Syntax:
           while(condtion)
           {
                        statement (s);
           }


Here statement may be a single statement or block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Key point of the while loop is that  the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example

#import<Foundation/Foundation.h>
int main()
{
         int a = 10;
         while (a<20)
         {
             NSLog(@"value of a: %d \n", a);
             a++;
         }
         return 0;
}

For Loop
A for loop is a repetition control structure that allow you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

       for( init ; condition ; increment)
       {
            statement(s);
       }

Here is the flow of control in a for loop:
  • Then init step is executed first, and only once. This step allows you to declare and initalize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and floe of control jumps to the next statement just after the for loop.
  • After the body of the for loop executes, the flow of control jump back up to the increment  statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself. After the condition becomes false, the for loop terminates.
Example

#import<Foundation/Foundation.h>
int main()
{
         int a = 10;
         for (a=10; a<20; a++)
         {
             NSLog(@"value of a: %d \n", a);
            
         }
         return 0;
}


Do----While loop
Unlike for and while loops,which test the loop condition at the top of the loop, the do...while loop in objective C programming language checks its condition at the bottom of the loop.

A do....while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax;
           do{
                 statement (s);
               }
               while(condition);


Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back to do and the statement(s) in the loop execute again. The process repeats until the given condition become false.

program

#import<Foundation/Foundation.h>
int main()
{
         int a = 10;
        do
         {
             NSLog(@"value of a: %d \n", a);
             a++;

         }while(a<20);

         return 0;
}


Nested Loops
Objective C programming language allows to use one loop inside another loops. Following section shows few example to illustrate the concept

The nested for loop syntax:

Syntax:
          for(init ; condition ; increment)
          {       
                 for( init ; condition ; increment )
                 {
                      statement(s);
                 }
                 statement(s);
         }


The nested while loop syntax:
Syntax: 
          while(condition )
          {       
                 while(condition )
                 {
                      statement(s);
                 }
                 statement(s);
         }

The nested do...while loop syntax
Syntax:
          do
          {       
                 do
                 {
                            statement(s);

                 }while(condtion);

                statement(s);

          }while(condition);



A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa

Example
 The following program uses a nested for loop to find the prime numbers from 2 to 100.

#import<Foundation/Foundation.h>
int main()
{
         int i,j;
          for(i=2 ; i<100; i++)
          {       
                 for( j=2; j<100; j++ )
                 {
                      if (!(i%j))
                      {
                                break;
                       }
                  }
                 if( j>(i/j))
                 {
                      NSLog(@"%d  is prime \n", i);
                 }     
         }
       return 0;
}



No comments:

Powered by Blogger.