Lec5 Mcas2130
Lec5 Mcas2130
UNIT II
Class and Methods
• jump: Java supports three jump statement: break, continue and return. These
three statements transfer control to other part of the program.
• Break: In Java, break is majorly used for:
• Terminate a sequence in a switch statement (discussed above).
• To exit a loop.
• Used as a “civilized” form of goto.
• Using break to exit a Loop
• Using break, we can force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body of the loop.
• Note: Break, when used inside a set of nested loops, will only break out of the
innermost loop.
•
• Continue:
• Sometimes it is useful to force an early iteration of a loop. That is, you might
want to continue running the loop but stop processing the remainder of the
code in its body for this particular iteration.
• This is, in effect, a goto just past the body of the loop, to the loop’s end. The
continue statement performs such an action.
• Return: The return statement is used to explicitly return from a method. That
is, it causes a program control to transfer back to the caller of the method.
•
References: