/ Programming. Cycles with a parameter

Programming. Cycles with a parameter

A special place in Turbo Pascal is occupied by cycles.They begin to learn immediately after the training of I / O skills on the screen. After all, most tasks are reduced to the fact that loops with a parameter and other constructs help to facilitate writing and functioning of a certain block of the program.

cycles with parameter

Types of cycles

In total, there are three varieties:

  • with parameter,
  • with a precondition,
  • with a postcondition.

Cycles with a parameter, otherwise they are called For ... to ...do or For ... downto .... do, repeatedly repeat a certain sequence of actions. In principle, other varieties are used for the same purpose, only in the for-cycle is the number of steps known in advance.

In the other two constructs (While and Repeat), the number of iterations is initially unknown. Therefore, when studying the task, it is already necessary to understand which cycle will be used.

Basic definitions on the topic

Cycles with parameters - repeatedly recurringiteration. Counter - the main indicator by which the given design is performed. The boundaries of the interval show the extent to which certain iterations will be performed. By the way, it's not necessary that the initial value is equal to 1. The user sets both boundaries of the gap independently. The body of the loop is a set of instructions for which the number of repetitions is already defined.

Понятие «циклы с параметрами» означает, что в This condition is checked, then a set of iterations is performed. The counter increases (or decreases), and everything repeats. The loop body will be used until the condition is true.

For ... to ... do: algorithm, syntax

As already mentioned, loops with a parameter are used in tasks that indicate the "gap" in which you want to work. So, it can be an array of numbers, days of the week, lines of a poem, etc.

There are 2 types of construction: to increase the meter and to reduce it. The first construction will be written as follows:

for outgoing : = boundary 1 then boundary 2 before

begin

body of the cycle;

end;

Here: ref. variable is declared by the user at the beginning of the program or block; boundary 1 and boundary 2 - the initial and final value of the gap; at body cycle a number of actions arerun by the program. It is necessary to remember that if the body of the loop contains only 1 command, then the operator brackets begin ... end can be omitted. In this design variant, the counter, namely , will increase in steps of 1.

for outgoing : = boundary 1 downto boundary 2 before

begin

body of the cycle;

end;

Here it is ref. the variable will decrease in steps of 1.

The scheme of the cycle with the parameter For ... to ... do will look like this:

  • The value of the upper boundary of the interval is set, that is, boundary 2.
  • Outgoing variable the value of the parameter is assigned boundary 1.
  • A condition check takes place: reference variable ≤ boundary 2.
  • When the result is received True (True) the body of the loop is executed.
  • The counter is incremented by a step equal to 1.
  • Execution of points 3-5 occurs exactly until the moment the condition is true: outgoing> border 2. As soon as this happens, the cycle is exited and control is transferred to the command following the given construction.

In the For ... downto ... do algorithm works similarly with the above, except for some items:

  • In the third paragraph, the condition is checked: reference variable ≥ boundary 2.
  • In the 5th line of the algorithm the counter is reduced by 1.
  • In the 6th point of the 3-5 command will be executed until the condition is satisfied: outgoing

cycles with parameters

All the rest is similar in both algorithms of work.

Block diagram of a cycle with a parameter

Parameter loops have the following form of a flowchart (although it has already been presented above). It also shows the simplified organization of the structure.

loops with parameters means

Basic requirements for a loop with a parameter

Parameter loops require a certain kind of condition.

  • The counter and the boundaries of the gap (i.e. ref.variable, boundary 1 and boundary 2) must belong to the same data type. If there is only compatibility between the initial and final values ​​of the segment and the original variable, then the program may behave incorrectly, because the boundaries will be converted according to the data type of the original parameter.
  • The data type to which the parameter values ​​must belong must be an integer. It is not recommended to use the real type.
  • Changing the value of the source variable parameter in the loop body is forcibly undesirable. Otherwise, the user can hardly trace the possible errors that have appeared.
  • Unlike other types of cycles, in For ... to ... do or For ... downto ... do the step can not change on a parameter other than 1.

Turbo Pascal: how to exit the loop

Often there are tasks in which occurslooping, i.e. the condition being tested is always true. The Break procedure helps to get out of cycles with a precondition, postcondition, parameter. That is, their work is terminated early.

Loops with a parameter in pascal (programmingwhich implies the “eternal” truth of the condition) can be stopped with the help of Continue. Here, the work is organized as follows: the current iteration ends its execution ahead of time, control is transferred to the next command, but without exiting the loop.

The exit procedure is necessary in order tocomplete the work of a block in the program code. It is called inside the procedure (function) and at the same moment, the execution of this “piece” immediately stops. If Exit is in the main block of the program, then it finishes its work.

The Halt procedure reduces the principle of operation to the following: the work of the program ends completely.

Examples of tasks with the decision

The user will be useful after studying the topic."Loops with a parameter in pascal" examples are first studied and then trained to write code independently. Simple tasks help the future programmer to learn the theory in practice, and then successfully apply it. On the topic “Cycles with a Parameter”, examples of problems with a solution can be found easy and complex. Here are 3 tasks in which the work algorithms are analyzed and explanations and comments are made to each solution.

Task 1

Given a two-dimensional array of natural numbers in the range [0..199], selected randomly. Find the number of all two-digit numbers whose sum of digits is a multiple of 2.

Algorithm of actions:

  1. Create a two-dimensional array.
  2. Check each number for compliance with the conditions:

a) if 9

b) select the second digit of the number by dividing through mod;

c) add the highlighted numbers;

d) divide by mod the given amount by 2;

e) if the result is 0, then the counter is incremented by 1.

parameter loops

Task 2

Given a one-dimensional array of integer elements. Find the number of positive numbers.

Algorithm of actions:

  1. Create an array of integer elements created by randomize.
  2. In a loop with a parameter, attach a conditional IF statement that will check the specified element for compliance with the condition: X> 0.
  3. If the condition is met, the counter is incremented by 1.
  4. After the cycle, you should display the resulting counter value.

The data indicated in brackets {} arecomments. In line 11, you can display an array on the screen in two ways: leave a space between the numbers or allocate a certain number of cells for each element (in this case there are 5).

In line 12, the counter variable can also be increased in two ways: either add 1 to the previous value or use the inc standard function.

cycles with parameter in pascal examples

Task 3

Given a square matrix. Find the number of positive elements on the main diagonal.

Explanations:

In the array of numbers, the main diagonal is drawn fromupper left corner to lower right. Its peculiarity is the fact that the row and column indices coincide. Therefore, it is enough to organize 1 cycle to go through the rows without searching the other elements.

Algorithm of actions:

  1. Create a square matrix.
  2. Assign the variable responsible for counting positive elements to the value "0".
  3. Create a cycle to create a square matrix.
  4. Organize verification cycle conditions: if the number is on the main diagonal> 0, then the counter is incremented by 1.
  5. After the end of the cycle, display the value of the variable that stores the number of positive elements on the screen.

cycles with parameter in pascal programming

The confrontation of two programming languages: C and Turbo Pascal

As a rule, a self-respecting programmer knowsseveral languages. For example, it could be C ++, Turbo Pascal, Delphi, Java, etc. The confrontation of two of them was clearly expressed in the 80s. (C and Turbo Pascal). At the end of the twentieth century, the same struggle was observed between C ++ and Java.

cycles with a parameter in c

In the virtual space among the three dozenThere are three brightest pairs of programming languages, the confrontation of which struck the greatest minds of cyberspace: Algol-60 and Fortran, Pascal and C, Java and C ++. Of course, these feelings are subjective, but at one time or another, one of the couple was the leader. This was due to industry requirements and the need for a particular software product. In the 70s. "Ruled the world" FORTRAN, in the 80s - Turbo Pascal, in the 90s - C ++. Of course, none of them "died." Rather, they were transformed into improved software products.

When learning programming languages, you can see that the syntax is similar in some topics. So, cycles with a parameter in C are similar to similar constructions in Pascal, with the exception of some moments.

Interestingly, the developers of Turbo Pascal (OldLight) used the results of developments of American scientists, while in the New World they actively used the results of research by European specialists. In Europe, developers advocate a greater degree of purity and compactness of programming languages, while American minds tend to use newfangled trends in writing code.