Sunday 3 March 2013

Loop Statements in Java

In last post we saw selection statements like if,if-else and switch. Now lets take a look at Loop statements also known as iteration statements.
So this category consist of  while, do while and for statements.Lets see each of them in detail.
  •  while loop - 

    Syntax for while statement is as follows -
    while(condition)
    {
        //Execute some code
    }
     Now lets see what above piece of code actually does.Code which is in the curly braces will keep on executing as long as condition is true.So there mush be some point in execution that the condition must go false only then the context comes out of the loop and proceeds with normal execution.If condition remains true forever the corresponding code will keep on executing unless you give a break statement.We will look into this break statement in detail in Transfer statements. We call this situation as infinite loop condition.Note  condition expression must evaluate to a boolean.

    Note : If you don't provide any curly braces first statement is taken as the code to be executed in while loop by default.If condition is false at the start of the loop, corresponding code will not be executed even once.
  •  do-while loop 

    Syntax for do while loop is as follows - 
    do{
    //Execute some code
    }while(condition)

    Above code will do the same thing as what while loop  does.It will keep on execution some code as long as  condition is evaluated to be true.

    So what is the difference between while and do-while loop?
    Answer is simple. If you observe in while loop we specify condition before the corresponding code whereas in do-while loop we specify it after the code.So if the condition is evaluated to be false while loop will not be executed whereas even if condition is evaluated to be false at the start of execution do-while loop will b executed once.So no matter what do-while loop will be executed at least once.
      
  •  For loop

    Syntax for for loop  is as follows - 
    for (<initialization>; <loop condition>; <increment expression>)
    {
         //Code to execute
    }
    Example
    for(int i=0;i<10;i++)
    {
        System.out.println(i + "\n");
    }
    Above code prints numbers from 0 to 9.Let us analyze it now.
    Inside you for loop first expression in initialization i.e you initialize some variables.Next expression is your looping condition i.e code in curly braces will be executed only when the looping condition is satisfied.Final expression is your increment expression where you increment or decrement variable so as at some point in execution of loop looping condition will not be satisfied and the loop will be terminated.In above code variable i of type integer.i=0 assigns value 0 to variable i.Then for each value of i ranging from 0 to 9 it just prints the value on standard output.When i becomes 10 it checks if i i.e 10 is greater that 10.No i.e loop condition is not satisfies.Terminate the loop and come out.

    Note: initialization is done only once at the start of the for loop.Loop condition is checked every time before entering the loop to execute corresponding code and increment/decrement expression is executed every time after a loop code is executed.

    Another way to use for loop is as follows
    String[] vehicle={'Car','bike','truck'};
    for(String x : vehicle)
    {
        System.out.println(x);
    }

    Above code iterates over all values in array vehicle and prints them to standard output.x here is a local variable to the loop and you can name it anything you want of-course sticking to naming conventions and taking care of scope.Above code will print Car bike and truck to standard output.

    One more thing if you write the following

    for(;;)
    {
       //Code to execute
    }

    it is completely legal.This is nothing but an infinite loop.This is same as doing

    while(true)
    {
       //execute some code
    }

    Note: if you are a C user you might try something like 
    while(1)
    {
       //do something
    }
    but as i mentioned earlier condition must evaluate to be of type boolean.So above code will throw an compile time error.So you need to use true instead of 1 to achieve infinite loop.

    In next post we will see transfer statements like break,continue etc.
     

     

No comments:

Post a Comment

t> UA-39527780-1 back to top