Java – Jump Statements

break statement

In several instances, such as an error or incorrect input from the user, it may be necessary for the flow of control to exit from a loop statement. To accomplish this, Java provides a jump statement called break statement.

The break statement forces the control to come out from a switch statement, a while loop, a do-while loop or a for loop. After the control moves out of a switch statement or any loop statement, it moves to the statement after the loop or switch statement.

SYNTAX:

break;

The break statement is written as break; without any expression.

Example

Consider a program that prints the value stored in a variable until the specified condition is false. The program ends when the value becomes equal to the value stored in another variable.

'break' statement example

The break statement has been used to enable the program to end when the value in the i variable becoes equal to the value in the num variable. If the value in the i variable is less than 10, the flow of control enters the for loop. When the code is executed, the program displays the numbers from 0 to 3.

Labelled Brake statement

In a nested loop, the break statement used in an inner loop transfers the flow of control to the immediate outer loop. In certain situations, you may also want the flow of control to exit the outer loop. In this case, you use the break statement with the label identifier.

SYNTAX:

break label_name;

The label identifier is used to group a loop statement that contains another loop statement or a switch statement and to specify the name for the group. The label identifier is specified just before a loop statement without any expression like ‘label_name: ‘.

Example

label1:

For example, to name the loop statement label1, you write label1: in the line preceding the loop statement.

When the brake statement is used with the label_name, the flow of control is transferred to the statement after the labeled loop.

Labelled Brake Statement - Example

Consider the code displayed above. that displays the value stored in variable ‘i’ until the condition specified in either of the for loops becomes false. The program terminates if the value of ‘i’ becomes equal to that of ‘num’. In the code shown above, a label statement, ‘label1:’ has been used to group the two for loops.

When the value of the ‘i’ variable becomes equal to the value of the ‘num’ variable, the program executes the labeled brake statement. This transfers the flow of control outside label1. Therefore, the print statement outside the nested for statement is not executed.

HINT: You cannot use the break statement without a switch statement or a loop statement.

Advertisements

11 responses to “Java – Jump Statements

  1. some of the examples are missing.
    the gif url are not there
    “http://image59.webshots.com/659/0/61/13/2081061130098576485xmpYmv_ph.jpg”

    the page is : https://idynsolutions.wordpress.com/2007/01/22/java-jump-statements/

    FYI

  2. @natan – Try reloading the page..because the images are available..but sometimes it does’nt load properly. try to access the images seperately and come back to the post..

  3. All the Examples are missing.
    Though its very easy to undersatand, its better to have examples to get the concepts clearly .

  4. John Turion

    Can someone help me i need a wireless data collection software that will enable me to make custom forms on my Blackberry or Motorola phone. The form have to support drop down menus, check boxes, support GPS, maybe bar coding and photo capture. Anyone knows where I can such a wireless data collection company like this?

  5. Angela Rodriguez

    I have been working in marketing industry for about 5 years and we found a solution that does this. We use it for wireless bar code reading and GPS tracking. The best part is we can customize the forms on-the-fly which saves us a lot of time over a customized solution. The company that makes the product is Westlake out of LA I think. There website is http://www.westlakesoftware.com. Their product is call AirMobility.net. They made a custom Extranet for us for our company to use but their main product site is http://www.airmobility.net. We found this service set up to make it pretty easy for us to migrate the information to our back office system. We use the technology and route it to 40 different servers nationally; has worked flawlessly for about three years. I am note sure if this will match your exact needs but it is what we have been using and it has been working pretty well for us.
    Angela Rodriguez, VP-IT

  6. Any one tel me that How can I put the label on the set of statement should be repeats if the problem occurs.

    It mean I don’t use loop, but the if there any exception kept, that time it should goes to satrting the code reading input.

  7. raghavendra

    pictures are missing

  8. @Arcadi: Nausea da… lavoro. A fine giornata ho la crisi di rigetto!!! Click http://www.l33turl.com/monday091730

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s