if statement - practice

In this section we will only practice using the if statement and combining it with loops.

Tasks for exercise

Go to the end of a path and take only one ball

Karel should arrive at the end of the corridor, and only take the first ball on the way. The starting square never has a ball on it, and Karel initially does not carry any balls.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__take_first_ball_only)

We have started one solution here. You are expected to complete the if statements with appropriate conditions.

Karel should take the ball only if two conditions are met:

  • the first condition is the one that we check whenever Karel tries to take the ball (without this condition the program could be terminated due to an undoable operation).

  • the second condition is imposed by the requirements of this task, which is that Karel takes the ball only if he has not already taken one before.

The order of checking these two conditions is not important, since both of them should be fulfilled in order to take the ball anyway.

Take the ball on the neighboring square

There is only one ball on the board. Karel and the ball are located on two adjacent squares with no wall between them (Karel is only one step appart from the ball, if he turns to the ball first). There may or may not be a wall between other squares. Karel should take the ball and he can finish on any square in the end.

As usual, run the program several times to test it on various examples.

One possible idea is that in each of the four directions, we try to make Karel go one step forward and pick up the ball. Various scenarios can occur in each of the four attempts:

  • it is possible that there are no squares in front of Karel in that direction

  • it is possible that there is a square in front of Karel, but there are no balls on it

  • it is possible that there is a square and that there is a ball on it

When trying the next direction, it is much simpler if we don’t have to take into consideration whether Karel has found a square without a ball in the previous direction he tried, or did not find a square at all. To simplify the next attempt, it is convenient for us that Karel finishes the previous attempt when he was on an empty square in the same state as when there was no square. When there is no square in the attempted direction, Karel will remain on the starting square, facing the attempted direction. To facilitate the continuation of the search, we can leave Karel on the same square facing the same direction when he returns from an empty adjacent square. In fact, it won’t do any harm if we do it also when Karel takes the ball (it is possible that Karel needlessly continues to search, but that will not cause any errors). Because we’ve brought Karel to the same state (position and orientation) after any of the three cases above, we know our new starting state exactly, for each subsequent attempt. After each attempted direction, we just need to turn Karel towards the next direction we will try to find the ball in (either to the left or to the right).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__take_neighboring_ball)

Follow the path

There is only one ball on the table, and Karel should take it. The way to the ball is not straight, but there are no intersections (there is always only one way to continue moving, even from the starting square).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__take_ball_no_branches)

We give a program-like instructions for one possible solution:

Sidetrack

There is only one ball on the table and Karel should take it. To get to the ball, Karel should go straight only when he can’t turn left or right (there will be no ambiguous crossroads where there is a path to both left and right).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__p1_left_p2_right_p3_forward)

Instructions for one possible solution:

Go left wherever you can

There is only one ball on the table and Karel should take it. Karel will always reach the ball by turning left whenever he can, and going straight when he can’t go left (when he cannot go either left or straight, that means he has arrived). Karel is initially turned the way he should, and his first step is always straight forward.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if_p1_left_p2_forward)

Instructions for one possible solution: