Cyclic algorithms Types of cycles and cyclic commands on Pascal. Presentation on the topic "Programming of cyclic algorithms in Pascal" Cycle with a postcalm in Pascal - Repeat-Until























Back forward

Attention! Preview slides is used exclusively for informational purposes and may not provide ideas about all presentation capabilities. If you are interested in this work, please download the full version.

Purpose: Studying the algorithmic structure of the cycles, the creation of models and algorithms for solving practical problems.

During the classes

I. Actualization of knowledge

  • Repeat the concept of the algorithm, the basic designs of the algorithmic language.
  • To be able to develop a mathematical model, the algorithm and block scheme for solving the problem.
  • Have the concept of programming languages \u200b\u200band their appointment.
  • Be able to work in the programming environment.
  • Know the structure of the program.
  • Be able to record expressions containing numeric and character values.
  • Know the structure of operators and features of their work.
  • To be able to apply operators when writing programs with linear and branching structures.
  • To be able to create and run programs for debugging.

II. Theoretical material lesson

Most practical tasks require multiple repetition of the same actions, i.e. the reuse of one or more operators. (Presentation)

Let it be necessary to enter and process the sequence of numbers. If the numbers are only five, you can make a linear algorithm. If there are thousands of them, write a linear algorithm, but very tedious and irrational. If the number of numbers by the time the algorithm is unknown, the linear algorithm is fundamentally impossible.

Another example. To find the surname of a person in the list, you need to check the first name of the list, then the second, third, etc. Until the desired or the end of the list is found or will not be found. You can overcome such difficulties with cycles.

The cycle is called a multiple executable portion of the algorithm (Programs). Accordingly, the 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. At the same time, in both cases, there is a number of repetitions at the stage of development of the algorithm.

There are 3 types of cyclic structures:

  • Cycle with precondition;
  • Cycle with afterclination;
  • Cycle with parameter;

Otherwise, these structures are called "Until", "for" type cycles.

Graphic form of data recording algorithmic structures:

Cycle with precondition (otherwise cycle until) It looks:

condition - logical expression.

The cycle may not be executed if the value of the logical expression immediately turns out to be a lie.

Series of commands located between Begin and End are performed until so far, the condition is true .

For to the cycle end, it is necessary that the sequence of instructions between Begin and End change the value of the variables included in condition.

Cycle with postcondition (otherwise cycle before) It looks:

condition - logical expression.

Note:

Sequence of instructions between eachrepeat. anduntil will always be fulfilled at least once;

In order for the cycle to be completed, it is necessary that the sequence of operators betweenrepeat. anduntil Changed the values \u200b\u200bof the variables included in the expression condition.

The Repeat instruction, as well as the WHILE instruction, is used in the program, if you need to conduct some repetitive calculations (cycle), but the number of repetition is not known in advance and is determined by the number of calculation itself.

Cycle with a parameter (otherwise cycle for) It has the form:

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

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

This command is done in this way: the parameter i is assigned the initial value A is compared with the final value B and, if it is less than or equal to the final value B, a series of commands is performed. The parameter is assigned the value of the previous, enlarged h. - Steps of changes in the parameter and again compared with the final value b.

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

If only one operator is between Begin and End, the operator brackets can not write. This rule works for a "yet" and "for" type cycle.

Consider an example of solving tasks using these structures.

Example.

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

Mathematical model:

P \u003d 1 · 2 · 3 · 4 · 5 \u003d 120

Let's make an algorithm in the form of a flowchart.

To verify the correctness of the algorithm, fill the trace table.

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

Verification Conditions occur in several steps: checking the terms and execution of commands on one of the branches. Therefore, the trace table does not record the algorithm commands, but separate operations performed by the computer at each step.

Step one: P is assigned a value one.

Step two: i is assigned to one.

Step Third: at i equal unit checking the condition alone less than or equal to five, yes, the condition is true, it means that the value of one multiplied to one will be two. For I: one plus one, there will be two.

Step four:at i equal to two, the condition is currently less than or equal to five, yes, the condition is true, it means that the value of 2 multiplied to one, will be 2. for i: two plus one, there will be three.

Pitch Fifth:at i equal to three, we check the condition three less or equal to five, yes, the condition is true, which means P assigns the value of two multiplied by three, there will be six. For i: Three plus one, there will be four.

Step Six:at i equal to four, the condition is four less or equal to five, yes, the condition is true, it means that the value of six multiplied by four will be twenty-four. For I: Four plus one, there will be five.

Step seventh:for i equal to five, the condition is five less or equal to five, yes, the condition is true, it means that the value of twenty four multiplied by five will be one hundred and twenty. For I: Five plus one, there will be six.

Step eighth:at i equal to six, the condition is six less or equal to five, no, the condition is false, then we leave the cycle, and as a result we obtain the last value equal to one hundred and twenty.

Program PR1;
VAR I: Integer;
Begin.
P: \u003d 1;
i: \u003d 1;
While I.<=5 do
Begin.
P: \u003d p * i;
i: \u003d i + 1;
end;
Write ('p \u003d', p);
end.

For a cycle with a deselation, we construct a block diagram and trace table. (slide16)

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

And for a cycle with a parameter, we build a block diagram and trace table. (slide17)

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

A task:

Display numbers from 1 to 5 V:

  1. straightforward;
  2. reverse order.

Mathematical model:

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

The block diagram and the problem of solving the problem is presented for numbers in direct and reverse order.

(Slide 21)

We record the considered algorithms in the programming language Pascal.

(Slide 22)

III. Summing up the lesson

And so we reviewed the following questions:

  1. Algorithmic structure cycle;
  2. Types of algorithmic structures:
    1. Cycle with precondition;
    2. Cycle with afterclination;
    3. Cycle with parameter;
  3. Considered ways to write these structures;
  4. Disassembled examples of solving problems with the help of these structures.

Types of cycles

cycles with parameter for

cycles with precondition

cycle while with precondition

cycle repeat - Until with postlagogo


Cycle with precondition in Pascal - While

The cycle operator with the precondition performs the actions in advance of the unknown number. The output from the cycle is carried out if some logical expression or its result will be false.

Since the loyalty of the logical expression is checked at the beginning, the cycle body may not be fulfilled.


Cycle structure While


Block - Cycle Scheme While

operator

condition


Example

Task: Write a program that calculates the sum of all even numbers to 50.

writeln ("Amount equal:", SUM);


A task

Write a program that is looking for N!.


Cycle with Pascal Pascal - Repeat-Until

This operator is similar to the operator of the cycle with the precondition, but differs from it to the fact that the validation conditions are made after the body (actions) of the cycle. This ensures its execution at least once unlike previously disassembled cycles.

Note that this cycle operator assumes the presence of several operators in the cycle body, that is, you can perform several actions, so service words Begin. and End. not needed.


Cycle structure

Repeat-Until.


Block - Cycle Scheme Repeat-Until.

operator

condition


Example

Task: Write a program that determines the amount of the first and last digits among.

a, B, C, D: Integer;

writeln ("Enter the number");

writeln ('The sum of the first and last digit is equal to:' C);


A task

Write a program that determines whether the number is simple.


Cycle with parameter in Pascal - for

Cycle For specifies the condition for which the program will work before it is fulfilled, let us say n times a program, then it is easy to do with this cycle.

At cycla For there is a characteristic feature - a counter that is usually denoted by the letter I or J.

In the cycle, the counter can be asked as in the literal (official word to. ) and in reverse order (official word downto. ).


Cycle structure For

For i: \u003d n1 to n2 do

1 - recording form

For i: \u003d N2 Downto N1 Do

2 - recording form


Block - Cycle Scheme For

i: \u003d N1 ... N2

Cycle body


Example

Task: Write a program that calculates the N-si degree of a specified number.

a, N, I, PR: INTEGER;

writeln ('Enter the number ");

writeln ('Enter the degree of the number ");

for i: \u003d 1 to n do

writeln ('The degree of number is equal to', PR);


A task

Write a program that finds the number P \u003d (1-1 / 2) (1-1 / 3) * ... * (1-1 / N).

N Entered from the keyboard.


Cyclic algorithms06.04.2017
Cyclic algorithms
Types of cycles and cyclic
Teams on Pascal

Cycle is repeated
Repetition of sequence
action
Repeating part of the algorithm
called cycle body
Types of cycles
With a given number
Repetitions
Terms condition
cycle
With condition
The exit condition is
cycle

Types of cycles (content)
Cycle with precondition
Practice
Cycle with postband
Practice
Cycle with parameters
Practice
Solution of complex tasks

Cycle with precondition

Practice

condition and the action that needs to be performed only
After checking the conditions use the cycle in the precondition.


Before each execution of the cycle body is checked
conditions, if the result of "truth", then the cycle body is performed
Once again, if "lie", then there is a way out of the cycle.
On the block - scheme
Start cycle
Not
Condition
YES
Cycle body
End of cycle
In Pascal.
While<условие> do.
Begin.
<тело цикла>
end;

Cycle with postband

Practice
If the number of repetitions is unknown in advance, but only specified
condition and the action that must be done before
Condition checks use cycle with postcal.
As a condition, a logical expression is used, body
Cycle is a simple or composite operator.
After each execution of the cycle body, checks
conditions if the result is "false", then the cycle body is performed
Once again, if the "truth", then there is a way out of the cycle.
On the block - scheme
In Pascal.
Repeat.
Cycle body
<тело цикла>
Yes
Not
Condition
Until.<условие>;

Cycle with parameter

Practice
Cycle with parameter
In cases where the number of repetitions is known in advance
A cycle is used in the parameter.
A variable specifying the number of repetitions is called
The cycle parameter or control variable.
After each body execution cycle managing
The variable increases or decreases, cycle
It is run until it is time until it exceeds either
will be less limitations.
On the block - scheme
In Pascal.
For x: \u003d a to b doo
X: \u003d A, B, C
Cycle body
X - control variable (cycle parameter)
A - initial meaning x, in - finite x
C - a step of change x
Begin.
<тело цикла>
End;
As a step you can use
only:
"To" \u003d 1;
"Downto" \u003d -1

Example of a task using a cycle with a precondition
Theory

Sliver algorithm:
Multiply the number x is initially equal to 1
The specified number of times (H) is 3.
Start
PROGRAMM STEPEN;
Var.
H, B, X: Integer;
Begin.
Writeln ('degree?' ');
Readln (h);
X: \u003d 1;
B: \u003d 1;
While B.<=H do
Begin.
X: \u003d x * 3;
B: \u003d B + 1;
End;
Writeln ('result', x);
End.
Pascal
N.
Enter a given degree
X: \u003d 1
Initial values
B: \u003d 1
Not
"B" degree counter
B≤h.
Yes
X: \u003d x * 3
Multiplication by 3.
B \u003d in + 1
Increase meter
H.
The derocation of the received
Values
end
Block diagram
Explanations

An example of a task using a cycle with a postcal
Theory
Task: raise the number 3 in a given degree
Sliver algorithm:

PROGRAMM STEPEN;
Var.
H, B, X: Integer;
Begin.
Writeln ('degree?' ');
Readln (h);
X: \u003d 1;
B: \u003d 0;
Repeat.
X: \u003d x * 3;
B: \u003d B + 1;
Not
Until b\u003e \u003d H;
Writeln ('result', x);
End.
Start
N.
Enter a given degree
X: \u003d 1
Initial values
B: \u003d 0
Multiplication by 3.
X: \u003d x * 3
Increase meter
B \u003d in + 1
Yes
B\u003e \u003d H
"B" degree counter
H.
The derocation of the received
Values
end
Pascal
Block diagram
Explanations

An example of a task using a cycle with a parameter
Theory
Task: raise the number 3 in a given degree
Sliver algorithm:
Multiply the number x is initially equal to 1 specified number of times (H) by 3.
PROGRAMM STEPEN;
Var.
H, B, X: Integer;
Begin.
Writeln ('degree?' ');
Readln (h);
X: \u003d 1;
For b: \u003d 1 to h doo
Begin.
X: \u003d x * 3;
End;
Writeln ('result', x);
End.
Pascal
Start
N.
X: \u003d 1
B: \u003d 1, H, 1
X: \u003d x * 3
H.
end
Block diagram
Enter a given degree
The initial value x \u003d 1
Parameters from 1 BC
Multiplication by 3.
The derocation of the received
Values
Explanations

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

Task: Starting training, athlete on the first day
ran 10 km. Every day he increased daylight
Norma for 10% of the norm of the previous day.
What a total path runs athlete for 7 days.
Input variables:
d - number of days
SD - Distance for Current Day
Output variables:
S - shared way

Block - scheme to solve

start
S: \u003d 10
SD: \u003d 10
D: \u003d 1
D: \u003d D + 1
SD: \u003d SD * 1.1
S: \u003d S + SD
not
D \u003d 7.
Yes
S.
end

Program on Pascal

Cycle "for"
Cycle "While"
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: \u003d 10;
S: \u003d 10;
S: \u003d 10;
SD: \u003d 10;
SD: \u003d 10;
SD: \u003d 10;
For d: \u003d 2 to 7 Do
Begin.
While D.<7 do
Begin.
Repeat.
D: \u003d D + 1;
SD: \u003d 1.1 * SD;
D: \u003d D + 1;
SD: \u003d 1.1 * SD;
S: \u003d S + SD;
SD: \u003d 1.1 * SD;
S: \u003d S + SD;
end;
S: \u003d S + SD;
Until (d \u003d 7);
Writeln ('s \u003d', s);
end;
Writeln ('s \u003d', s);
End.
Writeln ('s \u003d', s);
End.
End.

Questions for control:
1. What operator in Pascal is set cycle with
Provote
2. As in the cycle in the parameter indicate the step "1" and "-1"
3. What branch comes from the cycle with
postcondition
4. Is there a cycle with the condition parameter
5. What could be the cycle body
6. When the cycle is used with parameters
the end

Slide 2.

Plan

Cycle concept Operator Cycle for cycle while cycle Repeat literature

Slide 3.

Literature

Kastornov A.F., Evstratova G.A. Programming language Pascal: Tutorial for universities. - Cherepovets: GOU VPO CHSU, 2010. - 117 c. - Bibliogr.: P.114. Electronic textbook on programming language Pascal /Http://pascal.guti.ru Plan

Slide 4.

Cycle concept

Algorithms for solving many tasks are cyclical, in which, to achieve the result, a certain sequence of actions is performed several times. For example, the knowledge control program displays a question, accepts the answer, adds a mark for the answer to the amount of points, then repeats these actions until the subject responds to all questions. Or, for example, to search for the desired last name in the list, check the first name of the list to match the coincidence, then the second, third, etc. As long as the desired surname is found or the end of the list will be found.

Slide 5.

The algorithm in which there is a group of operators performed several times is called cyclic. A group of repeated operators is called a cycle body. In Pascal, cycles can be implemented using for, While and Repeat cycle operators. Plan

Slide 6.

Cycle operator for

The FOR cycle operator is used if the cycle body must be performed several times, and the number of repeats is known in advance.

Slide 7.

1st form of recording operator Cycle for

The 1st form of recording Operator for Operator is generally described as follows: Forschechik: \u003d initial_treatmentTo-opposite_Dorator; Where for, to, do - service words. The meter is a variable of a sequence type (usually such as Integer), which determines the number of cycle repetitions. The number of repetitions is considered according to the formula: the final_dative is the initial_station + 1. The final_ value should be greater than or equal to the initial_name.

Slide 8.

If the cycle body consists of several operators, then the 1st form of recording of the Operator for the Operator looks like this: Fant

Slide 9.

Consider the algorithm for the work of the FOR cycle in the first form of recording. The counter is assigned to the initial value. Condition is checked: the counter value is greater than the final_station? If the condition is true (yes), the cycle execution ends. If the condition is false (no), the cycle body is performed, then the counter value increases by one and the conditions check again, i.e. p.

Slide 10.

2nd form of recording operator Cycle for

The 2nd form of recording Operator for Operator is generally described as follows: For counter: \u003d initial_downDOWNTO-end_dactivity. Where: For, Downto, Do - Service Words. The counter is a variable of the sequence type (usually such as Integer), which determines the number of cycle repetitions. The number of repetitions is considered by the formula: the initial_ value-finite_dation + 1. Initial_namedInteriority to be greater than or equally intended.

Slide 11.

If the cycle body consists of several operators, then the 2nd form of recording of the Operator for the operator looks like this: Forschechik: \u003d Initial_DOWNTOFO-EVENINGDO BEGIN // Body Cycle END;

Slide 12.

Consider the algorithm for the operation of the FOR cycle in the second form of recording: the counter is assigned the initial value. Condition is checked: The counter value is less than the final_station? If the condition is true (yes), the cycle execution ends. If the condition is false (no), then the cycle body is performed, then the counter value decreases per unit and the conditions check again, i.e. p.

Slide 13.

Cycle operator for

pROGRAMEX1; VAR I, N: Integer; (i - counter, n - the required number of stars) S: String; (S - the generated string line) Begin Writeln ("Enter the number of stars"); (the number of asterisks) readln (n) is requested; (the user enters the number of spars n) s: \u003d ""; (The formation of the stroke string begins with an empty string) (the string is formed along the FOR cycle. The initial_nity is meter - 1, the final_tility is the required number of spars n.) fori: \u003d 1 to n do s: \u003d s + "*"; (at each step of the cycle to the string, one asterisk) Writeln (S) is glued; (Line is displayed) ReadLN; end. Plan example: the program forms the string string. The number of stars in the string is determined by the user.

Slide 14.

While cycle

The WHILE cycle is used if the number of cycle's body repetitions during the program is unknown and can only be determined during its operation. In general, the WHILE statement is written as follows: WHILE CONDITION OF DOUPERATOR; Where While, Do - Service Words. Condition is a logical type expression that determines the continuation of the cycle.

Slide 15.

If the cycle body consists of several operators, the WHILE cycle is written as follows: Wholeboard Do Begin // Body Cycle END;

Slide 16.

Consider the algorithm of the WHILE cycle: a condition is checked. If the condition is truly, the cycle body is performed. After that, the condition is checked again. If the condition is false, the cycle is completed.

Slide 17.

Thus, when the cycle with the precondition or cycle "while" (the cycle body is still true condition). If, when you first pass the cycle, the condition will be false, the cycle body will never be fulfilled. If the condition never becomes false, the cycle will be repeated infinitely, i.e. Cold will occur.

Slide 18.

Program EX2; VaracCount: Real; (account size) MONTH: Integer; (Number of months since the opening of the account) Begin Account: \u003d 1000; (at the expense put 1000 rubles) MONTH: \u003d 0; (The account has just opened) WhileAccount

Slide 19.

Repeat cycle

The Repeat cycle, as well as the WHILE cycle, is used in the program if you need to perform a cycle body several times, but the number of repetitions is unknown in advance. In general, the Repeat cycle is written as follows: repeat // Body Cycle Until Condition; Where Repeat, Until - Service Words. Condition is a logical type expression that determines the end of the cycle.

Slide 20.

Consider the operating algorithm of the REPEAT cycle: the repeat and until cycle body is performed between the reserved words. The condition is checked. If the condition is truly, the cycle is completed. If the condition is false, the cycle body is again performed.

Slide 21.

Thus, Repet is a cycle with a post-wave or cycle "To" (the cycle body is performed to the truth of the condition). Consequently, the cycle body is performed at least once. If the condition never becomes true, the cycle will become infinite.

Slide 22.

Program EX3; VAR Time: Integer; (Delivery time) Cells: Integer; (cell number) Begin Time: \u003d 0; (cell still never started) Cells: \u003d 1; (single) Repeat Time: \u003d Time + 3; (after the next three hours) Cells: \u003d Cells * 2; (the number of cells increased by 2 times) Until Cells\u003e 24; (up to truth condition "Number of cells more than 24") Writeln (Time); (output) ReadLN; end. Factory Example: Unicellular Ameba Every 3 hours is divided into 2 cells. Determine, after how many hours the number of cells exceed 24.

View all slides