<< 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.

Questions & Answers

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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