-6

Hey guys, i have this while loop for variable 'i' and i have created a board[][] which is 2x2 (so sizeX for the board is 2 in this example). The problem is i can't work out why it isn't leaving the while loop if it goes through one of the 'if' statements starting with 'i=1' and then it then sets 'i=i+1' (=2) but doesn't leave the while statement and tries to see what board[2][x] is equal to when obviously that is out of the bounds of the board. Sorry for confusing explanation i hope you understand...

Jake
  • 1
  • there are cases in which you don't change the value of x. – Stultuske Mar 21 '16 at 12:49
  • so does it have to leave while loop for x before it can leave the one for i? – Jake Mar 21 '16 at 12:51
  • 3
    First: you should not post the whole message of your post as a link and second: you can put source code directly into your post (without using an external image). That would make it even easier to read your question and your code simultaneously. ;-) – Alex Mar 21 '16 at 12:55
  • Please format question better. – K.Nicholas Mar 21 '16 at 13:00
  • You should check your outer `else` statement. Either this would cause your inner loop to run forever (it does not change the value of `x`) or crash your application (negative index). – Alex Mar 21 '16 at 13:01

1 Answers1

-1

I am assuming that you are new to programming because you are using BlueJ. When your second loop is running, I'm guessing that your line x=x is keeping x within range and hence the loop never exits.

One good thing to help is to use a debugger or print statements to show what the values of x and i are at each stage. That will quickly let you know what is going wrong.

dilbert
  • 41
  • 1
  • 5