In the given Karel example, the purpose of If Statement #2 is:
To pick up the ball that is in the last spot, if there is one.
This is because the loop (while frontIsClear()
) will move Karel until it can no longer move forward. When Karel exits the loop, it is indeed at the last position it can go to, and there may still be a ball there. The second if statement checks if there are balls present at that last position, and if so, Karel takes the ball.