Jumping around - break, continue, return


Premium Content - Free Preview

Code is usually executed linearly or in loops. However, sometimes you may want the program to jump to a different spot of code. This could create very confusing code if unrestricted, so Java limits jumping to special cases.

break

The previous node mentioned how the break keyword is used to exit from a switch statement. It can also be used to exit from any loop (for, while and do-while). When used in a loop, the program will break out of the loop and continue executing code from after the loop block. Here's a simple example:

for(int i=0; i<100; i=i+1) {
  if(i == 7){
    break;
  }
  System.out.println(i);
}
System.out.println("finished");

This code will output the following:

0
1
2
3
4
5
6
finished

When i equals 7, the program breaks out of the loop, so no further numbers get printed. This code is a simple demonstration of break, but would not make sense to use in practice, since the condition of the loop can just be changed to i<7 instead of i<100. break should instead be used when there's a special circumstance not covered by the main condition of a loop. For example, it could be used to find the position of a number in an array:

public static void findNum(int[] array, int num)
{
    int i;
    boolean isThere = false;

    for (i = 0; i < array.length; i=i+1) {
        if (array[i] == num) {
            isThere = true;
            break;
        }
    }

    if (isThere) {
        System.out.println(num + " is at index " + i);
    } else {
        System.out.println(num + " isn't in array");
    }
}

The above code goes through array and stops when it reaches num. It then prints out the location of num.

The following code calls the above method to find 30 in an array:

int[] ar = {0,10,20,30,40};
findNum(ar, 30);

It will output:

30 is at index 3

If it passed in a number not in the array:

 findNum(ar, 50);

it will output:

50 isn't in array

As long as num hasn't been found, the loop needs to finish searching through array. However, one the number is found, there's no reason to continue searching, so it makes sense to use break.

labels and nested loops

When break is used in nested loops, it will exit from the innermost loop. In some cases, you may wish to jump to the outside of an outer loop.

Extra - Breaking out of Specifc Loops

End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • The example doesn't work. Input 2 gives a sum of 22, so shouldn't go back to 0, input 4 sums to 4 and again shouldn't be zero'd.

  • @Shane, those hands contain odd multiples of 3, which cause the score to be 0. I added some explanation above.

  • How do I resolve this?

    for (int i=1; i<cards.length; i++) {
    if (i%2!==0 && i%3!==0) {
    System.out.println(sum +=i);
    }
    }
    }

  • is there any hint? I'm stuck :(

  • static int doStuff(int[] cards){
    for (int i=1; i<cards.length; i++) {
    if (i%3==0) {
    break;}
    if (i%2!==0 && i%3!==0) {
    System.out.println(sum +=i);
    }
    System.out.println("0")

  • When you get a multiple of 3 remember that the whole hand gets to 0 no matter the cards before or after. Use break in that case.

  • Why are there no awnsers anywere?

  • Here is the awnser i forgot sum=0; before the break. devil in the details i guess.
    my code

  • OK I'm lost here...

    I think, according to the instructions my code is right. See the expected output. I think theres a mistake!
    Input Correct Output Your Output
    1 2 6 7 8 0
    1 2 4 5 7 9 0 0
    2 4 8 11 13 24 24
    1 2 3 4 0 0

    cont...
  • OK never mind LOOOOLL

Contact Us
Sign in or email us at [email protected]