-3

My Code :

class MixFor5 {
public static void main (String [] args) {
    int x = 0;
    int y = 30;
    for (int outer = 0; outer < 3; outer++) {
        for (int inner = 4; inner > 1; inner--) {
            x = x + 3;
            y = y - 2;
            if (x == 6) {
                break; // *Useless break;*
            }
            x = x + 3; 
        }
        y = y - 2; 
    }
    System.out.println(x + " " + y);
 }
}

My output:

54 6

Can someone explain to me. Why when I remove break; my output data don't change at all.

2
  • 2
    Never even tried to use a debugger? This would be the perfect situation for it.
    – Tom
    Commented Sep 12, 2016 at 16:34
  • Break only breaks the inner loop. And it is not a breakpoint! Commented Sep 12, 2016 at 16:37

1 Answer 1

1

You are never fulfilling the if(x==6)

lets take a look at the first loop:

int x = 0; 

//....

x = x + 3; // x = 3;
if( x == 6 ) //false
   break;

x = x + 3; // x = 6

now the second loop

x = x + 3 // x = 9

if( x == 6 ) //false x = 9
    break;
x = x + 3; //x = 12

so you never are equal to 6 when comparing.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.