Sunday, 15 November 2009

Jump Statements

As the name suggests , these are used to transfer control during program execution

Different Jump statements
1 break
2 continue
3 return

Lets look at each starting with break

break has 3 different uses

a To exit a switch block
As we have seen earlier, switch case block has different cases.
If we want that after execution of a particular case , following
cases should be skipped, use break before the closing brace

example
int i = 1 ;
switch (i)
{
case 1 : { System.out.println ("case 1") ; break ; }
case 2 : { System.out.println ("case 2") ; }
case 3 : { System.out.println ("case 3") ; }
}

Here, output would be case 1.
Because of use of break in case 1 , following two cases are skipped
and control comes out of switch block.

b To exit a loop
We have discussed different loops: for , while , do while
break can be used to exit any of these loops depending on
a condition or as such

int numbers [ ] = { 1,3,9,5,4,7,15} ;
for (int i = 0 ; i < numbers.length ; i ++)
{
if (numbers [i] % 2 == 0)
{
System.out.println ("Even number found .. exiting ") ;
break ; // skip rest of iterations
}
System.out.println ( numbers [i] ) ;
}
  • for loop moves through the array and checks if a number at any index is even (divisibility check) .
  • When the expression evaluates to true loop is exited using the break statement.
Similarly, it can be used with while and do while loops
c exiting a loop marked by a label
label can be any descriptive text other than language reserved keywords

outer : for ( int i = 0 ; i < 5 ; i ++)
{
inner : for ( int j = 0 ; j < 10 ; j ++)
{
// break ;
break outer ;
} // inner loop ends
} // outer loop ends

  • By default break exits from the innermost enclosing loop, in this case loop labelled inner.
  • But, if we want to exit outer loop (any block other then the enclosing), what we should do ? We can mark that block with a label and then use that label along with break.
break outer ;
Here, outer loop labelled outer is exited instead of the inner one and control comes to line following the end of this loop.
Remember : This form can be used to exit any block (not just loops) which encloses the break statement.
2 using continue
Sometimes, we need to just skip the current iteration of a loop instead of all the remaining as in break.
example
int numbers [ ] = { 1,3,9,5,4,7,15} ;
for (int i = 0 ; i < numbers.length ; i ++)
{
if (numbers [i] % 2 == 0)
{
System.out.println ("Even number found") ;
continue ; // skip current iteration
} // if block ends
System.out.println ( numbers [i] ) ;
} // for loop ends

Here, when an even number is found
continue ;
causes rest of the code (outside the if block) to be skipped, though for loop will continue to execute .
Same way it can be used with while and do while loops
3 returning from a function
  • function is a piece of reusable code
  • Sometimes whole of the code in a function need not be executed.
  • To get back from a function early, we use
return ;

example
public void choice ( int number)
{
if (number > 10)

{
return ; // skip rest of the code
} // if block ends
// do something if number is less then or equal to 10
} // function ends

If a condition is not satisfied , all the code need not be executed
which would avoid unnecessary processing and errors.