Contacts

Methodology tabulation and programming functions. Programming cyclic computing processes Programming cyclic computing processes vba

In programming, we are often faced with tasks in which processes occur that are repeated. Therefore, we must know and be able to use such a concept as “ cyclical computing processes».

It will be easy for a novice programmer to understand them using a generalized example. Moreover, it is important to understand that in all programming languages ​​there are ways to implement loops.

What is a loop in programming?

Cycle - in programming, it is called multiple repetition of the same actions or calculations, but according to the same dependencies with different values ​​of variables.

We come across the concept of a cycle not only in programming. There are cycles in many areas of our life.

For example - the water cycle in nature, this is a natural cycle in our life.

Now let's look at the general rules and concepts used in computing cycles.

Cyclic process stages

In general, the cycle should be implemented in 4 stages:
  • Stage 1 - preparation of the cycle (initialization).
    Setting an initial value to a parameter and a loop variable.
    Loop parameter- this value, which counts the number of steps of the cycle (the number of repetitions of the cycle).
    Loop variable Is a value that changes its value at each stage of the cycle.
    Initialization Is the assignment of initial values ​​to the parameter and the variable of the loop.
  • Stage 2 - the body of the cycle.
    This is a multiple repetition of an action in a loop or calculations on the same mathematical dependencies with different values ​​of variables.
  • Stage 3 - modification (change) of the cycle.
  • Stage 4 - cycle management.
    This is a check of the condition for the continuation or the beginning of the cycle.
There are 3 loop operators in pascal that can implement any algorithmic - cyclic structure :
  1. Loop operator with parameter
  2. Loop operator with precondition
  3. Loop operator with postcondition
We will consider them in detail in the next article.

Objective:

Learn cyclic operators for, while, do - while, learn to compose and program cyclic algorithms.

Brief theoretical information

Loop operators are used when it is necessary to repeat some actions (operators and operations) several times, and such sections of algorithms are called loops.

For loop operator

The main form of the for loop operator is

for (expression_1; expression_2; expression_3)

operator;

where expression_1- initial value of the cycle parameter;

expression_2- checking the condition for the continuation of the cycle;

expression_3- change of the cycle parameter (correction);

operator- simple or compound operator of the C language.

The operator's work scheme is as follows: expression_1 is calculated only once, then expression_2 is checked, and if it is “true”, then the cyclic section of the program is executed, then the parameter is corrected, and so on until expression_2 takes the value “false”.

For instance: for (k = 1; k<5; k++)

printf (“\ n% d”, k);

As a result of executing this operator, numbers from 1 to 4 are printed in a column.

A variable of any base type can be used as a loop parameter.

For instance:

for (ch = 'a'; ch<=’z’; ch++) // Вывод на экран букв

printf (“% c”, ch); // latin alphabet

It is necessary to carefully control the structure of the for loops in the program so that an infinite loop (from which there is no way out) does not turn out.

for instance:

for (k = 10; k> 6; k ++)

printf (“endless loop \ n”);

Exit the loop ahead of schedule in the following ways:

By additional condition;

Using the following operators:

break;- exit from the loop in which break is located, control is transferred to the first executed statement after the loop;

exit (int Kod);- exit from the program;

return;- exit from the function;

Using the unconditional jump operator goto<метка>;

Early completion of the current cycle step possible with an additional condition or operator continue, which interrupts the execution of the current step of the loop, i.e. skips statements from the rest of the loop and transfers control to the head statement of the loop to correct the parameter and check the condition.

It is prohibited to transfer control from the outside to the inside of the cycle.

Any of the expressions in the for loop in parentheses may be missing, but the ";" can not be omitted.

for instance:

for (; i<3; i++)

puts (“Hello!”);

Loop statements while and do – while

The basic form of a cyclic operator while:

While (condition)

operator;

where operator

The loop is executed as long as the condition is “true”, i.e. a parenthesized expression returns a nonzero result. This is a loop with a precondition - first the condition is checked, then the statement is executed. Therefore, the while loop will not be executed even once if the initial result of evaluating the condition is 0.

Basic operator form do - while:

operator;

while (condition);

where operator Is a simple, compound, or empty operator.

Operator dowhile- loop operator with postcondition, i.e. the statement is executed first, and then the condition is checked for true. Since in the do – while loop the condition is checked at the end of the loop, the loop will be executed at least once.

In loops of the while and do – while type, the same methods of early exit from the loop and early termination of the current step of the loop are allowed as in the for statement, but in the latter case, unlike the for loop, control is transferred to check the condition. To prevent an infinite loop inside the while and do – while loops, it is necessary to provide for changing the variables included in the condition.

For instance:

for (i = 1; i<=300;i++) // Печать целых чисел, кратных 5

if (i% 5! = 0) continue;

printf (“% 5d”, i);

Examples of infinite loops:

operator;

2) while (number_not_0) // Always true!

operator;

operator;

while (number_not_0); // Always true!

There must be an exit condition among the loop operators.

Nested loops

In the case of nested loops, one loop is inside another, for example:

for (i = nn; i

for (j = mn; j

operator;

where operator Is a simple, compound, or empty operator. The inner loop will run for every value of the i parameter that meets the condition of the outer loop.

Example:

for (i = 1; i<10;i++) // Печать таблицы умножения

for (j = 1; j<4;j++)

printf (“\ n% d *% d =% 2d”, i, j, i * j);

printf (“\ n”);

An example of using the for statement

Calculate. The program should print intermediate and final results.

The program text may look like

#include

#include

puts (“Enter N”);

scanf (“% d”, & N);

for (s = 0, k = 1; k<=N; k++) // В заголовке цикла можно выпол-

(// nat and double assignment

printf ("\ n k =% d s =% f", k, s);

printf ("\ n ANSWER: s =% f, Press any key ...", s);

Options for individual assignments

Create a program to define a table of function values at in an arbitrary range [ a,b] argument changes X with an arbitrary step h... The values a, b, h entered from the keyboard. The table must contain the following columns: ordinal, argument value x, the value of the function, a message about the increase or decrease of the function, the difference between two adjacent values ​​of the function.

Determine the maximum and minimum values ​​of the function.

1. a = -p; b = p; h = 0.4.

2. a = 0.7; b = 1.8; h = 0.1.

3. a = -0.5; b = 2.5; h = 0.2.

4. a = -0.9; b = 2.7; h = 0.3.

5. a = -2; b = 0.8; h = 0.2.

6. a = -1.9; b = 2.7; h = 0.3.

7. a = -0.4p; b = 0.4p; h = 0.5.

8. a = -0.3p; b = 1.3p; h = p / 10.

9. a = -p / 2; b = p / 2; h = p / 10.

10. a = -3; b = 3; h = 0.5.

1. Methods for constructing cyclic computing processes in programs.

2. The computer is enteredNreal numbers. Create a program that displays the arithmetic mean of this set.

Introduction

Loop programs are used in almost any software. In this case, loops can be explicit or implicit. In particular, the implicit loop is present in interrupt handlers, which actually work in an infinite loop, whose body is initiated by an interrupt. Subroutines, the window functions of Windows applications, are also circular. Further, we consider programs with a loop, the body of which contains functional modules.

Cyclic process is a computational process in which calculations are performed repeatedly using the same formulas for different values ​​of the argument.

Programs that implement a cyclic process are called cyclic programs.

In organizing the cycle, the following stages can be distinguished:

preparation (initialization) of the cycle (I);

execution of cycle calculations (cycle body) (T);

modification of parameters (M);

checking the condition of the end of the cycle (Y).

The order in which these steps are performed, such as T and M, may vary. Depending on the location of the check, the end conditions of the cycle distinguish between cycles with lower and upper ends. For a loop with a bottom end, the body of the loop is executed at least once, since first calculations are performed, and then the condition for exiting the loop is checked.


In the case of a loop with an upper end, the body of the loop may not be executed even once if the exit condition is immediately met.

A loop is called deterministic if the number of repetitions of the loop body is known or determined in advance. A loop is called iterative if the number of repetitions of the loop body is not known in advance, but depends on the values ​​of the parameters (some variables) involved in the calculations.

Loop body is a repetitive section of the program.

Loop parameter is a variable that takes on new values ​​each time the loop is repeated (loops can be simple or complex).

General view of the cycle n times

In general, the cycle is written n times like this:

nts number of repetitions times

The service word nts (beginning of the cycle) and kts (end of the cycle) are written strictly one below the other and are connected by a vertical line. To the right of this line, the repeated sequence of commands (the body of the loop) is written.

The number of repetitions is an arbitrary integer.

When executing the algorithm, the sequence of commands in the body of the loop is repeated the specified number of times. The algorithmic language rules allow any integer number of repetitions to be specified. It can be zero or even negative. These cases are not considered erroneous, just the body of the loop will not be executed even once, and the computer will immediately proceed to execute the commands written after kts

General view of the cycle so far

In general, the cycle is still written like this:

nc so far condition

| loop body (sequence of commands)

During the cycle, the computer repeats the following steps:

a) checks the condition written after the service word for now;

b) if the condition is not met, then the execution of the cycle is completed and the computer starts to execute the commands written after kts. If the condition is met, then the computer executes the body of the loop, checks the condition again, and so on.

General view of the cycle for

nts for i from i1 to i2

| loop body (sequence of commands)

Here i is the name of an integer value, i1, i2 are arbitrary integers or expressions with integer values. The body of the loop is sequentially executed for i = i1, i = i1 + 1, i1 + 2,… i = i2.

The algorithmic language rules allow specifying any integers i1, i2. in particular, i2 may be less than i1. this case is not considered erroneous - just the body of the loop will not be executed even once, and the computer will immediately proceed to execute the commands written after kts.

Loop n times and loop bye

Loops n times and while they are formatted in the algorithmic language are almost the same. This is not surprising, since both of these commands create a loop - a repeating sequence of commands. The service words nts and kts indicate that the loop is being executed, and the loop header specifies a specific mechanism for its execution.

However, these two cycles have one significant difference. Starting to execute the loop n times, the computer knows how many times it will have to repeat the body of the loop. While executing a loop, this is not the case yet: the computer checks the loop condition each time and cannot determine in advance when the execution will end. It is possible to find out the number of repetitions of the cycle only after the cycle is completed.

From this it is clear in which cases which cycle should be used. If the number of repetitions is known by the time the cycle begins, it is convenient to use the cycle n times. If the number of repetitions cannot be determined in advance, a so far cycle is needed.

For example, an automatic control program has the structure shown in Fig. one. Loop modules(as well as interrupt processing modules), each with one input and one output, usually have a characteristic feature: the modules contain static variables that are assigned a value in the current cycle, and the analysis of these variables is performed in the next cycle. Thus, the mentioned variables characterize the state of the module at the end of the current or the beginning of the next program cycle. In what follows, we will consider only such modules of cyclic programs and denote them briefly as MCP.


Fig. 1. Typical structure of a control program with an infinite loop.

MCPs have a varied structure, the complexity of which must be assessed according to special criteria. V.V. Lipaev proposed a convenient and objective criterion for the complexity of software modules, namely: the number and total length of paths in the control graph of the module. In this case, only conditional and selection statements are taken into account. However, this criterion is clearly not enough for an MPC with static memory, because when analyzing an MPC it is necessary to remember the values ​​of all static variables set in the previous cycle. In addition, there are no recommendations for the standardization of algorithms and programs, except for the long-known structured programming in common programming languages ​​such as C and Pascal. This article proposes filling these gaps in relation to MCP.

2. Fragments of modules of cyclic programs

A bipolar fragment, or just a fragment, will be considered a program section with one input and one output (including loop operators) on the assumption that the MCPs under consideration are structured. The simplest snippet includes a single statement. A sequence of fragments is also a fragment. MCP, in turn, is a fragment and consists of a sequence of fragments.

In the proposed method of independent fragments for the synthesis of the structure of modules that implement decision tables. In this case, such a fragment is considered as independent if it can be inserted anywhere in the sequence of fragments of the module. The independence of the location of such a fragment is due to the fact that the data analyzed in it is not formed in the specified sequence of fragments, and the data generated in an independent fragment is not analyzed in this sequence of fragments. Therefore, independent fragments can be executed in parallel (pseudo-parallel). In fig. 2 shows possible implementations of a module with two independent fragments. In variants "a" and "b" the fragments are rearranged without distorting the essence of the program; in the “c” variant, the fragments are implemented in parallel.


Fig. 2. Implementation options for a module with independent fragments:

a) and b) - sequential implementation,

c) - parallel implementation: a double horizontal line denotes parallelization of the program, a bold horizontal line denotes the termination of parallel processes.

A dependent fragment is one whose location depends on the location of another (s) fragment in the module. We will distinguish between the top and bottom dependent fragments. Above-dependent fragment must always be located below some fragment in which the variables used in this (dependent) fragment are formed. The bottom-dependent fragment should always be placed above the fragment, which uses the variables formed in this fragment. Two dependent fragments, one of which is from above dependent on the second, and the second from below dependent on the first, will be called mutually dependent fragments. They cannot be swapped and cannot be implemented in parallel. In fig. 3 shows an example of a module with mutually dependent fragments. Between the mutually dependent fragments there may be others, dependent or not dependent on them. Fig. 3. A module with dependent fragments.

Fixed is a dependent fragment, the location of which in the module is strictly defined. For example, in the module for recognizing a character entered from the keyboard, the first from the bottom must be the dependent fragment of the character input itself. The operators “start” and “end” of the module are fixed fragments.

Absolutely independent fragments do not exist, if only because in any module there are the mentioned fixed fragments of the beginning and end. Therefore, an independent fragment, in the general case, has an area of ​​possible location limited by two mutually dependent fragments. That is, a more rigorous definition of an independent fragment is as follows: independent with respect to two fixed fragments is a fragment that can be placed anywhere in a sequence of fragments bounded above and below by the specified fixed fragments.

"Programming of cyclic computational processes"

Objective: mastering the methods of compiling algorithms for cyclic computing processes and organizing cyclic programs of complex structure.

Theoretical part

4.1.1. Cyclic algorithms.

A cycle is a sequence of actions that can be performed more than once.

A looping algorithm is an algorithm that contains one or more loops.

There are 3 types of cycles:

Loop with precondition;

Loop with postcondition;

Counter cycle (counting cycle).

If the execution of the loop is associated with any logical condition, then loops with a precondition or with a postcondition are used.

Counter loops represent a class in which the execution of the loop body must be repeated a predetermined number of times.

The loop diagrams look like this:

1. Cycle with counter.

2. Loop with precondition. 3. Loop with postcondition.

4.1.2 Operators of the loop in the C ++ programming language.

In C ++, there is a corresponding operator for each type of loop:

While loop (with precondition);

Loop like do ... while (with postcondition);

For loop (counting).

1.The loop operator of the while type

Record form:

while (condition) statement;

where: (condition) - logical expression;

operator - a statement executed in a loop or the body of a loop.

If the body of the loop is a compound statement, then it must be enclosed in operator brackets (...):

while (condition)

group of operators

How such a loop works: while the condition is true, the body of the loop is executed and the condition is checked again, etc. When the condition becomes false (false), the loop exits.

2. A loop operator like do ... while

Record form:

operator;

while (condition);

The scheme of work of such a cycle: first the operator is executed, then the condition is checked, if the condition is true, the operator is executed and the condition is checked again, etc. When the condition becomes false, the loop ends.

If the body of the loop is a compound statement, then, as for a loop with a precondition, it must be enclosed in operator brackets (...):



group of operators

while (condition);

3. Operator of a loop of the for type

Record form:

operator;

A is an initial expression that sets initial values ​​for the loop parameter and, if necessary, initial values ​​for other parameters. For instance:

i = 0, x = 0.5, p = 1, s = 0

B is a conditional expression that checks the condition for continuing the loop. For instance:

C is an increment expression that sets an increment to a loop parameter and, if necessary, to other parameters, then they are written as a list. For example: x + = 0.1, i ++

4.1.3 An example of compiling an algorithm and a program in C ++ for a cyclic computational process.

Calculate the value of the expression:

b- the initial value, its value is entered from the keyboard and does not change;

a- changes in the range with a step of 1;

y- the result, its values ​​are displayed on the screen.

Based on the setting condition, the variable a is an integer, so it can be used as a counter in a counting cycle.

The block diagram of the algorithm for solving this problem using a counting cycle is as follows:

#include

#include

#include

printf (“Enter b:“);

scanf (“% f”, & b);

printf (“a y \ n”);

for (a = 0; a<=10;a++)

printf (“% 3d”, a);

printf (“% 8.2f \ n”, y);

y = (a-b) / sqrt (a);

printf (“% 8.2f \ n”, y);

The block diagram of the algorithm for solving this problem using a loop with a precondition is as follows:

The text of the C ++ program corresponding to this algorithm looks like this:

#include

#include

#include

printf (“Enter b:“);

scanf (“% f”, & b);

printf (“a y \ n”);

printf (“% 3d”, a);

printf (“% 8.2f \ n”, y);

y = (a-b) / sqrt (a);

printf (“% 8.2f \ n”, y);

else printf (“y doesn't exist \ n”);

The block diagram of the algorithm for solving this problem using a loop with a postcondition is as follows:

The text of the C ++ program corresponding to this algorithm looks like this:

#include

#include

#include

printf (“Enter b:“);

scanf (“% f”, & b);

printf (“a y \ n”);

printf (“% 3d”, a);

printf (“% 8.2f \ n”, y);

y = (a-b) / sqrt (a);

printf (“% 8.2f \ n”, y);

else printf (“y doesn't exist \ n”);

while (a<=10);

Practical part

4.2.1 Requirements for performance of work:

Complete the task from laboratory work No. 3 for the range of values ​​of one of the variables. The variable to be changed, the range of its change and the step are indicated in Table 4. Draw up block diagrams of algorithms and programs for the two types of cycles indicated in the individual task (Table 4).

Format the output of the results in such a way that the values ​​of the parameter being changed are clearly highlighted and for each of its specific values ​​the values ​​of the result (three variables from column 2 of Table 3) are displayed in the form of a table.

The order of the work.

1. Analyze the task, formulate the statement of the task.

2. Draw up block diagrams of algorithms.

3. Write a program in C ++. Provide input of initial data from the keyboard and display of results on the screen.

4. Check the functionality of the program on various initial data.

5. Analyze the results.

Options for individual assignments.

Options for individual assignments are selected from Table 4 in accordance with the student's number in the group list in the teacher's journal.

Table 4. Options for individual tasks

P / p No. Mutable variable Loop types
10 ≤ a ≤ 10,Δ a = 1
-4 ≤ d ≤ 4, Δ d = 0.5
-6 ≤ x ≤ 3, Δ x = 0.5
0 ≤ b ≤ 3 0, Δ b = 1.5 1.With precondition, 2.Countable
-15 ≤ j ≤ 1 0, Δ j = 0.5 1.With a precondition, 2.With a postcondition
5 ≤ e ≤ 35,Δ e = 2 1.Countable, 2.With postcondition
-5 ≤ m ≤ 15,Δ m = 1 1.With precondition, 2.Countable
1 ≤ c ≤ 70,Δ c = 3 1.With a precondition, 2.With a postcondition
1.5 ≤ c ≤ 15,Δ c = 0.5 1.Countable, 2.With postcondition
-8 ≤ b ≤ 28,Δ b = 2 1.With precondition, 2.Countable
-4.5 ≤ x ≤ 11.5,Δ x = 0.5 1.With a precondition, 2.With a postcondition
-7 ≤ k ≤ 2,Δ k = 0.3 1.Countable, 2.With postcondition
-1 ≤ m ≤ 21,Δ m = 1 1.With precondition, 2.Countable
-2 ≤ e ≤ 34,Δ e = 2 1.With a precondition, 2.With a postcondition
-11 ≤ c ≤ 23,Δ c = 2 1.Countable, 2.With postcondition
-13 ≤ p ≤ 50,Δ p = 3 1.With precondition, 2.Countable
3.3 ≤ b ≤ 9.3,Δ b = 0.3 1.With a precondition, 2.With a postcondition
3.5 ≤ y ≤ 12.3,Δ y = 0.4 1.Countable, 2.With postcondition
-7.5 ≤ a ≤ 5.7,Δ a = 0.6 1.With precondition, 2.Countable
-1.5 ≤ h ≤ 1.2,Δ h = 0.1 1.With a precondition, 2.With a postcondition
0 ≤ h ≤ 10,Δ h = 0.5 1.Countable, 2.With postcondition
-15 ≤ b ≤ 15, Δ b = 2 1.With precondition, 2.Countable
-7 ≤ l ≤ 3, Δ l = 0.5 1.With a precondition, 2.With a postcondition
-5.5 ≤ b ≤ 6.5, Δ b = 0.5 1.Countable, 2.With postcondition
1 ≤ k ≤ 9, Δ k = 0.4 1.With precondition, 2.Countable
0 ≤ b ≤ 6.9,Δ b = 0.3 1.With a precondition, 2.With a postcondition
-3 ≤ v ≤ 9,Δ v = 0.6 1.Countable, 2.With postcondition
-2 ≤ p ≤ 2.6,Δ p = 0.2 1.With precondition, 2.Countable

4.3 Test questions and practical tasks:

1. How does the while statement work?

2. How does the do ... while statement work?

3. How does the for statement work?

4. Underline the statements in the program that form the cycle.

5. What is the difference between the while and do ... while statements?

6. Replace one cycle operator with another in the program.



Did you like the article? Share it