How to do a certain number of repetitions in the idol. Branching in the idol language

How to do a certain number of repetitions in the idol.  Branching in the idol language
How to do a certain number of repetitions in the idol. Branching in the idol language

Acquaintance with the program Kumir mastering the basics of programming.

In it, students can gain practical skills in creating and debugging an algorithm, working with performers such as Robot, Draftsman, Aquarius, Grasshopper, Turtle.

When studying one of the most difficult sections of computer science "algorithmization and programming".

Purpose of development :

Download:


Preview:

Methodical development in informatics.

Topic: "Robot performer in the KuMir program at informatics lessons"

technology teacher "Informatics and ICT"

Explanatory note

Development goal: explore the possibilities of programming by example specific artist Robot using the KUMIR environment; give practical skills to work with the performer.

Methodical developmentcompiled for informatics lessonsPractice on the computer: work with the educational executor of algorithms; drawing up linear, branching and cyclic algorithms performer management; drawing up algorithms with a complex structure; use of auxiliary algorithms (procedures, subroutines).

Students should know:

  • what is a performer; SKI Robot, Wednesday performer Robot;
  • what is an algorithm;what are the main properties of the algorithm;
  • ways of writing algorithms: flowcharts, educational algorithmic language;basic algorithmic constructions: following, branching, loop; structures
  • algorithms; ⇒ assignment of auxiliary algorithms; technologies for building complex algorithms:

Students should be able to:

  • understand descriptions of algorithms in a learning algorithmic language;
  • perform an algorithm trace for a known performer;
  • compose linear, branching and cyclic control algorithms for the Robot executor; allocate subtasks; define and use auxiliary algorithms.

Session 1 (2 hours) Lesson 1.

Performer Robot.Executor command system.

Lesson plan.

  1. Description of the executor's UCS, the executor's environment.

2. Analysis of typical robot algorithms.

During the classes.

Consider the description of the performer.

Executor environment: Performer The robot is able to move through the labyrinth, drawn on a plane divided into cells.

SKI Robot : simple commands: up, down, left, right, color.

Logic commands: (condition checks)

top free bottom free

left free right free.

Logical connectives: AND, NOT, OR:

Example: (Not left free) or (Not right free)

Branch command: cycle command:

If condition then nts while condition

series of commands series of commands

all kts

(In CIMs of 2009, the commands of the Robot differed from those familiar to children, which led to confusion :)

Branch command: cycle command:

If condition then nts while condition do

series of commands series of commands

end end

General form windows of the Kumir program. Graphical environment of the Robot:

in KIMs demo version 2010 command format changed to habitual

The order of creating the algorithm:

1.Teams Tools -Edit starting environment draw walls on the Robot field and set the Robot to its initial position.

2.Commands Robot - Change starting environmentsave the new environment.

3.Commands Paste- Use Robotspecify the artist.

4. In the document window, write the algorithm using the menu Insert.

5. Commands Execution - execute continuously (or step by step) run the algorithm.

6. Consider the result of the algorithm execution and, if necessary, debug it.

Lesson 1 (2 hours) Lesson 2.

Practical work "Compilation of linear algorithms.

Tasks: 1. Robot at an arbitrary point in the field. Color the cell above, below and to the right of the starting position.

  1. Robot at an arbitrary point in the field. Move the Robot 4 spaces to the right, painting over them.
  2. Create a new starting environment by drawing a 4-cell square on the board. Save the environment as a start.
  3. Create a new starting environment by drawing a corridor with passages in the walls on the field. Save the environment as obst2.fil. Change the starting environment to the newly created one.

Session 2 (2 hours) Lesson 1.

Subject : Branching and sequential refinement of the algorithm.

Analysis of CIM tasks using the Robot executor.

use Robot

alg kim 2009

early

if not bottom loose

then to the right

All

if not bottom loose

then to the right

All

if not bottom loose

then to the right

All

con

use Robot

alg kim 2010

early

if not bottom loose

then to the right

All

if not bottom loose

then to the right

All

if not bottom loose

then to the right

All

con

Etc. slave. No. 14. Compilation and debugging of branching algorithms

Tasks. See Attachment.

Lesson 3. Cyclic algorithms. Lesson 1-2

Target: reveal the essence of the concept of a cycle in algorithms, show the forms of writing cycles in algorithms, give skills in creating and writing cyclic algorithms.

Etc. slave. No. 15. Compilation and debugging of cyclic algorithms

1. Make an algorithm that paints all the inner cells adjacent to the wall.

use Robot

alg

early

nc while right free

paint over; right

kts

nc while the bottom is free

paint over; down

kts

nc until the bottom loose

paint over; to the left

kts

con

2. Create an algorithm that fills all the cells between the Robot and the wall. The distance to the wall is unknown.

use Robot

alg

early

nc while right free

right; paint over

kts

con

3. Create an algorithm that paints over all the cells between two walls.

use Robot

alg uch3

early

nc yet (not top loose) or (not bottom loose)

right

if (not top free) and (not bottom free)

That

paint over

All

kts

con

4. Create an algorithm that fills all the cells around a rectangular wall.

alg uch4

early

paint;up

nc until right loose

paint;up;

kts

paint;right

nc until the bottom loose

paint;right;

kts

paint over;down

nc until left loose

paint;down;

kts

paint;left

nc until top loose

paint over; left;

kts

con

use Robot

alg uch5

early

right

nc until the bottom loose

paint over; right

kts

paint over; down

nc while left free

paint over; to the left

kts

nc until left loose

paint over; down

kts

paint;left;paint; up;

nc while top free

paint over; up

kts

nc until top loose

paint over; to the left

kts

con

Activity 4 Lesson 1

Helper Algorithms.

Target: introduce the concept of the main and auxiliary algorithm; explain the rules for using the auxiliary algorithm; parse examples of algorithms using an auxiliary.

Lesson plan

1.Introduction of new terms (main and auxiliary algorithm, call) and explanation of new concepts.

2. Analysis of examples of solving problems using an auxiliary algorithm.

When solving some problems, it is convenient to break them into smaller subtasks, each of which can be designed as an independent algorithm. In this case, the so-called main algorithm is first compiled, in which calls to auxiliary algorithms are used to solve subtasks, which are added later. This kind of solution is calledsequential refinement method.It allows a group of programmers to work on a project, while each one solves his own subtask.

In the process of solving the problem, each auxiliary algorithm can, if necessary, be divided into smaller auxiliary algorithms.

The command to execute the auxiliary algorithm is called challenge and is written in the body of the main algorithm.

One and the same algorithm can be considered as the main and auxiliary in relation to other algorithms. In an algorithmic language, the main algorithm is written first, and auxiliary ones are written in a row below.

Task 1:

The robot is in the upper left corner of the field. There are no walls or shaded cells. Compose an algorithm, using an auxiliary one, drawing four crosses on one horizontal line. The final position of the Robot can be arbitrary.

Solution

Analysis on the board:

Task2. The robot is in the upper left corner of the field. There are no walls or shaded cells. Write an algorithm that paints an 8 x 8 square in a checkerboard pattern. The final position of the Robot can be arbitrary.

Activity 4 Lesson 2

Practical work on a PC "Problem solving using auxiliary algorithms".

Target : to instill practical skills in constructing algorithms by the method of sequential refinement.

Lesson plan

1. The task is completely completed by the PC. Students receive assignments and complete them software environment Idol. The results of the Work are saved as files for later verification.

Task1 . The robot is in the lower left corner of the field. There are no walls or shaded cells. Write an algorithm that paints 6 vertical stripes of the same length in 6 cells. The final position of the Robot can be arbitrary.

Task2 .Using auxiliary, make an algorithm for painting over the cells that form the number 1212.

Homework: Come up with an algorithm that draws the following image: Apply two auxiliary algorithms to solve the problem.

Activity 5 Lesson 1-2

Test

"Compilation of the algorithm in the environment of the executor Robot".

Target: to test the acquired knowledge on the creation and ability to analyze algorithms in the Kumir software environment.

Tasks for control work are divided by difficulty levels and includes 3 tasks with the executor Robot (tasks 1 and 2 - for branching and loops, task 3 - for using an auxiliary algorithm.) The texts of the tasks are given in the appendix.

The initial and final conditions and the created algorithms are recorded as a file.

The grade is set according to the level of difficulty of the task. The student has the right to choose the type of task.

Executor Control Robot in the KUMIR system

The robot exists in a certain environment (rectangular checkered field). Walls can be located between some cells of the field. Some cells may be shaded (Fig. 3.11).

The robot occupies exactly one cell of the field.

On commands up, down, left and right, the Robot moves to the next cell in the specified direction. If there is a wall on the way, then a failure occurs - a message is displayed about the impossibility of executing the next command.

On command to paint over, the Robot paints over the cell in which it stands. If the cell has already been painted over, it will be painted over again, although no visible changes will occur.

The robot can only execute correctly written commands. If you write down instead of the command down, then the Robot will not understand this entry and will immediately report an error.

ABOUT
errors: 1 syntactic; 2. logical

Scene descriptions are stored in text files special format (.fil format).

Current- the environment in which the Robot is in this moment(including information about the position of the Robot).

Home- the environment in which the Robot is forcibly placed at the beginning of the execution of the program using the Robot.

Operating procedure:


  1. Ask starting environment according to the task:
Menu Tools → Change the starting environment of the Robot (draw the environment according to the task condition, give a name, save in the Personal folder)

2. Specify the Contractor:

Insert Menu →Use Robot

3. Write an algorithm for solving the problem.

4. Run the algorithm (Menu Run → Run continuously / F9)

The system of commands of the executor Robot in the KUMIR system


Team

Action

up

The robot moves up 1 cell

down

The robot moves down 1 cell

to the left

The robot moves 1 cell to the left

right

The robot moves 1 cell to the right

paint over

The robot paints the cell in which it is

right free

The robot checks the execution of the corresponding simple conditions

left free



top loose



bottom loose



the cell is shaded



cage clean



Cyclic algorithms

Cycle organization of repeating actions until a certain condition is true .

Loop body - a set of repeatable actions.

Condition - boolean expression (simple or complex (compound))
Cycle types:

1.Loop "Repeat n times" 2. Loop "Bye"
nc n times nts bye
. . Loop body. . Loop body
kts kts

Example: nts bye right free


General view of the cycle "Repeat n times:

REPEAT n TIMES

END
kts

General view of the while loop:

WHILE TO DO

END
Compound conditions are formed from one or more simple conditions and service words AND, OR, NOT.


Compound condition A and B(where A, B are simple conditions) is satisfied when each of the two simple conditions included in it is satisfied.

Let A - free on top IN - free on the right then the compound condition A and B- free on top AND free on the right.


Compound condition A OR B is satisfied when at least one of the two simple conditions included in it is satisfied: top free OR right free
Compound condition NOT A- met when condition A is not met.

Example: Let A be a shaded cell (simple condition).

P Checking the compound condition NOT A:

a) A - done, NOT A (NOT shaded) - not done.

b) A - not done, NOT A (NOT shaded) - done.


Branch command

Branching - a form of organization of actions in which, depending on the fulfillment or non-fulfillment of a certain condition, either one or another sequence of actions is performed.

General view of the IF command:

IF THAT OTHERWISE

END

In the KUMIR language:

Full branching: Partial branching:
If That If That

otherwise

all all

Helper Algorithm- an algorithm that solves some subproblem of the main problem.

In the KUMIR system, auxiliary algorithms are written at the end of the main program (after the service word con) are called for execution in the main program by name.

IN surveys and assignments

1. Give all the algorithms of the three commands that will move the Robot from its original position to cell B.

Is there an algorithm for this task, during which the Robot does:

a) two steps b) four steps; c) five steps; d) seven steps?


  1. Petya made an algorithm that transfers the Robot from cell A to cell B with some cells painted over. What should Kolya do with this algorithm in order to obtain an algorithm that takes the Robot from B to A and fills in the same cells?


7. Two auxiliary robot algorithms are known

Draw what happens when the Robot performs the following basic algorithms:


A)

nc 5 times


pattern_1

right; right;


b)

nc 7 times


pattern_2

right; right


V)
right; right; right

up; up

right; right; right

down; down


G)
right; right
right; right

8. Create algorithms under which the Robot will paint over the specified cells:



9. It is known that somewhere to the right of the Robot there is a wall. Make up an algorithm, under the control of which the Robot will paint over a number of cells up to the wall and return to its original position.

10. It is known that somewhere to the right of the Robot there is a shaded cell.

WITH leave the algorithm, under the control of which the Robot will paint a number of cells up to the shaded cell and return to its original position.

11. It is known that the Robot is located near the left entrance to the horizontal corridor.

12. It is known that the Robot is somewhere in the horizontal corridor. None of the cells of the corridor is painted over.

Make up an algorithm, under the control of which the Robot will paint over all the cells of this corridor and return to its original position.


13. In a row of ten cells to the right of the Robot, some cells are shaded.

WITH leave the algorithm that paints the cells:

a) below each shaded cell;

b) above and below each shaded cell.


14. What can be said about the correctness of the following fragment of the algorithm?

nts bye the cell is shaded

IF right free THAT

right; paint over

To
c

15. Write a program with which the Robot can get to cell B in all three mazes.


16. Write a program, following which the Robot will be able to go along the corridor from the lower left corner of the field to the upper right. The corridor has a width of one cell and stretches in the direction from left-bottom-right-up. An example of a possible corridor is shown in the figure.

W

adachi GIA


  1. Corridor1. The robot is somewhere in the vertical corridor. None of the cells of the corridor is painted over. Create an algorithm under which the Robot will paint over all the cells of this corridor and return to its original position.

  1. TO
    Necessary

    Given
    corridor2. The robot is located in the upper cell of a narrow vertical corridor. The width of the corridor is one cell, the length of the corridor can be arbitrary.

A possible variant of the initial location of the Robot is shown in the figure (the Robot is indicated by the letter "P")

Write an algorithm for the Robot that fills all the cells inside the corridor and returns the Robot to its original position. For example, for the above picture, the Robot should paint over the following cells (see picture):


  1. There is a long horizontal wall on the endless field. The length of the wall is unknown. The robot is in one of the cages directly above the wall. The initial position of the Robot is also unknown. One of the possible positions:
H


Necessary

Given
Write an algorithm for the Robot that paints all the cells above and adjacent to the wall, regardless of the size of the wall and the initial position of the Robot. For example, for the given drawing, the Robot must paint over the following cells:

The final position of the Robot can be arbitrary. When executing the algorithm, the Robot should not be destroyed.



  1. There is a long vertical wall on the infinite field. The length of the wall is unknown. The robot is in one of the cages located directly to the right of the wall. The initial position of the robot is also unknown. One of the possible positions of the robot is shown in the figure (the robot is marked with the letter “P”): Write an algorithm for work that paints over all the cells adjacent to the wall: on the left, starting from the top unpainted and through one; on the right, starting from the bottom shaded and through one. The robot must paint over only the cells that satisfy this condition. For example, for the above figure, the robot must fill in the following cells (see figure): The final location of the robot can be arbitrary. The algorithm must solve the problem for an arbitrary wall size and any valid initial position of the robot. When executing the algorithm, the Robot should not collapse.


Write an algorithm for the Robot that paints all cells located to the left of the vertical wall and above the horizontal wall and adjacent to them. The robot must paint over only the cells that meet this condition. For example, for the above picture, the Robot must paint over the following cells (see picture).


H write an algorithm for the Robot that paints the cells adjacent to the wall, from above and below, starting from the left and through one. The robot must paint over only the cells that meet this condition. For example, for the given figure a) the Robot must paint over the following cells (see Fig. b).

The final position of the Robot can be arbitrary. The algorithm must solve the problem for an arbitrary wall size and any valid initial position of the Robot.



R

  1. There is a long vertical wall on the infinite field. The length of the wall is unknown. The robot is in one of the cages located directly to the left of the wall. The initial position of the robot is also unknown. One of the possible positions of the robot is shown in the figure (the robot is marked with the letter "P"):
Write for work an algorithm that paints over all the cells adjacent to the wall:

  • all on the left;

  • on the right, starting from the top unpainted and through one.
The robot must paint over only the cells that meet this condition.

B
1102_GIA2011

There are two horizontal walls on the infinite field. The length of the walls is unknown. The distance between the walls is unknown. The robot is located above the bottom wall in a cage located at its left edge. Write an algorithm for the Robot that paints all the cells located above the bottom wall and below the top wall and adjacent to them. The robot must paint over only the cells that meet this condition. For example, for the above drawing, the robot must fill in the following cells (see figure):

The final location of the robot can be arbitrary. The algorithm must solve the problem for an arbitrary field size and any admissible location of walls inside a rectangular field. When executing the algorithm, the Robot should not collapse.


IN
1103_GIA_2011


There is a horizontal wall on the infinite field. The length of the wall is unknown. From the right end of the wall, a vertical wall extends downward, also of unknown length. The robot is located above a horizontal wall in a cage located at its left edge. The figure shows one of possible ways the location of the walls and the Robot (the Robot is indicated by the letter "P").

Write an algorithm for the Robot that paints all the cells located above the horizontal wall and to the right of the vertical wall and adjacent to them. The robot must paint over only the cells that meet this condition. For example, for the above picture, the Robot must paint over the following cells (see picture).

Khiryanov Timofey Fedorovich

The main algorithmic constructions, in addition to elementary operations represented by one element of the circuit, are alternative execution and cycles. There are two alternative programming options and there are three main cycle types.

Conditionally executable code

Some operations can be placed under a conditional statement. Then they will be executed only if this condition is true.

If<условие>
That
<действия>
All

Alternative

In a flowchart, a condition test can serve as a principle for selecting alternative operations. That is, if the condition is true, the execution will go along one trajectory, and if it is false, then along the other. In the KuMir language, a loop with a precondition has the following form:

If<условие>
That
<действия>
otherwise
<альтернативные действия>
All


Conditions for the robot:
left wall
right wall
bottom wall
top wall
the cell is shaded
left free
right free
bottom loose
top loose
cage clean

Loop with precondition

A loop with a precondition is a loop that is executed while some condition specified before its start is true. This condition is checked before the execution of the loop body, so the body may not be executed even once (if the condition is false from the very beginning). In most procedural programming languages, it is implemented by the while statement, hence its second name is the while loop. In the KuMir language, a loop with a precondition has the following form:

nts bye<условие>
<тело цикла>
kts

Loop with postcondition

A loop with a postcondition is a loop in which the condition is checked after the execution of the loop body. It follows that the body is always executed at least once. In Pascal this loop is implemented by the repeat..until operator, in C it is do…while .
In the KuMir language, a loop with a postcondition has the following form:

nc
<тело цикла>
cc_at<условие>

Loop with counter

A loop with a counter is a loop in which some variable changes its value from a given initial value to a final value with some step, and for each value of this variable, the body of the loop is executed once. In most procedural programming languages, it is implemented by the operator for, which specifies the counter (the so-called "loop variable"), the required number of passes (or the limit value of the counter), and possibly the step in which the counter changes. In the KuMir language, a cycle with a counter has the following form:

whole
nc for a from 0 to 9
... loop body
kts

In different programming languages, the question of the value of a variable at the end of a loop in which this variable was used as a counter is solved differently.

Algorithm for drawing a spiral:

use Drawer
alg
early
. move to a point(3,3)
. lower the pen
. coil(1); coil(3); coil(5); coil(7); coil (9)
. raise the pen
con
alg turn(arg w)
early
. shift by vector(a, 0)
. shift by vector(0, -a)
. shift by vector(-a-1.0)
. shift by vector(0, a+1)
con

Pay attention to the command block:

Coil(1); coil(3); coil(5); coil(7); coil (9)

Auxiliary algorithm "coil (arg thing a)" is called 5 times, but it cannot be called in the "N times" loop, because each time it is called with different values argument.

But you can see that the values ​​of the argument change from 1 to 9, each time increasing by 2. So, we can help loop with counter. Also, such a cycle is called the "for" cycle.

Loop with counter- a loop in which some variable changes its value from a given initial value to a final value with some step, and for each value of this variable, the body of the loop is executed once.

Typically, this loop is used if you need to iterate over some values ​​and perform some actions for each of them.

General view of the cycle with a counter:

nc for<счетчик>from<нач. знач.>before<кон. знач.>[step<знач.>]
<тело цикла (последовательность команд)>
kts

It is not necessary to indicate the step, if it is not specified, then it is considered equal to one.

Now we can rewrite the "spiral" algorithm in this way:

use Drawer
alg
early
. move to a point(3,3)
. lower the pen
. whole size
. nc for size 1 to 9 step 2
. . coil(size)
. kts
. raise the pen
con
alg turn(arg w)
early
. shift by vector(a, 0)
. shift by vector(0, -a)
. shift by vector(-a-1.0)
. shift by vector(0, a+1)
con

In this example, the counter variable "size" will receive the values: 1, 3, 5, 7, 9. That is, loop will be executed 5 times. For each value of the “size” variable, the loop body will be executed once, in our example, this is a call to the auxiliary algorithm “coil (arg thing)”.

Before the first use of a variable, it must be declared, i.e., what type it is. This is done in our program in the line “integer size”, i.e. we indicate that we will use the “size” variable to store integers, and therefore we need to allocate memory for it. We will talk more about variables a little later.

The block diagram of such an algorithm looks like this:

Let's look at another example:

Let's first remember and write an auxiliary algorithm that will draw a square at the point (x, y). For a change for drawing, we will use the command shift by vector(in the previous examples, they were shifted to a point).

The algorithm could be like this:

alg square(arg x, y, side)
early
. move to a point(x, y)
. shift by vector(-side/2, side/2)
. lower the pen
. shift by vector(side, 0)
. shift by vector(0, -side)
. shift by vector(-side, 0)
. shift by vector(0, side)
. raise the pen
con

Using such an auxiliary algorithm, we draw the following figure:

To do this, we use the "for" loop. Study the example program:

use Drawer
alg figure1
early
. integer z
. nc for z from 2 to 10 step 2
. . square(0, 0, z)
. kts
con
alg square(arg x, y, side)
early
. move to a point(x, y)
. shift by vector(-side/2, side/2)
. lower the pen
. shift by vector(side, 0)
. shift by vector(0, -side)
. shift by vector(-side, 0)
. shift by vector(0, side)
. raise the pen
con

In this example, the variable "z" will receive the values: 2, 4, 6, 8, 10. That is, loop will be executed 5 times. For each “z” value, the loop body will be executed once, in our example, this is a call to the auxiliary square algorithm.

Before the first use of a variable, it must be declared, i.e., what type it is. This is done in our program in the line "integer z", i.e. we indicate that we will use the variable "z" to store integers, and therefore we need to allocate memory for it. We will talk more about variables a little later.

As you noticed, the algorithm used not only numbers, but also algebraic expressions, formulas, for example "-side/2". In computer science, these expressions are called arithmetic. The rules of the language allow, when writing algorithms, wherever you can write a number, write an arbitrary arithmetic expression.

Let's talk about cycles today. Let's figure out what a cycle is and how to teach our Robot to perform cyclic algorithms.

So, what is a cycle? Imagine that we are in a physical education lesson and we are faced with the task do 7 squats. This task can be formalized as a linear algorithm and then it will look something like this:

do a squat

do a squat

do a squat

do a squat

do a squat

do a squat

do a squat

That is, we repeated the command to do a squat 7 times. Does it make sense to write 7 identical commands? Might be easier to give a command. do 7 squats? Certainly easier and more correct. This is the cycle. You can yourself remember examples of cycles from life - there are quite a few of them.

Thus linear algorithm , where the same commands are repeated, we can issue in the form of a cyclic algorithm- like that:

repeat 7 times

do a squat

end of cycle

So, in the language we invented, we designed the cycle. The Robot performer also has the ability to record cycles. Moreover, cycles are different.. The option we have just considered is called loop with counter or loop with parameter.

Types of cycles.

Cycle with a counter.

Loop with counter used when it is known in advance how many repetitions need to be done. In the example above with squats, this is exactly the case.

In order to write a loop with a counter for an executor, you need to know its syntax. And he's like this:

nc<number of repetitions> times

<команда 1>

<команда 2>

<команда n>

Here we must specify the number of repetitions (number) and the commands that will be repeated. Commands that are repeated in a cycle are called loop body.

Let's look at this with an example.

Initially, the Robot was in the upper left cell.

Let's solve the problem linearly first. In this case, we will paint over the current cell and move 1 cell to the right and the program will look like this:
use Robot
alg
early

paint over

right

paint over

right

paint over

right

paint over

right

paint over

right

paint over

right

paint over

right

As you can see, the commands to paint over and to the right are repeated 7 times. Let's now rewrite the program using a loop. By the way, to insert a loop into your program, you can use the menu Insert select item nc-time-kc or press one of the keyboard shortcuts Esc, P(Russian letter Р) or Esc, H(Latin letter H). And keys must be pressed sequentially- first Esc, release it and only then P or H.

So here is our cycle program will look like this:

use Robot

nc 7 times

paint over

right

If we run it, we will see that the result will be the same - 7 filled cells. However, the program has become shorter and much more intelligent from an algorithmic point of view!

As a warm-up and consolidation, I propose to independently write a program for the Robot, which will draw a square with a side of 7 cells. Naturally, using a cycle. Waiting for the solution in the comments.

Conditional loop.

When solving problem 19 of the GIA in informatics with a Robot, it will not work to use a loop with a counter. Since there the field is usually infinite and the walls do not have a specific length. Therefore, we will not be able to determine the number of repetitions for a loop with a counter. But it doesn't matter - it will help us loop with condition.

Let's go back to physical education and change the task. After all, someone may not do 7 squats, while the other is able to do 27. Is it possible to take this into account when creating a cycle? Certainly. Only now we will use not a counter (number of repetitions), but a condition. For example, before you get tired, do squats. In this case, a person will not do a specific number of squats, but squat until he gets tired. And our loop in the abstract language will look like this:

Bye not tired

do a squat

end of cycle

The words are not tired in our case - this is a condition. When it is true, the loop is executed. If it is false (tired) the body of the loop will not be executed. The robot performer has several conditions

top loose

bottom loose

left free

right free

top wall

bottom wall

left wall

right wall

But in the condition of problem 19 of the GIA, only the first 4 are indicated. So we will use only them.

Now let's solve the next task for the Robot - draw vertical line from the left to the right border of the field I use a loop with a condition. Initially, the Robot is in the upper left corner.

Let's first formulate a verbal algorithm - that is, we will describe in words what the Robot needs to do. This algorithm will sound something like this:

« While on the right, freely take a step to the right and paint over the cell »

As a result, the Robot will run through all the cells to the right and will paint over them until the wall is on the right.

The source code for our Robot program will be something like this:

use Robot

nc while right free

right

paint over

As a result of the execution of this program, we will see the following picture: