Hi Salvatore,
I don’t think there’s a mistake in the problem presentation. What I see, however, is a possible confusion with the implementation as it seems to be the opposite of the problem. But it’s not, because there could be two or more ways to say the same thing.
Here is the problem statement:
Now if we wanted to implement this verbatim, we would write:
if (postion_is_greater_than_x_limit) {
stop_moving();
}
move_backwards();
But this would not work. Let’s assume that at the beginning x_limit is 10
and position is 1
, then the loop would be skipped and the robot would move backwards ONCE and would NEVER STOP, because it will not go back to check the the loop condition again even when position gets to 11
(as the program flows from top to bottom).
What we want is for the robot to keep moving backwards while position is 10 or less
, so we need to do the backward movement inside the loop. Then when that condition is no longer true, we break out of the loop and stop the robot. Which is why we introduce the opposite of the problem as the loop condition, and also reverse the actions:
if (postion_is_NOT_greater_than_x_limit) {
move_backwards();
}
stop_moving();
So now, as position
progresses from 1 through 10
, the loop keeps executing and moving the robot backwards. The moment it exceeds 10
, the loop breaks and we stop the robot.
We could also implement it like this:
if (postion_is_less_than_or_equal_to_x_limit) {
move_backwards();
}
stop_moving();
Well, we could also change the problem statement so that it sounds exactly like the implementation, but that would be unrealistic because we normally don’t decide what the problem says, but we can determine how to interpret and solve it.