3

I have this code which generates random number from an array, at a particular condition (when x and y both are equal to zero). I want the control to jump to the label. But the control never jumps to the label in any condition. I wanted to know that whether I am doing it right or not?

int[] arr = {0, 1, 2};    
Random rn = new Random();
label: {
    //some code
    if (x != 0 && y !=0) {
        //some code
    } else {
        break label;
    }
}
informatik01
  • 16,038
  • 10
  • 74
  • 104
user2044187
  • 107
  • 1
  • 1
  • 10
  • @Luksprog: Thanks brother for editing, but the code is actually for android, for keeping it simple I removed that code and added comments in those places. – user2044187 Feb 12 '13 at 15:26
  • Don't you need `||` instead of `&&` in your `if`? I.e. `if (x != 0 || y != 0)`? – sp00m Feb 12 '13 at 15:31

5 Answers5

12

Without examining whether you should, yes, you can use labeled statements with if in Java. According to the 1.7 specification

The Identifier is declared to be the label of the immediately contained Statement. [...] identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement.

It goes on (emphasis added)

If the statement is labeled by an Identifier and the contained Statement completes abruptly because of a break with the same Identifier, then the labeled statement completes normally. In all other cases of abrupt completion of the Statement, the labeled statement completes abruptly for the same reason.

So if you break an if block (remember a block is a statement), you can exit the if body. Let's test it:

public static void main(String[] args) {
    if (true) label: {
        if (args != null)
            break label;
        System.out.println("doesn't get here");
    }
    System.out.println("Outside of labeled block");
}

Output:

Outside of labeled block
Papershine
  • 4,995
  • 2
  • 24
  • 48
rath
  • 3,655
  • 1
  • 40
  • 53
  • note that there is no 'continue', i.e. with an if-else chain, the break will resume execution after the entire chain – AlexO May 02 '23 at 14:23
3

The break statement breaks loops and does not transfer control to the label.

Using a label with break is for when you have inner and outer loops. An unlabeled break breaks the inner most loop (the one you are in) whereas a labeled break allows you to specify an outer loop.

See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Specifically the BreakWithLabelDemo

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • The linked example demonstrates how `break` is used with loops, but it doesn't say that it has to be used with loops explicitly. I don't know why all answers seem to think that. You can use a `break` in an if statement, please see my answer. – rath Sep 13 '17 at 11:01
2

Try to Avoid using labels. What you could do there, is:

while(true) {
    if(x == 0 && y == 0) {
        break;
    }
    // some stuff with x and y
}
sp00m
  • 47,968
  • 31
  • 142
  • 252
1

I suggest that you use a recursive function.

public void work(){
    // some code
    if (x != 0 && y != 0) {
        //some code
    } else {
        work();
    }
} 

You cannot use break without loops

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Adeeb
  • 1,271
  • 3
  • 16
  • 33
-1

As far as I know, labels cannot be used arbitrarily around {}, you need to do them to mark a for, while or do-while loop

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • 2
    you can label if-else statements as well: the compiler wouldn't raise errors. Of course, it would be no use since you can't "break" or "continue" them. :) – javatutorial May 24 '16 at 10:12