Turbo Pascal, although not the world's favorite application for programming, but creators taking their first steps in writing software, begin to get acquainted with this particular environment. It gives you an idea of ramifications, operators, functions and procedures, and many other things. For example, when studying, the programmer will encounter cycles in Turbo Pascal: While, For and Repeat.
The concept of a cycle and its varieties
Cycle is a process that repeats many times. This environment uses:
- with parameter (For … to… do);
- with precondition (While … do);
- with postcondition (Repeat … until).
The first type is used when it is known in advance how many steps in solving the problem. However, there are a number of tasks where 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.
What is the essence of work inPascal While, For and Repeat loops? Such constructions have a heading and a body. In the first component, the variables that will “work” are indicated, the conditions for checking the truth are set, the period until which the body will be executed. The second component contains expressions that should be used if the condition is met, i.e. True, not False.
When the iteration is performed on the last line of code, then it returns to the header where the condition is checked. If true, the operations are repeated, and if the condition is not met, the program “leaves” the loop and performs further operations.
The While loop looks like this. Pascal ABC and similar programs require you to write code like this:
- While Condition do;
- Cycle body;
If 1 operator (1 action) will be executed in the loop body, then the “brackets” begin… end can be omitted.
Turbo Pascal While has the following features:
- complex conditions can be used inside the construction;
- there should not be a semicolon after the word do (this is considered an error in Turbo Pascal and Pascal ABC);
- A variable, constant or expression that, when False is answered, is the output of their subroutine must be of a boolean type, i.e. Boolean.
The block diagram of this kind of cycle looks like this. It shows the sequence of actions.
Algorithm of the cycle
In the simplest programming environments, including Pascal ABC, the While loop works as follows:
- given iterations, i.e. repetitions, will be repeated as many times as long as the condition is true (True);
- as soon as the condition is not met and gives the answer False (or otherwise "False"), the operator exits the loop;
- as soon as this happened, the program "went" to the constructs after the loop.
This is a significant difference between While and Repeat, i.e. a loop with a precondition and a postcondition.
It is very important to provide in the body of the loop the final change of the given variable in the While header. In any case, there must sometime be a situation that evaluates to False. Otherwise, a loop will occur, and then you will have to use additional measures to exit the compiler. Such mistakes are considered gross and unforgivable.
How to exit the program while looping?
It often happens that the While Pascal statement produces a loop in the written program code. What does this mean? The iteration is repeated an infinite number of times since the condition is always true. For example, here is a program fragment:
- While 2>1 do;
In this case, to interrupt the task, just press CTRL + F2.
There are 2 more ways to control this behavior of the program. For example, if you enter Continue in the code, which will transfer control to the beginning of the cyclic construction (here the exit condition from the loop is controlled, that is, the execution of the current iterationwill be interrupted). Then control is transferred in the While loop to the previous test.
The Break statement is able to interrupt the execution of the entire loop and transfer control to the next iteration. Here, the exit from the structure will not be controlled. The image shows examples of using these operators.
Let's look at the While loop in action. Pascal offers a variety of tasks to solve. Let's dwell on the simplest ones for now to understand the principle of operation. Problems solved in Pascal ABC. But images of the classic Turbo Pascal environment will also be provided for comparison.
Task 1: given the function Y=5-X^2/2. Make a table of values with step sh=0, 5 on the interval [-5;5].
Algorithm of actions:
- set the initial value for the variable X to -5 (i.e. the beginning of the interval);
- calculate the value of Y until the variable x reaches the end of the specified segment;
- display function values and abscissa (X);
- increase X by a given step.
This is what the code looks like in the Pascal ABC program.
What does the code look like in the Turbo Pascal program. The image below shows this clearly.
Task 2: array A is given, consisting of positive and negative integers. It contains 10 elements. It is necessary to form a matrix B, which will display the positive elements of the array A, which have an even index. Display amount on screensquares 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 that have an even index. In the loop, the value of the variable responsible for the index parity will increase by 2.
- If the number with an even index from matrix A meets the condition x>0, then the array element counter is incremented by 1. The current value of the counter variable will be the index of the copied number in array B.
- Initially, the summa variable responsible for finding the sum of the squares of positive numbers is assigned 0. Then the operation will be performed: the new value of the square is added to the previous sum.
- Don't be afraid if not all positive numbers have passed from one matrix to another. You need to be careful. Many novice programmers rewrite code in a panic. You should carefully study the condition: positive numbers that are in even "places", that is, having indices that are multiples of 2.
Manual tracing is necessary to make sure the calculations are correct. Sometimes using this method you can find errors that are not caught by the eye during normal checking of the written code.
If you carry out manual calculations, you can make sure that the program works correctly. This, in turn, indicates that the code creation algorithm is correct, the sequence of actions leads to a logical end.