$$ \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.

Working with Blocks from the Loops Category

There are processes in nature that are repeated constantly or from time to time. As in nature, in programming, sometimes it is necessary to repeat certain parts of the program several times. We have already mentioned that repeating one or more commands (blocks) is a powerful concept in programming. When some of the program commands are repeated several times, we say that the program contains loops. We have used this concept several times before.

Repeating commands is a very common occurrence in programming.

MakeCode has three types of blocks into which other blocks, whose execution needs to be repeated, can be inserted. Blocks can be repeated:

  • a certain number of times:

_images/p68.png

We should use this block when we know the exact number of repetitions in advance (they are also called iterations).

  • an infinite number of times (continuously, until the user stops the program):

_images/s1.png

This is one of the most frequently used blocks. The running of this block is stopped when the user clicks the stop button (stop).

  • until a certain condition is fulfilled:

_images/p68.png

We should use this block when we do not know how many times blocks inserted into the repetition block should be executed, and therefore, we want them to run until a certain condition is fulfilled. Blocks, which need to be repeated, are dragged and inserted into repetition blocks.

The block, which will repeat other blocks an exact number of times, can be used to reproduce the tone C three times.

The look of the code:

_images/p70.png

The block for repeating commands will be executed an infinite number of times. The running will never stop on its own. We can stop the running by clicking on the stop button (stop).

We used the blocks for repeating in our previous lessons when we displayed the animation of a square.

    Q-9: Analyze the look of the blocks. You can see that the forever block used for repeating commands indefinitely does not have the option for connecting to other blocks, other blocks cannot be added onto it. Why?

  • New blocks can be inserted into the block itself, so there is no need to continue the script.
  • It is a bug in MakeCode. The block that repeats commands an infinite number of times must have the option to continue stacking the script.
  • The answer is not correct!
  • Adding further blocks would be pointless since they would never be executed. :feedback_a: The answer is correct!
  • The answer is not correct!

The block which repeats other blocks (commands) runs until a specific condition is fulfilled. The blocks within this block are executed based on checking whether the condition is fulfilled or not. We use this block when we do not know how many times we need to repeat the commands inside it, and we want them to run until a specific condition is fulfilled.

    Q-10: You want to create a program, which continuously turns on the LED on the screen with coordinates (2,2) until the light level drops below a certain value. Which blocks will you use for repeating commands?

  • The block that repeats commands a certain number of times.
  • The answer is not correct!
  • The block that repeats commands an infinite number of times.
  • The answer is not correct!
  • The block that repeats commands until a certain condition is fulfilled.
  • The answer is correct!

We will demonstrate the use of this block by creating a program that plays a tune as long as the light level is lower than 120.

For the purposes of this program, we will define the variable svetlost, which will store the value of the light level reading. As long as the light level value is less than 120, a sound signal will be emitted. In the figure below you can find our suggestion for the code, along with comments which explain it. Programmers consider commenting on scripts and explaining what each block does to be useful. Commenting makes it easier for other programmers to understand and upgrade programs we created. You can add a comment by right-clicking on the script and selecting the option Add comment.

_images/p72.png

To test the program, we will run it in the simulator by clicking the play button.