Comparing break with break <return value>

A break statement of the break <return value> form is referred to as an extended break statement.

A traditional switch construct uses a break statement without any return values in its switch branches, to take control out of a switch construct. This also <indexentry content="break:comparing, with break ">prevents fall through the control <indexentry content="break :comparing, with break">across multiple switch branches. A switch expression uses a break statement with a return value and breaks out of switch expressions.

Let's compare the break statement with the return statement, which can be used with or without a value. In a method, you can use a return statement to return a value and exit a method or just exit a method without returning a value. Here's a quick example:

int sum(int x, int y) {                   // return type of method is 
// int int result = x + y; return result; // returns int value }
void output(List<Integer> list) {         // return type of method is 
// void
if (list == null)
return; // exit method without
// returning a value
else {
for (Integer i : list)
System.out.println(i);
}
System.out.println("End of method"); // this doesn't execute if
// list is null
}
 A switch expression uses break to return a value. A traditional switch construct uses a break statement to prevent the fall through of control across its case labels.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.70.21