A special place in Turbo Pascal is occupied by cycles. They begin to study immediately after practicing the skills of input-output of information on the screen. After all, most tasks come down to the fact that loops with a parameter and other constructions help to facilitate the writing and functioning of a certain program block.
Varieties of cycles
In total there are three varieties:
- with parameter
- with the precondition
- with a postcondition.
Loops with a parameter, otherwise they are called For ... to ... do or For ... downto .... do repeatedly repeat a specific sequence of actions. In principle, other varieties are used for the same purpose, only in the for-loop the number of steps is known in advance.
In two other 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.
Key definitions on the topic
Loops with parameters - iterated over many times. Counter - the main indicator with which a given design is performed. The boundaries of the gap indicate the extent to which certain iterations will be performed. By the way, it is absolutely not necessary that the initial value be 1. The user independently sets both boundaries of the gap. The body of the loop is a set of instructions for which the number of repetitions has already been determined.
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 body of the loop will be invoked as long as the condition is true.
For ... to ... do: work algorithm, syntax
As already mentioned, loops with a parameter are used in tasks that specify a “gap” in which 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 design will be spelled out as follows:
for original variable : = border 1 to border 2 do
begin
cycle body ;
end;
Here: ref. the variable is declared by the user at the beginning of the program or block; border 1 and border 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 must be remembered that if the loop body contains only 1 command, then the begin ... end operator brackets can be omitted. In this design variant, the counter, namely <exc. Variable>, will increase in increments of 1.
for original variable : = border 1 downto border 2 do
begin
cycle body ;
end;
Here ref. the variable will decrease in increments of 1.
The scheme of the loop with the For ... to ... do parameter will look like this:
- The value of the upper boundary of the interval, i.e., boundary 2, is set .
- The original variable is assigned the value of the parameter boundary 1 .
- The condition is checked: initial variable ≤ boundary 2 .
- When the result is True , the loop body is executed.
- The counter is incremented by 1.
- The implementation of items 3-5 occurs exactly until the condition is true: original variable> boundary 2 . As soon as this happens, the loop exits and control is transferred to the command following this construction.
In For ... downto ... do, the algorithm is similar to the above, with the exception of some points:
- In the 3rd paragraph, the condition is checked: original variable ≥ boundary 2 .
- In the 5th line of the algorithm, the counter decreases by 1.
- In point 6, commands 3-5 will be executed until the condition is satisfied: the initial variable <boundary 2.
Everything else is similar in both work algorithms.
Block diagram of a cycle with parameter
Loops with a parameter have the following type of flowchart (although it has already been presented above). A simplified design organization is also shown here.
Basic requirements for a loop with a parameter
Loops with parameters require a certain kind of condition.
- The counter and span boundaries (i.e., the original variable, border 1 and border 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, since the boundaries will be converted according to the data type of the initial parameter.
- The data type to which the parameter values must belong must be integer. It is highly discouraged to use the material type.
- It is forcibly undesirable to change the value of the original variable in the loop body. Otherwise, the user can hardly track possible errors that appear.
- Unlike other kinds of loops, in For ... to ... do or For ... downto ... do the step cannot change to a parameter other than 1.
Turbo Pascal: how to get out of the loop
Often there are problems in which a loop occurs, that is, the condition being checked is always true. The Break procedure helps to get out of loops with a precondition, postcondition, parameter. That is, their work is terminated ahead of schedule.
Loops with a parameter in pascal (programming of which assumes the “eternal” truth of the condition) can be stopped using Continue. Here, the work is organized as follows: the current iteration prematurely ends its execution, control is transferred to the next command, but without exiting the loop.
The Exit procedure is necessary in order to complete the operation 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 ends its work.
The Halt procedure reduces the principle of operation to the following: the program ends completely.
Examples of tasks with a solution
After studying the topic “Cycles with a Parameter in Pascal,” it will be useful for the user to study the examples first, and then train to write the code yourself. Simple tasks help the future programmer to recognize 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 work algorithms are understood and explanations and comments for each solution are given.
Task 1
A two-dimensional array of natural numbers is given 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:
- Create a two-dimensional array.
- Check each number for compliance with the conditions:
a) if 9 <X <100, then divide it entirely by 10 by div;
b) highlight 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.
Task 2
A one-dimensional array of integer elements is given. Find the number of positive numbers.
Algorithm of actions:
- Create an array of integer elements created by randomize.
- In the loop with the parameter, embed the conditional IF statement, which will check the given element for compliance with the condition: X> 0.
- If the condition is met, the counter is incremented by 1.
- After the cycle, the resulting counter value should be displayed.
The data shown in brackets {} are comments. In line 11, you can display the array on the screen in two ways: leave a space between the numbers or leave a certain number of cells (in this case, 5) for each element.
In line 12, the counter variable can also be increased in two ways: either add 1 to the previous value, or use the standard Inc. function
Task 3
Given a square matrix. Find the number of positive elements located on the main diagonal.
Explanations:
In an array of numbers, the main diagonal extends from the upper left corner to the lower right. Its peculiarity is the fact that the row and column indices are the same. Therefore, it is enough to organize 1 cycle to go through the lines without iterating through the remaining elements.
Algorithm of actions:
- Create a square matrix.
- Set the variable responsible for counting positive elements to "0".
- Create a cycle to create a square matrix.
- Organize a cycle to check the conditions: if the number on the main diagonal is> 0, then the counter increases by 1.
- After the loop ends, display the value of the variable storing the number of positive elements.
The confrontation of two programming languages: C and Turbo Pascal
As a rule, a self-respecting programmer knows several languages. For example, it can be C ++, Turbo Pascal, Delphi, Java, etc. The confrontation of two of them was pronounced back in the 80s. (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, there are three brightest pairs, 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, not one of them "died." Rather, they have transformed into advanced software products.
When learning programming languages, you may notice that the syntax is similar in some topics. So, loops with a parameter in C are similar to similar constructions in Pascal, with the exception of some points.
Interestingly, the developers of Turbo Pascal (Old World) used the results of the work of American scientists, while in the New World they actively used the results of research by European experts. In Europe, developers advocate more for the purity and compactness of programming languages, and American minds are more inclined to use new-fangled trends in writing code.