javabreaklabeled-statements

Using label and break in if-else statement


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;
    }
}

Solution

  • 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
    }