Contacts

Presentation on computer science on the topic "Algorithms for cyclic structure. Programming loops in Pascal." Cyclic algorithms Types of cycles and cyclic commands in Pascal. Loop with precondition in Pascal - WHILE

Cyclic algorithms04/06/2017
Cyclic algorithms
Types of cycles and cyclic
Pascal commands

A cycle is multiple
repetition of sequence
actions
Repetitive part of the algorithm
called the BODY OF THE LOOP
Types of cycles
With a given number
repetitions
Fulfillment condition
cycle
With condition
Exit condition
cycle

Types of cycles (contents)
Loop with precondition
Practice
Loop with postcondition
Practice
Loop with parameters
Practice
Solving complex problems

Loop with precondition

Practice

condition, and the action that must be performed only
After checking the conditions, use a loop in the precondition.


Before each execution of the loop body, a check occurs
conditions, if the result is “true”, then the body of the loop is executed
Once again, if “false”, then the loop exits.
On the block diagram
Start of the cycle
No
Condition
YES
Loop body
End of the cycle
In Pascal
While<условие>do
begin
<тело цикла>
end;

Loop with postcondition

Practice
If the number of repetitions is unknown in advance, but only given
condition, and the action that must be performed before
Condition tests use a loop with a postcondition.
The condition is used logical expression, body
loop – simple or compound operator.
After each execution of the loop body, a check occurs
conditions, if the result is false, then the body of the loop is executed
Once again, if “true”, then the loop exits.
On the block diagram
In Pascal
Repeat
Loop body
<тело цикла>
Yes
No
Condition
Until<условие>;

Loop with parameter

Practice
Loop with parameter
In cases where the number of repetitions is known in advance
a loop is applied in the parameter.
The variable that specifies the number of repetitions is called
loop parameter or control variable.
After each execution of the loop body, the control
variable increases or decreases, loop
is executed until it exceeds either
there will be less restriction.
On the block diagram
In Pascal
For X:=A to B do
X:=A,B,C
Loop body
X – control variable (cycle parameter)
A – initial value of X, B – final value of X
C – change step X
Begin
<тело цикла>
End;
As a step you can use
only:
"to" = 1;
"downto" = -1

Example task using a loop with a precondition
Theory

Verbal algorithm:
Multiply the number X initially equal to 1
a given number of times (N) by 3.
Start
ProgrammStep;
Var
H,B,X:integer;
Begin
Writeln('Degree?');
Readln(H);
X:=1;
B:=1;
While B<=H do
Begin
X:=X*3;
B:=B+1;
End;
Writeln('Result',X);
End.
Pascal
N
Entering the desired degree
X:=1
Initial values
B:=1
No
"B" degree counter
B≤H
Yes
X:=X*3
Multiply by 3
B=B+1
Counter increase
X
Output of the resulting
values
end
Block Diagram
Explanations

Example of a task using a loop with a postcondition
Theory
TASK: Raise the number 3 to a given power
Verbal algorithm:

ProgrammStep;
Var
H,B,X:integer;
Begin
Writeln('Degree?');
Readln(H);
X:=1;
B:=0;
Repeat
X:=X*3;
B:=B+1;
No
Until B>=H;
Writeln('Result',X);
End.
Start
N
Entering the desired degree
X:=1
Initial values
B:=0
Multiply by 3
X:=X*3
Counter increase
B=B+1
Yes
B>=H
"B" degree counter
X
Output of the resulting
values
end
Pascal
Block Diagram
Explanations

Example task using a loop with a parameter
Theory
TASK: Raise the number 3 to a given power
Verbal algorithm:
Multiply the number X, initially equal to 1, a specified number of times (H) by 3.
ProgrammStep;
Var
H,B,X:integer;
Begin
Writeln('Degree?');
Readln(H);
X:=1;
For B:=1 to H do
Begin
X:=X*3;
End;
Writeln('Result',X);
End.
Pascal
Start
N
X:=1
B:=1,H,1
X:=X*3
X
end
Block Diagram
Entering the desired degree
Initial value X=1
Parameters from 1 to H
Multiply by 3
Output of the resulting
values
Explanations

The choice of cycle depends on the characteristics of the problem conditions. Only practice will tell you the optimal solution.

Task: Having started training, the athlete on the first day
ran 10 km. Every day he increased the daily
the norm is 10% of the norm of the previous day.
What is the total distance the athlete will cover in 7 days?
Input variables:
d – number of days
Sd – distance for the current day
Output variables:
S – common path

Block - diagram for the solution

Start
S:=10
Sd:=10
d:=1
d:=d+1
Sd:=Sd*1.1
S:=S+Sd
No
D=7
Yes
s
end

Program in Pascal

Cycle "For"
Cycle "Bye"
Cycle "Before"
Program beg;
Program beg;
Program beg;
Var
Var
Var
S,Sd: real;
S,Sd: real;
S,Sd: real;
d:byte;
d:byte;
d:byte;
Begin
Begin
Begin
S:=10;
S:=10;
S:=10;
Sd:=10;
Sd:=10;
Sd:=10;
For d:=2 to 7 do
begin
While d<7 do
begin
Repeat
d:=d+1;
Sd:=1.1*Sd;
d:=d+1;
Sd:=1.1*Sd;
S:=S+Sd;
Sd:=1.1*Sd;
S:=S+Sd;
end;
S:=S+Sd;
until (d=7);
Writeln(‘S=‘,S);
end;
Writeln(‘S=‘,S);
End.
Writeln(‘S=‘,S);
End.
End.

Questions for control:
1. Which operator in Pascal defines a loop with
precondition
2. How to specify the step “1” and “-1” as parameters in a loop
3. Which branch does the loop exit from?
postcondition
4. Are there any conditions in a loop with a parameter?
5. What can be the body of a loop
6. When using a loop with parameters
End

Slide 1

Performer ROBOT Cyclic algorithm
Presentation for a computer science lesson. Grade 9 Topic: Control and algorithms

Slide 2

FOR i:=1 TO N DO BEGIN action1;
action2;
1

END;

2
FOR i:=1 TO N DO action1;
action2;

Slide 3

3
WHILE (CONDITION IS TRUE) DO BEGIN action1;
action2; END;

WHILE (CONDITION IS TRUE) DO action1; action2;

4
Slide 4
17 cells
12 cells
Slide 5
Program N1; var i:integer; Begin For i:=1 to 12 do RobotForw;
RobotLeft;

For i:=1 to 17 do RobotForw;

5
RobotLeft;

For i:=1 to 12 do RobotForw;

6
Program N2; var i:integer; Begin While FreeForw do RobotForw;
RobotLeft;

While FreeForw do RobotForw;

RobotLeft;

8
While FreeForw do RobotForw;
RobotLeft;

While FreeForw do RobotForw;

9
RobotLeft; end.

While there is free space ahead, move the robot forward.

10
Slide 8
Slide 9
1
1

Program N3; var i:integer; Begin for i:=1 to 4 do begin While FreeForw do RobotForw;

11
RobotLeft;

end; end.

12
Move forward four times until there is an obstacle and turn left

Slide 10

13
Move forward four times until there is an obstacle and turn left

Slide 11

14
Tasks for independent work
Task 1. An obstacle is placed at the left wall of the setting in a random place. The robot must reach point 1 and return to its original state. Note: use three series-connected loops BYE
Slide 12
Task 2. A weight is placed at the left wall of the setting in an arbitrary place. The robot must reach the cargo, take it, transport it to the warehouse and return to its original state. Note: use two series-connected loops BYE
Slide 13

Task 3. Five weights are placed at the left wall of the setting in a random place. The robot must transport all the goods to the warehouse. Note: use two sequentially connected WHILE loops nested in a loop with a parameter.

15
Slide 14

Example 1 The robot is located in front of the entrance to the corridor. It is necessary to mark all the cells inside the corridor and go back

16
Slide 15
Program N7; Begin RobotForw; While not FreeLeft do begin Select;
RobotForw;
end; RobotBack; While not FreeLeft do RobotBack; end.
Taking a step forward to enter the tunnel

While there is a wall on the left, mark the cell and take a step forward

We return back to the tunnel

18
Example 3 The furnishings are blocked by a wall, dividing the furnishings into two parts. There is a cage-sized passage in the wall in a random location. It is necessary to create a program in which the robot finds this passage and moves to another part of the environment.

Slide 20

19
Program N9; Begin RobotLeft;
While FreeForw do RobotForw;
RobotRight;
While not FreeLeft do RobotForw;
RobotLeft;
RobotForw;
RobotForw; end.


We turn the robot towards the wall.

Let's move forward until we hit the wall Rotate the robot along the wall

We move forward until the wall ends

Turn the robot towards the passage We take two steps forward, we pass to the other half of the situation Types of cycles

Turn the robot towards the passage loops with parameter for


loops with precondition

cycle

while


with precondition repeat - until


with postcondition repeat - until

Loop with precondition in Pascal - WHILE

A loop operator with a precondition performs actions an unknown number of times. The loop exits if some logical expression or its result turns out to be false.


Since the validity of the logical expression is checked at the beginning, the body of the loop may not be executed even once.

Loop structure

WHILE


Block - cycle diagram

operator


condition

Example

Task: Write a program that calculates the sum of all even numbers up to 50. writeln("The sum is: ",sum); Task Write a program that searches for n!. Loop with postcondition in Pascal – REPEAT-UNTIL


with precondition

This operator is similar to the loop operator with a precondition, but differs from it in that the condition is checked after the body (actions) of the loop are executed. This ensures that it is executed at least once, unlike previously parsed loops.


with postcondition This operator is similar to the loop operator with a precondition, but differs from it in that the condition is checked after the body (actions) of the loop are executed. This ensures that it is executed at least once, unlike previously parsed loops.

Loop with precondition in Pascal - WHILE

A loop operator with a precondition performs actions an unknown number of times. The loop exits if some logical expression or its result turns out to be false.


Since the validity of the logical expression is checked at the beginning, the body of the loop may not be executed even once.

Please note that this loop operator assumes the presence of several operators in the body of the loop, that is, several actions can be performed, so the service words

Begin

And

End


Block - cycle diagram

Not needed.


REPEAT-UNTIL

Task: Write a program that determines the sum of the first and last digits in a number. a,b,c,d:integer; writeln("enter a number");

writeln(‘The sum of the first and last digit is:‘c); a,b,c,d:integer; Write a program that determines whether a number is prime.

Loop with a parameter in Pascal - FOR Cycle FOR sets the condition under which the program will work before it is executed, let’s say you need to loop the program n times, then this can be easily done using this loop. ).


with precondition a,b,c,d:integer;

U cycle

There is a characteristic feature - a counter, which is usually designated by the letter i or j.

FOR i:= n2 DOWNTO n1 DO

2nd recording form


with postcondition a,b,c,d:integer;

i:= n1 … n2

Loop body


Since the validity of the logical expression is checked at the beginning, the body of the loop may not be executed even once.

Task: Write a program that calculates the nth power of a given number.

a, n, i, pr: integer;

writeln('Enter a number');

writeln('Enter the power of the number');

for i:= 1 to n do

writeln('The power of the number is',pr);


Block - cycle diagram

Write a program that finds the number P = (1-1/2)(1-1/3)*…*(1-1/n).

N is entered from the keyboard.
























Back forward

Attention! Slide previews are for informational purposes only and may not represent all the features of the presentation. If you are interested in this work, please download the full version.

Target: studying the algorithmic structure of cycles, creating models and algorithms for solving practical problems.

During the classes

I. Updating knowledge

  • Review the concept of an algorithm and the basic constructs of an algorithmic language.
  • Be able to develop a mathematical model, algorithm and block diagram for solving a problem.
  • Have an understanding of programming languages ​​and their purposes.
  • Be able to work in a programming environment.
  • Know program structures.
  • Be able to write expressions containing numerical and symbolic quantities.
  • Know the structures of operators and the features of their work.
  • Be able to use operators when writing programs with linear and branching structures.
  • Be able to create and run programs on a computer for debugging.

II. Theoretical material of the lesson

Most practical problems require repeated repetition of the same actions, that is, reuse of one or more operators. (Presentation)

Suppose you need to enter and process a sequence of numbers. If there are only five numbers, you can create a linear algorithm. If there are a thousand of them, it is possible to write a linear algorithm, but it is very tedious and irrational. If the number of numbers is unknown at the time the algorithm is developed, then a linear algorithm is fundamentally impossible.

Another example. To find a person's last name on the list, you need to check the first last name on the list, then the second, third, etc. until the desired one is found or the end of the list is reached. You can overcome such difficulties with the help of cycles.

A cycle is a section of an algorithm (program) that is executed repeatedly. Accordingly, a cyclic algorithm is an algorithm containing cycles.

There are two types of cycles: with a known number of repetitions and with an unknown number of repetitions. In both cases, this refers to the number of repetitions at the algorithm development stage.

There are 3 types of cyclic structures:

  • Loop with precondition;
  • Loop with postcondition;
  • Loop with parameter;

Otherwise, these structures are called cycles like “While”, “Before”, “For”.

Graphic form of recording data of algorithmic structures:

Loop with precondition (aka loop Bye) has the form:

A loop operator with a precondition performs actions an unknown number of times. The loop exits if some logical expression or its result turns out to be false. – expression of logical type.

The loop may not be executed even once if the value of the logical expression immediately turns out to be false.

The series of commands between begin and end are executed until while the condition is true .

For that for the cycle to end, it is necessary that the sequence of instructions between BEGIN and END changes the value of the variables included in condition.

Loop with postcondition (aka loop before) has the form:

A loop operator with a precondition performs actions an unknown number of times. The loop exits if some logical expression or its result turns out to be false. – expression of logical type.

Note:

Sequence of instructions betweenrepeat Anduntil will always be fulfilled at least once;

In order for the loop to complete, it is necessary that the sequence of statements betweenrepeat Anduntil changed the values ​​of the variables included in the condition expression.

The repeat instruction, like the while instruction, is used in a program if it is necessary to carry out some repeated calculations (a loop), but the number of repetitions is not known in advance and is determined by the progress of the calculation itself.

Loop with a parameter (aka loop For) has the form:

i – cycle parameter;
a – initial value of the cycle;
b – final value of the cycle;
h – parameter change step.

The structure of this cycle is otherwise called cycle i times.

This command is executed in this way: the parameter i is set to the initial value a, compared with the final value b, and if it is less than or equal to the final value b, a series of commands is executed. The parameter is assigned the value of the previous one, increased by h– step of parameter change and is again compared with the final value b.

In the Pascal programming language, the parameter change step can be equal to one or minus one.

If there is only one statement between begin and end, then operator brackets do not need to be written. This rule works for loops like “While” and “For”.

Let's look at an example of solving problems using these structures

Example.

Calculate the product of numbers from 1 to 5 using various loop options

Mathematical model:

Р= 1·2·3·4·5=120

Let's compose the algorithm in the form of a block diagram.

To check the correctness of the algorithm, let's fill in the trace table.

Step Operation R i Condition check
1 P:=1 1
2 i:=1; 1 1
3 i<=5
P:=P*I
i:=i+1
1 1 1<=5, да (истина)
4 i<=5
P:=P*I
i:=i+1
2 2 2<=5, да (истина)
5 i<=5
P:=P*I
i:=i+1
6 3 3<=5, да (истина)
6 i<=5
P:=P*I
i:=i+1
24 4 4<=5, да (истина)
7 i<=5
P:=P*I
i:=i+1
120 5 5<=5, да (истина)
8 i<=5
P:=P*I
i:=i+1
6<=5, нет (ложь)

Checking a condition occurs in several steps: checking the condition and executing commands on one of the branches. Therefore, the trace table does not record algorithm commands, but individual operations performed by the computer at each step.

Step one: P is assigned a value of one.

Step two: i is assigned the value one.

Step three: when i is equal to one, we check the condition one is less than or equal to five, yes, the condition is true, which means P is assigned the value one multiplied by one, there will be two. For i: one plus one equals two.

Step four: when i is equal to two, we check the condition two is less than or equal to five, yes, the condition is true, which means P is assigned the value 2 times one, it will be 2. For i: two plus one, it will be three.

Step five: with i equal to three, we check the condition three is less than or equal to five, yes, the condition is true, which means P is assigned the value of two multiplied by three, it will be six. For i: three plus one equals four.

Step six: with i equal to four, we check the condition four is less than or equal to five, yes, the condition is true, which means P is assigned the value of six times four, it will be twenty-four. For i: four plus one equals five.

Step seven: with i equal to five, we check the condition five is less than or equal to five, yes, the condition is true, which means P is assigned the value of twenty-four multiplied by five, it will be one hundred and twenty. For i: five plus one is six.

Step eight: when i is equal to six, we check the condition six is ​​less than or equal to five, no, the condition is false, then we exit the loop, and as a result we get the last value equal to one hundred and twenty.

Program Pr1;
Var i: integer;
writeln("The sum is: ",sum);
P:=1;
i:=1;
While i<=5 do
begin
P:=P*i;
i:=i+1;
end;
Write('P=', P);
end.

For a loop with a postcondition, we will build a block diagram and a trace table. (slide16)

As a result, we get the last value equal to one hundred and twenty at the seventh step

And for the Cycle with a parameter we will build a block diagram and a trace table. (slide17)

As a result, we get the last value equal to one hundred twenty at the sixth step

Task:

Display numbers from 1 to 5 in:

  1. direct order;
  2. in reverse order.

Mathematical model:

  1. 1 2 3 4 5;
  2. 5 4 3 2 1.

The block diagram and program for solving the problem are presented for numbers in forward and reverse order.

(slide 21)

Let us write the considered algorithms in the Pascal programming language.

(slide 22)

III. Summing up the lesson

And so we considered the following questions:

  1. Algorithmic structure cycle;
  2. Types of algorithmic structures:
    1. Loop with precondition;
    2. Loop with postcondition;
    3. Loop with parameter;
  3. We looked at ways to record these structures;
  4. We looked at examples of solving problems using these structures.





Loop with a precondition If the number of repetitions is unknown in advance, but is specified only by a condition, and an action that must be performed only after checking the conditions, use a loop with a precondition. A logical expression is used as a condition, the body of the loop is a simple or compound operator. Before each execution of the loop body, the condition is checked, if the result is “true”, then the loop body is executed again, if “false”, then the loop is exited. On the block diagram In Pascal begin end; Condition Body of the loop No Practice Start of the loop End of the loop YES While do


Loop with a postcondition If the number of repetitions is unknown in advance, but is specified only by a condition, and the action that must be performed before checking the condition, use a loop with a postcondition. A logical expression is used as a condition, the body of the loop is a simple or compound operator. After each execution of the loop body, the condition is checked, if the result is “false”, then the loop body is executed again, if “true”, then the loop is exited. On the block diagram In Pascal Repeat Condition Loop Body Yes No Practice Until ;


Loop with a parameter In cases where the number of repetitions is known in advance, a loop with a parameter is used. The variable that specifies the number of repetitions is called a loop parameter or control variable. After each execution of the loop body, the control variable is increased or decreased, the loop is executed until it exceeds or becomes less than the limit. In the block diagram in Pascal, X is the control variable (cycle parameter) A is the initial value of X, B is the final value of X C is the step of changing X. As a step you can only use: “to” = 1; “downto” = -1 X:=A,B,C Loop Body Practice For X:=A to B do Begin End;


An example of a problem using a loop with a precondition Raise the number 3 to a given power TASK: Verbal algorithm: Multiply the number X initially equal to 1 a given number of times (H) by 3. start H BHBH X:=1 X:=X*3 end X Enter the given degrees Initial values ​​“B” degree counter B=B+1 Multiplying by 3 Increasing the counter Outputting the resulting value Programm Stepen; Var H,B,X:integer; Begin Writeln(Degree?); Readln(H); X:=1; B:=1; While B


H X:=1 X:=X*3 end X Enter a given power Initial values" title="Example of a task using a loop with a postcondition Raise the number 3 to a given power TASK: Verbal algorithm: Multiply the number X initially equal to 1 given number of times (H) for 3. start N B>=H X:=1 X:=X*3 end X Enter a given degree Initial values" class="link_thumb"> 8 !} An example of a problem using a loop with a postcondition Raise the number 3 to a given power TASK: Verbal algorithm: Multiply the number X, initially equal to 1, a specified number of times (H) by 3. start H B>=H X:=1 X:=X*3 end X Entering a given degree Initial values ​​“B” degree counter B=B+1 Multiplying by 3 Increasing the counter Outputting the resulting value Programm Stepen; Var H,B,X:integer; Begin Writeln(Degree?); Readln(H); X:=1; B:=0; Repeat X:=X*3; B:=B+1; Until B>=H; Writeln(Result,X); End. No Yes Pascal Theory Block Diagram Explanations B:=0 =H X:=1 X:=X*3 end X Entering a given degree Initial values"> =H X:=1 X:=X*3 end X Entering a given degree Initial values ​​"B" degree counter B=B+1 Multiplying by 3 Increase the counter Output the resulting value Programm H,B,X:integer;Readln(H):=1; =B+1; Until B>=H; Writeln (Result,X); No Yes Theory Pascal Block Diagram Explanations B:=0"> =H X:=1 X*3 end X Enter the specified degree Initial values" title=" Example of a problem using a loop with a postcondition Raise the number 3 to a given power TASK: Verbal algorithm: Multiply the number X initially equal to 1 a given number of times (H) by 3. beginning N B>=H X: =1 X:=X*3 end X Entering the specified degree Initial values"> title="An example of a problem using a loop with a postcondition Raise the number 3 to a given power TASK: Verbal algorithm: Multiply the number X initially equal to 1 a given number of times (H) by 3. start H B>=H X:=1 X:=X*3 end X Entering a given degree Initial values"> !}


An example of a task using a loop with the parameter Raise the number 3 to a given power TASK: Verbal algorithm: Multiply the number X, initially equal to 1, a specified number of times (H) by 3. start H X:=1 X:=X*3 end X Enter a given power Initial value X=1 Parameters from 1 to N Multiplication by 3 Output of the resulting value Programm Stepen; Var H,B,X:integer; Begin Writeln(Degree?); Readln(H); X:=1; For B:=1 to H do Begin X:=X*3; End; Writeln(Result,X); End. B:=1,H,1 Pascal Theory Block Diagram Explanations




Task: Having started training, the athlete ran 10 km on the first day. Every day he increased the daily norm by 10% of the previous day's norm. What is the total distance the athlete will cover in 7 days? Input variables: Output variables: S – total path d – number of days Sd – distance for the current day


End Questions for control: 1. Which operator in Pascal defines a loop with a precondition 2. How to specify the step “1” and “-1” in a parameter in a loop 3. Which branch does the loop with a postcondition follow? 4. Is there a condition parameter 5. What can be the body of a loop 6. When is a loop with parameters used

Did you like the article? Share it