Saturday 21 February 2015

Jump statements in Java

Jump statements are the statements which transfers the control to another part of the program. There are 3 types of jump statements
1)break
2)continue
3)return

1)Using break statements
There are mainly 3 uses of break statement
           * It terminates a sequence in a switch statements.
           * It can be used to exit a loop
           * It can be used the civilized form of GO TO.

Example for exit a loop :


Output:









Example for form of Go TO:
Syntax:
break label;

where label is the name given to a block of code.



Output:







Using Continue:
In the iteration statement you can come up with a scenario as you need to continue with next iteration without executing the remaining statements in the loop body .In such scenario continue can be used.

Example:


Output:














 return statement:
The return statement is used to explicitly return from a method. It directly returns the control to the caller of the method.
We will discuss later about this with more examples.

No comments: