ComputersProgramming

Programming. Cycles with a parameter

A special place in Turbo Pascal is occupied by cycles. They are beginning to be studied 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.

Types of cycles

In total, there are three varieties:

  • With a 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 iterations. 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 is 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.

The concept of "cycles with parameters" means that the condition is checked in this construction, after which 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 of operation, syntax

As already mentioned, cycles 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 design: to increase the counter and to reduce it. The first construction will be written as follows:

For outgoing : = boundary 1 to boundary 2 do

Begin

The body of the cycle ;

End;

Here: ref. The 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; In the body of the cycle, a number of actions are prescribed that must be performed 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 border 2 do

Begin

The 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 gap, i.e., boundary 2, is specified .
  • The value of parameter boundary 1 is assigned to the variable.
  • A condition check takes place : ref . Variable ≤ boundary 2 .
  • When the True result is obtained, the body of the loop is executed.
  • The counter is incremented by a step equal to 1.
  • The execution of points 3-5 occurs exactly until the condition is true: ref> boundary 2 . Once this happens, the loop 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 verified: the reference variable ≥ boundary 2 .
  • In the 5th line of the algorithm, the counter is decremented by 1.
  • In the 6th point, the commands 3-5 will be executed until the condition is satisfied: ref.

All the rest is similar in both algorithms of work.

Block diagram of a cycle with a parameter

Cycles with a parameter have the following form of flowchart (although it has already been presented above). Here is a simplified organization of the structure.

Basic requirements for a cycle with a parameter

Cycles with parameters require a certain kind of conditions.

  • The counter and the bounds of the gap (i.e., the reference 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 source variable, the program can behave incorrectly, because the boundaries will be transformed according to the type of data of the original parameter.
  • The data type to which parameter values should belong must be an integer. It is highly not recommended to use a real type.
  • To change the value of the parameter variable in the body of the loop is forcibly unwanted. Otherwise, the user will hardly be able to track possible errors that have occurred.
  • Unlike other types of cycles, in For ... to ... do or For ... downto ... do step can not be changed To a parameter other than 1.

Turbo Pascal: how to get out of the loop

Often there are problems in which the cycling occurs, that is, the test condition is always true. The Break procedure helps to exit loops with a precondition, a postcondition, a parameter. That is, their work ends prematurely.

Cycles with a parameter in Pascal (the programming of which assumes the "eternal" truth of the condition) can be stopped with the help of Continue. Here the work is arranged as follows: the current iteration completes its execution ahead of schedule, control is transferred to the next command, but without exiting the loop.

Exit procedure is necessary in order to complete the work of a block in the program code. It is called within procedures (functions) and at the same time, the execution of this "piece" immediately ceases. If Exit is in the main block of the program, then it terminates its work.

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

Examples of tasks with a solution

The user will be useful after studying the topic "Cycles with a parameter in Pascal" examples first to learn, and then practice to write the code yourself. Simple tasks help the future programmer learn the theory in practice, and then successfully apply it. On the topic "Cycles with a parameter" examples of tasks with a solution can be found easy and complex. Here are 3 tasks in which the algorithms of work are disassembled and explanations and comments for each solution are given.

Task 1

Given a two-dimensional array of natural numbers in the range [0..199], chosen 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 it through mod;

C) add the selected figures;

D) divide mod by a given amount by 2;

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

Task 2

A one-dimensional array of integer elements is given. Find the number of positive numbers.

Algorithm of actions:

  1. Create an array of integer elements created by randomize.
  2. In the cycle with the parameter, insert the conditional IF operator , which will check the given element for the condition: X> 0.
  3. If the condition is met, the counter increments by 1.
  4. After the cycle, you should display the resulting value of the counter.

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

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

Task 3

A square matrix is given. Find the number of positive elements on the main diagonal.

Explanations:

In the array of numbers, the main diagonal extends from the upper left corner to the lower right. Its peculiarity is the fact that the indices of the row and column coincide. Therefore, it is enough to organize 1 cycle to go through the rows without looking through the remaining elements.

Algorithm of actions:

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

Confrontation of two programming languages: C and Turbo Pascal

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

In the virtual space, among the three dozen programming languages, three brightest pairs can be identified, 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 pair was the leader. This was due to the requirements of the industry and the need for a particular software product. In the 70's. "Controlled the world" Fortran, in the 80's - Turbo Pascal, in the 90's - C ++. Of course, none of them "died." Rather, they have transformed into improved software products.

When studying programming languages, you can notice that in some topics the syntax is similar. Thus, cycles with a parameter in C are similar to similar constructions in Pascal, except for some moments.

It is interesting that the developers of Turbo Pascal (Old World) used the results of the developments of American scientists, while in the New World they actively used the results of European studies. In Europe, developers argue more for the purity and compactness of programming languages, and American minds tend more to the use of newfangled trends in writing code.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.