Thursday, November 14, 2013

Loop in Java



A loop is a sequence of instructions which is continually repeated a part of a program until the given condition is satisfied. Often when the given condition failed to satisfy either intentional or due to programming error, the loop failed to stop and it is known as infinite loop or unconditional loop.

In java we have three types of Loop for, while, do-while.

For Loop

For loop is combined with three parameters, initialization of loop variable, final value of loop variable and increment or decrement value. 'For' is a entry controlled loop as condition checked at the entry point and also known as incremental loop.

Syntax:
     for(int i=1;i<=10;i++)

We also can have more than three parameters in for but there will be only a pair of semi-colon before final value and other separated by a comma.

Syntax:

     for(i=1,j=0;i<=10;j++,i++)

infinite loop :

     for(;;);

While Loop

While loop is a conditional loop and it is too is a entry controlled loop. The test condition given in a pair of bracket followed by 'while'. If any initial value for loop variable then it should be given before 'while' and the increment and decrement value should be inside body of the loop.

Syntax:

     int i=0;
     while(i<10)
        {
            //some code;
            i++;
        }

do - while Loop

While and do-while is almost same but do-while is a exit controlled loop as the condition tested at the end of the loop. Therefore, the do-while loop perform at least once no mater whatever is the condition.

Syntax:

     int i=0;
        do
            {
              //some code;
              i++;
            }while(i<10)

For Loop      While Loop   Do-while
for is an entry controlled loop and  is very similar to while loop except that the loop has three parameter for initialization of loop variable, the test condition and the incremental or decrement value. It is also known a incremental loop.
          While loop is a conditional loop and also an entry controlled loop.       Like while loop do-while loop too is a conditional loop but it is an exit-controlled loop.



Jump Statement

In Java we have three jump statements - break, continue, and return. These statements used to transfer control back to a desired part of the program.

Break

Break statement is used to jump out of current loop unless a label is defined next to it.  Break statement also used in switch – case statement.

Syntax:

In a Loop

     for(int i=1;i<=10;i++)
        {
            if(i==5)
              break;  //↓ transfer control out of I loop
        }              

Label

Outer:     //Label Declared
     for(int i=1;i<=10;i++)
        {
            for(j=0;j<=10;j++)
              {
                   if(j==5)
                             break Outer; // transfer control back to the outer loop                                          }
        }

In switch-case

     int c=1;
        switch(c)
        {
            case 1: //some code;
              break;
            case 2://some code;
              break;
        }

Continue

Continue statement is used only within looping statements.  Continue statement is forced the next iteration of the loop. The statements after continue within loop are skipped. Execution starts from the top of the loop.

Syntax

     int i=0;
     for(i=1;i<=10;i++)
     {
        if(i==5)
            continue;    // transfer control to next iteration i.e. i=6;↑
        System.out.println(i);
     }

Return

The return statement is used to return to parent method (caller) from the child (called) method. Return statement terminate executing the method is called and transfer back to the caller function and continue with the remaining statement in the caller function.

     void calledMethod()                            int calledMethod(int var)
        {                                                          {
            //some code;                                       //some code;
            return;                                                  return var;
        }                                                          }


void callerMethod()                                  Void callerMethod()
     {                                                             {
         calledMethod();                                        int var=calledMethod(var);
     }                                                             }

No comments: