ComputersSoftware

Turbo Pascal. While ... do is a loop with a precondition

Turbo Pascal, although not a world-wide favorite application for programming, but the creators making the first steps in writing software, begin to familiarize with this environment. It gives an idea of branching, operators, functions and procedures, as well as many other things. For example, while learning, the programmer will encounter cycles in Turbo Pascal: While, For, and Repeat.

The concept of a cycle and its variety

A cycle is called repeated actions. In this environment, we use:

  • With the parameter (For ... to ... do);
  • With a precondition (While ... do);
  • With a postcondition (Repeat ... until).

The first kind is used when it is known in advance how many steps in the solution of the problem. However, there are a number of tasks when there is no information about how many times one or another action will be repeated. In this case, in the Pascal While cycle becomes indispensable, as, in principle, and Repeat.

Structure of the cycle

What is the essence of the work in Pascal While, For and Repeat cycles? These structures have a header and a body. The first component specifies the variables that will "work", the conditions for verifying the truth, the period to which the body will be executed are specified. In the second component, expressions are written that must be used if the condition is satisfied, that is, True, and not False.

When iteration is performed on the last line of code, then it returns to the header where the condition is checked. In the case of truth, the operations are repeated, and in case of non-fulfillment of the condition, the program "leaves" the cycle and performs further operations.

The While loop looks like this. Pascal ABC and similar programs require writing this code:

  • While Condition do;
  • Begin;
  • The body of the cycle;
  • End.

In the event that 1 operator (1 action) is executed in the body of the loop, then the "begin" end brackets can be omitted.

Flowchart of cycle

In Turbo Pascal While has the following features:

  • Complex conditions can be used inside the structure;
  • After the word do, there should not be a semicolon (this is considered an error in Turbo Pascal and Pascal ABC);
  • Variable, constant or expression that is used when getting a False output of their subroutine must be of a logical type, that is, Boolean.

The block diagram of this version of the cycle looks like the following. It shows the sequence of actions.

The algorithm of the cycle

In the simplest programming environments, including Pascal ABC, While the loop operates according to the following principle:

  • The given iterations, i.e., repetitions, will pass so many times until the condition is true (True);
  • As soon as the condition is not satisfied and gives a False response (or otherwise "False"), the statement exits the loop;
  • As soon as this happened, the program "went" in the construction, after the cycle.

This is an essential difference from While from Repeat, ie, a cycle with a precondition from a postcondition.

It is very important to provide in the body of the loop the final change to the specified variable in the While heading. In any case, there should someday come a situation giving the value False. Otherwise, the loop will occur, and then you will have to use additional measures to exit the compiler. Such errors are considered rude and unforgivable.

How do I exit the program during looping?

Often a situation occurs when the While Pascal operator issues a loop in the written code. What does this mean? Iteration is repeated an infinite number of times, because the condition is always true. For example, here is a fragment of the program:

  • While 2> 1 do;
  • Write (1).

In this case, to stop the task, just press CTRL + F2.

There are also 2 ways to control this behavior of the program. For example, if you enter into the code Continue, which will transfer control to the beginning of the cyclic construction (here the exit condition from the loop is controlled, i.e. the execution of the current iteration will be interrupted). Then control is passed in the While loop to the previous check.

The Break statement is able to abort the execution of the entire loop and pass control to the next iteration. Here, the output from the structure will not be controlled. The image shows examples of using these operators.

Problem Solving

Consider the While loop. Pascal's task is to solve a wide variety. Let's dwell on the simplest while to understand the principle of work. Solved tasks in the program Pascal ABC. But images of the classical Turbo Pascal environment will be presented for comparison.

Task 1: Given a function Y = 5-X ^ 2/2. Create a table of values with step sh = 0.5 on the interval [-5; 5].

Algorithm of actions:

  • Set the initial value for variable X equal to -5 (that is, the beginning of the interval);
  • Calculate the value of Y until the variable x reaches the end of the specified segment;
  • Display the values of the function and abscissas (X);
  • Increase X by the given step.

This is the code in the Pascal ABC program.

What does the code look like in Turbo Pascal. The image below shows this clearly.

Task 2: Given an array A, consisting of positive integers and negative numbers. It contains 10 elements. It is necessary to form a matrix B, in which positive elements of array A with an even index will be displayed. Display on the screen the sum of squares in the number of the new matrix.

Algorithm of actions:

  • It is necessary to write a subroutine that will "work" only with elements of array A having 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 A corresponds to the condition x> 0, then the counter of the array elements is incremented by 1. The current value of the counter variable will be the index of the copied number in array B.
  • Initially, summa, responsible for finding the sum of the squares of positive numbers, is assigned 0. Then, the operation will be performed: a new value of the square is added to the previous sum.
  • Do not be afraid if not all the positive numbers have moved from one matrix to another. You have to be careful. Many novice programmers rewrite the code in a panic. We must carefully study the condition: positive numbers located on even "places", that is, having indices that are multiples of 2.

Manual tracing is necessary to ensure that the calculations are correct. Sometimes with the help of this method you can identify errors that do not come to your eyes when you normally check the written code.

If you perform manual calculations, you can make sure that the program works correctly. This, in turn, indicates that the algorithm for creating the code is correct, the sequence of actions leads to a logical end.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.