$$ \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor} \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil} \renewcommand{\mod}{\,\mathrm{mod}\,} \renewcommand{\div}{\,\mathrm{div}\,} \newcommand{\metar}{\,\mathrm{m}} \newcommand{\cm}{\,\mathrm{cm}} \newcommand{\dm}{\,\mathrm{dm}} \newcommand{\litar}{\,\mathrm{l}} \newcommand{\km}{\,\mathrm{km}} \newcommand{\s}{\,\mathrm{s}} \newcommand{\h}{\,\mathrm{h}} \newcommand{\minut}{\,\mathrm{min}} \newcommand{\kmh}{\,\mathrm{\frac{km}{h}}} \newcommand{\ms}{\,\mathrm{\frac{m}{s}}} \newcommand{\mss}{\,\mathrm{\frac{m}{s^2}}} \newcommand{\mmin}{\,\mathrm{\frac{m}{min}}} \newcommand{\smin}{\,\mathrm{\frac{s}{min}}} $$

Prijavi problem


Obeleži sve kategorije koje odgovaraju problemu

Još detalja - opišite nam problem


Uspešno ste prijavili problem!
Status problema i sve dodatne informacije možete pratiti klikom na link.
Nažalost nismo trenutno u mogućnosti da obradimo vaš zahtev.
Molimo vas da pokušate kasnije.

Even shorter programs

Let’s get back to the last program of the previous lesson. We needed Karel to take five balls from each of the three squares in fornt of him.

../_images/nested_for_3x5.png

The program that solves the task could look like this:

We see that, in this program, the following group of statements repeats three times:

This allows us to shorten the program further. In explaining the for statement, we mentioned that other loops can be found in the loop body. Now we have the opportunity to use it.

Nested for loops

When there is a second loop in the body of one loop, then the first loop is called an outer loop and the other one is called an inner loop. Together we call them nested or inserted loops. In the following example, we’ll see how nested for loops are written.

Pick up five balls three times

There are three squares in front of Karel, and there are 5 balls on each of them. Karel needs to pick up all the balls.

The task is repeated, but now we will solve it in a different way.

We mentioned that i in the previous examples of for statements is a name for the repetition counter. Now, for the first time we need to count other things (balls) during the counting of one thing (squares). This means, for example, that we will need to exactly know when we are on the third square, taking the second ball. Therefore, we can not use the same name for both counters, so we have introduced new names for counters instead of the previous i. In the following program, we call the square counter i_square, and the name for the ball counter is i_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_for_TakeMxN)

In the given solution, the statement pick_ball() is additionally indented, because it is executed ones for each i_ball from the range [0, 1, 2, 3, 4]. In addition, the whole statement for i_ball in range(5): (together with its body and the statement move() above it), repeats 3 times, ones for each i_square from the range [ 0, 1, 2]. This means that the command pick_ball() executes a total of 3 x 5 = 15 times (on each of the three squares five times).

With nested loops, it is necessary to pay extra attention to the correct indentation of the statements, because it becomes somewhat more complicated. Incorrect indentation of some commands can lead to the wrong result, or to a program that does not work at all.

Tasks for exercise

5 balls on every third square

There are five balls on every third square in front of Karel, and he should collect them all.

The task is similar to the previous, you just need to repeat picking up the ball. Make sure the loop for taking the balls is underneath the loop for moving forward, not in it.

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

Go about

Karl once more needs to pick up all the balls.

The outer loop should be executed 3 times, and in it Karel should do the following:

  • Repeat twice the two actions: “move forward” and “take the ball”

  • Turn left

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

We’ve made verbal instructions a little more similar to the program, try converting them into statements. If you still want to see the program itself, click the “Solution” button.

Go about and pick 3 balls each time

Write a program by which Karel will pick up all 18 balls.

This task differs from the previous one in just one thing: now picking up the balls should be in an additional loop. This means that we will have three nesting loops: one that counts sides of the maze, one that counts the squares along one side, and the third one that counts the balls on one 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_for_ring_3)

Again, we give instructions which look like a program (this time without the program itself).