If you want to exit only when check
and goal
refer to the same cell, then you'll need to change &&
to ||
in your while
condition.
Java Do While loop skipping condition
Question
I have a do while loop that is terminating without the condition being met and I'm not sure why. When I debug check's row and columns are (4,10) and goal's are (4,1). Any ideas?
Position check = unexplored.get();
do{
if(isValid(check.up())){
unexplored.put(check.up());
numUnexplored++;
}
if(isValid(check.right())){
unexplored.put(check.right());
numUnexplored++;
}
if(isValid(check.down())){
unexplored.put(check.down());
numUnexplored++;
}
if(isValid(check.left())){
unexplored.put(check.left());
numUnexplored++;
}
explore(check);
explored.put(check);
check = unexplored.get();
}while(check.getColumn() != goal.getColumn() && check.getRow() != goal.getRow());
It's worth noting that if I change checks initialization at the top to:
Position check = null;
and then set it at the top of the loop, like:
do{
check = unexplored.get();
....
the loop terminates at the same spot.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow