Check out and decide

The while statement proved very useful, because by using it we were able to solve more diversified tasks. However, the following example shows that there are simple tasks that we can not solve with what we have learned so far.

Let’s say that in some situation you need to move Karel only for one square if possible (if not possible, Karel should remain where he is).

Obviously, we need a new statement, which will tell Karel “if you can go forward, move one place”.

If statement

The statement we need in the described case is the if statement, which also exists in almost all programming languages. In Python, it (in its simpler form) is written like this:

We see that writing the if statement is very similar to writing a while statement. Under the if statement we can also place one or more other statements, which constitute the body of the if statement. Same rules apply for writing a colon : after the condition and for indenting statements that are executed if the condition is met. The difference is that the statements in the body of an if statement will not be repeated - if the condition is fulfilled, they will only be done once.

The if statement is sometimes called the branching statement because the program execution flow branches at this stement: the next statement to be executed depends on the answer to the question from the condition.

In the above example, we should write:

Let’s see some tasks in which (besides already known statements) the if statement is used.

Take one ball, if there are any

There is one square in front of Karel, with zero or more balls. Write a program that tells Karel to move onto that square, and then take exactly one ball if there is at least one ball in the field.

Run the program several times to test it on different examples.

In our case, the condition will be is_ball_on_square(), and the command that is conditionally executed is pick_ball().

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_one_if_any)

Go to the end of path and pick one ball where possible

There is at least one square in front of Karel, and there can be any number of them. Each square has zero or more balls. Karel should pick up exactly one ball from each square on which there is a ball.

Run the program several times to test it on different examples.

Here it is necessary to use while statement for advancing forward, and in the body of the while loop, after every step forward, an if statement should be used to check whether Karel stands on a square with a ball on it or not.

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__many_squares_take_one_if_any)

If you don’t do that, do this (if-else)

In some tasks, one thing needs to be done if a certain condition is met, and some other thing if it is not met. In such case, we can use the extended form of an if statement, which looks like this:

In the extended form of the if statement, the first part (up to the word else) has the same look and meaning as before. Below that part, the word else is written, equally indented as the word if`, followed by a colon :. In the following lines we write one or more other statements, which constitute the body of else branch. This second group of statements is indented one level deaper than the word else above, and is executed if the condition specified in the `` if`` statement is not met.

Example - taking and dropping balls

There are 3 squares in front of Karel, and on each of them there can be either one ball or no balls. Karel should take balls from the squares that have balls on them and place one ball on each square that was initially empty. Karel has enough balls with him at start.

Using the new, expanded form of the if statement, we can tell Karel: “If there is a ball on the square, then take that ball, otherwise drop one ball”, so that the task can be easily solved:

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_else_put)

Pick up the balls that you can reach

A labyrinth consists of two rows. Karel is in the upper row, which is completely empty and passable. In the lower row there may be obstacles, or squares with one ball. Karel’s task is to pick up all the 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_all_from_lower_row)

Act only when something is not met

Let’s say that Karel needs to turn left if he can not go forward (if he can go forward, he should not do anything).

According to the rules of writing an if statement, after the condition (in the body of the first branch) there must be at least one statement, and according to the logic of the task, we do not need any statements at that place. In such situations we can write:

or

In the first case, we use special pass statement that does nothing. By doing so, we both satisfy the syntax (writing rules), and we get a program that works the way we want.

In the second case, by using the word not, we make the opposite condition, which means that the condition of the if statement is satisfied when Karel can not go forward. In this case, branches change roles, so the else branch becomes the one that is no longer needed.

In the few upcoming tasks, there is something to be done only when the condition is not met.

Turn to an empty field

Initially, Karel can face either side, but he can start moving only in one direction. Karel needs to turn to the free square and make one step.

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__turn_to_free_square)

We offer you two short solutions:

Where there are no balls, add them

There is an unknown number of squares in front of Karel, and, each of them can contain one ball or no balls. Karel has enough balls with him, and he needs to put one ball on an each emtpy sqare.

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__fill_the_empty_squares)