-4

I've taken this excerpt from the OCA Programmer Assessment test. I have changed the code slightly a little.

public class FeedingSchedule {

    public static void main(String[] args) {
        int x=5, j=0;
        OUTER: for(int i=0; i<3; )
            INNER: do {
                i++; x++;
                if(x>10) continue INNER;
                x+=4;
                j++;
            } while(j<=2);

        System.out.println(x);
        }
    }

can someone tell me why this returns -2147483639? my initial assumption was that the code should run forever and not quit.

Any replies as to the explanation of the code will be greatly appreciated.

Thanks

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
zam
  • 451
  • 2
  • 5
  • 21
  • 3
    Sorry, this is not the way StackOverflow works. Questions of the form _"Here's some code, please explain it for me"_ are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Mar 15 '17 at 20:52
  • 1
    You *could* just throw this into an IDE with a debugger and *execute* it, since it's an easily-run snippet... – Makoto Mar 15 '17 at 20:54
  • 2
    Also, that's crap code. If I had a programmer working for me who wrote that code I'd have a serious talk with him/her. This is unmaintainable. – Jim Garrison Mar 15 '17 at 20:54
  • 1
    http://stackoverflow.com/q/3001836/669576 – 001 Mar 15 '17 at 20:57

1 Answers1

1

Check for the value of x. Its starts with 5, then it is incremented by 1, then by 4. At a certain point it is greater than 10, then the continue with label comes into play: x is incremented until an overflow occurs. The overflow stops the loop "Inner: ... continue Inner", so the rest of the outer loop is executed.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33