Thursday 7 March 2013

Transfer statements in Java

We are discussing control flow statements in Java and so far we have covered Selection and Loop statements. Lets go ahead and see Transfer statements.

Transfer statements are
  • continue
  • break
  • return
  • try-catch-finally

Continue statement

We use continue statement when we are in a loop and wish to skip some code execution based on certain criteria.Continue statement causes execution to resume at the top of the nearest enclosing loop.You use a continue statement when you do not want to execute the remaining statements in the loop, but you do not want to exit the loop itself.

Lets us take an example.Lets say we want to print even numbers from 0 to 10.Lets use for loop to achieve this.

for(int i=0;i<=10;i++)
{
     if(i%2!=0)
     {
           continue;
     }
     System.out.println(i + "\n");
}


Frankly the code is redundant and not the best way to print even numbers from 0 to 10 but will serve our purpose to understand continue statement.

Lets see what above code does.It's a for loop where variable i runs from 0 to 10.Inside the for loop we say if(i%2!=0) which means if remainder obtained by dividing i by 2 is not zero then just continue with the execution i.e skip rest of code in loop and start the loop again.However if the remainder is  0 then go ahead and print the number on standard output.

break statement

break statement is used when you want to stop the execution of the loop forcefully and come out.The break statement transfers control out of the enclosing loop ( for, while, do or switch statement).

Lets take the same example mentioned above to demonstrate this.We want to print even numbers in the range 0 to 10.Lets do it this time using a infinite while loop.
int i = 0;
while(true)
{
    if(i%2=0)
    {
         System.out.println(i + "\n);
    }
  if(i>10)
  {
      break;
   }
 i++;
}

Above code is very much similar to the code sample used to demonstrate continue statement.Above while loop is infinite and so we must stop the loop manually which in fact is done by break statement.Above code
starts with i=0 before the while loop begins and increments in each loop(i++). There is slight change is first part of the loop as compared to previous sample code.This program checks if remainder is zero and if it is then prints i to standard output but what is important is second part of the loop.As we keep on incrementing i when its value goes above 10 i.e 11 it will enter the second if statement and execute break.break will simply break from the loop and come out.

Note : Both continue and break statement are used only in loops.break is also used in switch statement.I have seen programmers doing silly mistake of using break and continue in "if" statements.

return statement

Return statement is very much simple. It will return the the execution control from the point where it is encountered to the point where the function containing the return statement is called.Lets take an example to understand this better.If you wish to return some value you can do even that but remember you can return one and only one variable from any function or module.

What if we have to return more than two variable from a function you might ask.Well there are ways to achieve that and we will cover that in some time. Lets take an example of return statement.
Class Greeting
    private String name;
    public void setName(String name)
    {
         this.name = name;
     }
     public String getName()
     {
          return(this.name);
      }
   public static void main(String args[])
  {
       Greeting greeting = new Greeting();
         greeting.setName("John");
         System.out.println(greeting.getName());
  }

}

Above code may look complicated but it isn't. Don;t worry about classes and object for now. We will see it later.Lets just concentrate on understanding return statement for now.

In above code we create a Greeting object called greeting. Set the name parameter to "John". When you call greeting.getName() it will call the getName() method in Greetings class which will then return the name variable which was set earlier.So "John" will be printed to standard output.

Note : Since name is defined using private access modifier you cannot access it using greeting.name. Refer Access modifiers to brush up access levels. Even if you are not getting this at this point don't worry we have lot of example and sample coming our way to demonstrate all these concepts again :).

try-catch-finally

This statement is used in exceptions so it is better we not cover it here otherwise it will lead to unnecessary confusions.Just keep in mind at this point that this is a transfer statement and is used to handle exceptions.


Note : Java is a case sensitive language. So take care of your cases. Also an important thing to mention here is that all keywords in Java are lowercase. So these keywords break,continue,return,try,catch,finally are all lower in case. Also recall that we can't use keywords as variable names.



No comments:

Post a Comment

t> UA-39527780-1 back to top