Cycle for: Pascal for Beginners

Cycle for: Pascal for Beginners

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 performthe same action several times. Here, and save the design, allowing you to arrange a repetition of the code. So, a cycle in programming is a multiply executed action, organized with the help of language tools.

The Pascal programming language includes several such constructs:

  • for- Repeat with parameter;
  • while- Repeat with precondition;
  • Repeat with postcondition.

for pascal

Despite the seeming simplicity and obviousnesscyclic constructions, novice programmers are faced with a number of problems in their implementation. The most difficult is the formulation of exit conditions from the cycle. In this respect, the for statement is the most uncomplicated.


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, reserved language words forThe convenience is written in capital letters, in practice you can use a lowercase letter. The variables and operators used are shown in curly brackets. They mean:

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

Working out the constructionfor, Pascal calculates the value{b}, assigns{a}: = {c}, after which the action is restarted:

  • verification of the condition{b}<={c}, during which the cycle stops its work;
  • operator start-up{d};
  • an increase in 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 deterministic cycles.


To understand how it will be donefor, further, an example is given, the code in Pascal language 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 sees on the screen the number "15" - the sum of the numbers from 1 to 5.

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

Common mistakes

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

  1. Counter (cycle parameter){a}is given by an integer.
  2. Values{b}and{c}must also have an 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 incorrectfora: = 0topido.The solution is to use rounding functionstrunc() orround(). The first discards the fractional part of the 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 cycle is completed, the counter{a}can have any value. Although this is contrary to logic, but in practice{a}<>{c}. Conclusion: it is not recommended to contact{a}after the construction is completedfor.
  5. In the body of the loop (that is, after the worddo) it is not recommended to use any operators that change the parameter values{a}. Following this rule will not cause an error, but the program logic will be violated.

The following rule is generally accepted andcorresponds to a "good tone" in programming: to make it convenient to work with the code, you need to write the operators of the loop body not from the first column. For example, making 2-3 spaces to the left or using the Tab key.cycle for pascal

This provision applies not only to the Pascal language. For "dummies" it allows you to quickly find and fix an error in the instructions.

Typical tasks

Suppose you want to tabulate a function of the formf (x) = 3 *x + 15, that is, get a table ofMvalues ​​of functions in the range [x1;x2], wherex1andx2- minimum and maximum values ​​of the argument. To solve this and similar problems is the constructionfor. Pascal recommends that you write the program 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.

Because the step changexnot 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 a cycle

In view of the fact that any operators can be used within a construct, it is allowed to put another cycle in its bodyfor. 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

To work correctly, you needobserve the condition: the counters in each cycle must be different. Otherwise, the inner loop will change the value of the parameter of the outer loop, which will lead to logical errors.

Related news

  • Finance: site map (articles)
  • Medicinal product McMiror complex: application features
  • Why did he put his ears and how to help solve this problem
  • How to mix concrete
  • Tent for winter fishing machine: recommendations for selection and operation
  • Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners Cycle for: Pascal for Beginners