Objective-C: For loop

Iterations/Loops are used for repetition of codes. Simply, when a statement or set of statements is to be used repeatedly, iterations are used.

There are three types of loops that are generally used while programming namely,

  • for loop
  • while loop
  • do while loop

 

For loop

This loop repeats the statements a specific number of times by using a counter, till the condition is satisfied. Whenever condition is unsatisfied, the program counter jumps out of the loop.

Syntax:-

        for (initialization ; condition ; update counter)

        {

         …….

         ……. Statements;

         …….

        }

  Initialization: Firstly, counter of the loop is initialized here.

 Condition: After initializing, counter is evaluated for the condition, if satisfied, the statements execute and if not satisfied, loop terminates.      

Update counter: Once, statements of the loop are executed, the counter is updated and again the condition is evaluated, and the loop continues till the condition remains satisfied.

 Suppose, we need to print numbers for a specific range. We can either write individual statements for all those numbers or just perform the task smartly via using for loop. Let us see a short program using for loop.

 

Explanation

The statements executes as follows:

1.The counter i is initialized by 1.

2.Evaluate condition,   1<=5

[Condition: true, therefore, statement inside the loop is executed.]

The value of i that is 1, is printed on the console.

3.Now, the counter i is incremented by 1, and value of the counter becomes 2.

4.Again, condition is evaluated, 2<=5

[Condition: true, therefore, statement inside the loop is executed.]

The value of i that is 2, is printed on the console, and the counter is incremented by 1.

5.Again, condition is evaluated, 3<=5

[Condition: true, therefore, statement inside the loop is executed.]

The value of i that is 3, is printed on the console, and the counter is incremented by 1.

6.Again, condition is evaluated, 4<=5

[Condition: true, therefore, statement inside the loop is executed.]

The value of i that is 4, is printed on the console, and the counter is incremented by 1.

7.Again, condition is evaluated, 5<=5

[Condition: true, therefore, statement inside the loop is executed.]

The value of i that is 5, is printed on the console, and the counter is incremented by 1.

8.Again, condition is evaluated, 6<=5

[Condition: false, therefore, loop terminates, and program ends.]

 

We can initialize more than one value for the loop and can update both of them together, and, it is not always necessary to give values in all three fields of for loop, but it requires same syntax as shown below:

int main (int argc, const char *argv[])

      {

       int i=1;

       for( ; i<=5; )

       {

         NSLog(@“ %i”,i);

         i++;

       }

        return 0;

      }

The output of the above program will be same as the previous one.

While loop

This loop is similar to for loop except, it is used when only condition is required for executing the loop and the developer does not know how many times the loop will execute.

Note that all the program written using for loop can also be written using the while loop.

Syntax:-

        while ( condition)

        {

         …….

         ……. Statements;

         …….

        }

 Program for removing digits from a number one by one

 

Explanation

The number may be entered by the user at random but we have taken 500 for explaining the program. The output shows three values:

500

50

5

Each time a digit is removed from the number and the remaining number is printed on the console.

In situation, when limit of the loop is not pre-known, while loop are used.