Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA PROGRAMMING

Jump Statements ( break, continue, return)

The break statement 

The break statement has two forms: unlabelled and labeled. You saw the break statement in action within the switch statement earlier. As noted there, break terminates enclosing the switch statement and flow of control transfers to the statement immediately following the switch. You can also use the unlabelled form of the break statement to terminate a for, while, or do-while loop. 

An example code using unlabeled break





While, the unlabelled form of the break statement is used to terminate the innermost switch, for, while, or do-while, the labeled form terminates an outer statement, which is identified by the label specified in the break statement.

An example code using labeled break





The continue statement

You use the continue statement to skip the current iteration of a for, while , or do-while loop. The unlabelled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop, basically skipping the remainder of this iteration of the loop. 

The  return statement

The last of Java's branching statements is the return statement. You use return to exit from the current method and jump back to the statement within the calling method that follows the original method call. There are two forms of return: one that returns a value and one that doesn't. To return a value, simply put the value (or an expression that calculates the value after the return keyword: 
return ++count;

The value returned by return must match the type of method's declared return value. 
When a method is declared void use the form of return that doesn't return a value: 

return;


Previous

Next