<< Chapter < Page Chapter >> Page >
Listing 7 . The program named switch2.java.
/*File switch2.java This is a Java application which uses a labeled break.Note that the program uses nested switch statements. See switch1.java for a comparison program which does notuse a labeled break. The program displays the following output:Match and break from here Beyond switch statements**********************************************************/ class switch2 { //define the controlling classpublic static void main(String[] args){ //main methodouterSwitch: switch(5){//labeled outer switch statement case 5: //execute the following switch statement//Note that the code for this case is not followed by // break. Therefore, except for the labeled break at// case 1, execution would fall through the case 6 and // the default as demonstrated in the program named// switch1. However, the use of the labeled break // causes control to break all the way out of the// labeled switch bypassing case 6 and the default. switch(1){ //inner switch statementcase 1: System.out.println( "Match and break from here");break outerSwitch; //break with label case 2: System.out.println("No match for this constant"); break;}//end inner switch statement case 6: System.out.println("Case 6 in outer switch"); default: System.out.println("Default in outer switch");}//end outer switch statement System.out.println("Beyond switch statements");}//end main }//End switch1 class.

The modified program in Listing 7 uses a labeled break statement in the code group for case 1 whereas the original program in Listing 6 has an unlabeled break in that position.

By comparing the output from this program with the output from the previous program, you can see that execution of the labeled breakstatement caused control to break all the way out of the labeled switch statement completely bypassing case 6 and default.

As you can see from examining the output, the labeled break statement causes the program to break all the way out ofthe switch statement which bears a matching label.

A similar situation exists when a labeled break is used in nested loops with one of the enclosing outer loops being labeled. Control willbreak out of the enclosing loop to which the labeled break refers. It will be left as an exercise for the student to demonstrate this behavior tohis or her satisfaction.

Labeled continue statements

Now consider use of the labeled continue statement. A continue statement can only be used in a loop; it cannot be used in a switch. The behavior of a labeled continue statement can bedescribed as follows: "Terminate the current iteration and continue with the next iteration of the loop to which the label refers."

Again, it will be left as an exercise for the student to demonstrate this behavior to his or her satisfaction.

The return statement

Use of the return statement

Java supports the use of the return statement to terminate a method and (optionally) return a value to the calling method.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask