Turbo Pascal, though not the world's favoritean application for programming, but the creators who take the first steps in writing software begin their acquaintance with this particular environment. It gives an idea of ramifications, operators, functions and procedures, as well as many other things. For example, when studying a programmer will encounter cycles in Turbo Pascal: While, For and Repeat.
The concept of the cycle and its varieties
The cycle is called repeated actions. In this environment are used:
- with the parameter (For ... to ... do);
- with a precondition (While ... do);
- with postcondition (Repeat ... until).
The first kind is used when it is known in advancehow many steps in solving the problem. However, there are a number of tasks, when there is no information about how many times certain actions will be repeated. In this case, in Pascal While the loop becomes indispensable, as, in principle, Repeat.
Cycle structure
What is the essence of work in Pascal While, Forand repeat cycles? These structures allocate the title and body. The first component specifies the variables that will “work”, set the conditions for verifying the truth, the period until which the body will be executed. In the second component, expressions are written that should be used if the condition is met, that is, True, not False.
Когда итерация выполняется на последней строке code, then it returns to the header where the condition is checked. In the case of truth, the operations are repeated, and in the case of non-fulfillment of the condition, the program “exits” from the cycle and performs further operations.
The While loop looks like this. Pascal ABC and similar programs require writing such code:
- While Condition do;
- Begin;
- Cycle body;
- End.
If in the body of the loop 1 statement (1 action) is executed, then the “brackets” begin ... end can be omitted.
Cycle flowchart
Turbo Pascal While has the following features:
- inside the structure, difficult conditions can be used;
- there should be no semicolon after the word do (this is considered an error in Turbo Pascal and Pascal ABC);
- a variable, constant, or expression that, when received, returns a False response by the output of their subroutine, must be of a boolean type, i.e. Boolean.
The following is the block diagram of this type of cycle. It shows the sequence of actions.
Cycle Algorithm
In the simplest programming environments, including Pascal ABC, While the cycle operates according to the following principle:
- given iterations, i.e., repetitions, will be performed as many times as the condition is true (True);
- as soon as the condition is not satisfied and the answer is False (or otherwise “False”), the operator leaves the loop;
- as soon as this happened, the program “went” to the structures that were standing after the cycle.
This is a significant difference between While and Repeat, that is, a cycle with a precondition and a postcondition.
It is very important to include in the loop body the finalchange the specified variable in the While header. In any case, a situation giving False value should come sometime. Otherwise there will be a looping, and then you have to use additional measures to exit the compiler. Such errors are considered gross and unforgivable.
How to exit the program during a looping?
Often a situation occurs when the operatorWhile Pascal loops in written program code. What does this mean? The iteration repeats an infinite number of times, since the condition is always true. For example, here is a fragment of the program:
- While 2> 1 do;
- Write (1).
In this case, to interrupt the execution of the task, it is enough to press CTRL + F2.
There are 2 more ways to control this.program behavior. For example, if you enter into the code Continue, which will transfer control to the beginning of the cyclic construction (here the condition for exiting the loop is controlled, i.e. the execution of the current iteration will be interrupted). Then control is passed in a While loop to the previous check.
Break operator is able to interrupt the execution of allloop and pass control to the next iteration. Here the exit from the structure will not be monitored. The image shows examples of the use of these operators.
Problem solving
Consider the While loop in action.Pascal task offers to solve the most diverse. Let us dwell on the simplest in order to understand the principle of operation. Solved tasks in the program Pascal ABC. But the images of the classic Turbo Pascal environment will be presented for comparison.
Exercise 1: given function Y = 5-X ^ 2/2. Create a table of values with sh = 0.5 in the interval [-5; 5].
Algorithm of actions:
- set variable X to an initial value of -5 (i.e. the beginning of the gap);
- calculate the value of Y until the variable x reaches the end of the specified segment;
- display the function and abscissa values (X);
- increase X by the given step.
This is what the code in Pascal ABC looks like.
How does the code in the program Turbo Pascal. The image below clearly shows this.
Task 2: An array A is given, consisting of positive integers andnegative numbers. It contains 10 elements. It is necessary to form a matrix B in which the positive elements of array A, having an even index, will be displayed. Display the sum of squares in the number from the new matrix.
Algorithm of actions:
- It is necessary to write a subroutine that will “work” only with elements of array A, which have an even index. In the loop, the value of the variable responsible for the parity of the index will increase by 2.
- If the number with an even index from the matrix Acorresponds to the condition x> 0, then the count of the array elements is increased by 1. The current value of the counter variable will be the index of the number to be copied in the array B.
- Initially, the variable summa responsible for finding the sum of squares of positive numbers is assigned to 0. Then the operation will be performed: the new value of the square is added to the previous sum.
- Do not be afraid if not all positivethe numbers are transferred from one matrix to another. Need to be attentive. Many novice programmers in a panic rewrite the code. The condition should be carefully studied: positive numbers that are in even “places”, i.e., have indices that are multiples of 2.
Manual tracing is necessary to ensure the accuracy of the calculations. Sometimes using this method, you can identify errors that are not caught the eye during a routine check of written code.
If you do manual calculations, you can make surethat the program works correctly. This, in turn, suggests that the code generation algorithm is correct, the sequence of actions leads to a logical end.