The for: Pascal Starter Cycle

The basis of programming is loops, for example, for. Pascal. Like any computer language, it also contains such constructs in its syntax.

Assignment of cyclic operators

A typical task is to make the computer perform the same action several times. This is where the constructions that save the repetition of the code are saved. So, a cycle in programming is a repeatedly executed action, organized by means of language.

The Pascal programming language includes several such constructs:

  • for - repeat with parameter;
  • while - repeat with precondition;
  • repeat ... until - repeat with a postcondition.

for pascal

Despite the apparent simplicity and obviousness of the cyclic constructions, novice programmers face a number of problems in their implementation. The most difficult is setting the conditions for exiting the cycle. In this regard, the count operator for is the most straightforward.

Structure

To write a repeat construct with a parameter, you need to type the following code:

FOR {a}: = {b} TO {c} DO {d}.

In the example, the reserved words of the language are written in capital letters for convenience; in practice, you can use the lower case. The variables and operators used are shown in braces. They mean:

  • { a} - a variable of any countable type, most often INTEGER;
  • { b}, { c} - expressions of countable type, the same as { a} ;
  • { d} - an arbitrary operator / language operators, also called the body of the loop.

After completing the for construct, Pascal calculates the value { b} , assigns { a}: = { c} , and then the action is repeated:

  • checking condition { b} <= { c} , under which the loop stops its work;
  • start of the operator {d} ;
  • increasing the value of {a} by one, that is, { a}: = { a} + 1 .

pascal for dummies

Since the number of repetitions inside the for body is known, this construction is referred to as deterministic cycles.

Example

To understand how for will be executed, the following is an example of Pascal code for dummies.

  • s: = 0;
  • for a: = 1 to 5 do
  • begin
  • s: = s + a;
  • end;
  • writeln (s);

Understanding the written instructions, you can write the values ​​in each iteration:

  • 1st iteration: a = 1; s = 1 ;
  • 2nd iteration: a = 2; s = 3 ;
  • 3rd iteration: a = 3; s = 6 ;
  • 4th iteration: a = 4; s = 10 ;
  • 5th iteration: a = 5; s = 15 .

As a result, the user will see the number β€œ15” on the screen - the sum of the numbers from 1 to 5.

To make the first program more universal, replace the numbers 1 and 5 with variables.

Common mistakes

When using the for loop, Pascal requires careful attention to the values ​​of { a}, { b}, { c} . If you do not follow the rules for writing programs, the compiler will report an error. There are five such rules.

  1. The counter (loop parameter) { a} is given as an integer.
  2. The values { b} and { c} must also be of integer type. For example, you need to calculate the values ​​of the angle given in radians from 0 to p. But the following code entry will be incorrect for a: = 0 to pi do. The solution is to use the trunc () or round () rounding functions. The first discards the fractional part of a real number, the second rounds it to the nearest integer.
  3. If the user made a mistake and indicated {b} < {c} , the operator {d} will never work.
  4. After the loop is completed, counter { a} can have any value. Although this is contrary to logic, in practice { a} <> { c} . Conclusion: it is not recommended to refer to { a} after the for construct is completed.
  5. In the body of the loop (that is, after the word do ) it is not recommended to use any operators that change the values ​​of { a} . Following this rule will not cause an error, but the program logic will be violated.

The following rule is generally accepted and corresponds to the β€œgood form” in programming: in order to make it convenient to work with the code, it is necessary to write the loop body operators not from the first column. For example, making 2-3 spaces to the left or using the Tab key.

for pascal loop

This requirement applies not only to Pascal. For "dummies" it allows you to quickly find and fix a mistake in the instructions.

Typical tasks

Suppose you want to tabulate a function of the form f ( x) = 3 * x + 15 , that is, get a table of M function values ​​in the range [ x 1 ; x 2 ], where x 1 and x 2 are the minimum and maximum values ​​of the argument. The for construct helps solve this and similar problems. Pascal recommends writing code in the following way:

  • for a: = 1 to M do
  • begin
  • x: = x1 + (x2 – x1) * (a – 1) / (M – 1);
  • f: = 3 * x + 15;
  • writeln (x, '', f);
  • end.

Since the step of changing x is not specified, the value of the argument is calculated during the program at each iteration using the formula: x: = x1 + ( x2– x1) * ( a – 1) / ( M – 1).

Cycle in cycle

Due to the fact that any operators are allowed to be used inside the structure, it is allowed to place another for loop in its body. Pascal has a standard description for this task, similar to other programming languages:

  • FOR {a}: = {b} TO {c} DO
  • FOR {a1}: = {b1} TO {c1} DO

pascal programming language

For the design to work correctly, it is necessary to observe the condition: the counters in each cycle must be different. Otherwise, the inner loop will change the value of the outer loop parameter, which will lead to logical errors.


All Articles