Useful while loop for the programmer

A novice programmer may not yet be familiar with one useful while loop. This is a typical cyclic operation in a Pascal environment. It is convenient to use in various tasks, therefore, we will analyze how this cycle can be included in the program .

While loop

The While Pascal cycle requires the following format:

  • While (Condition) do (begin if necessary).
  • Action algorithm.
  • End (if there is a begin command).

The first line is the "header" of the command. The condition may be the limit numerical value of the variable Integer (d <100), the value of a variable of the type boolean (t = True) or string (tex = 'hello'). The "begin" command is issued if more than one condition is necessary in the loop body. The following is a description of the actions that the program must perform, provided that the While loop is executed. It may contain various commands, procedures, functions, similar cycles. At the end, you need to put the command "end", if, of course, the first line was "begin".

Pascal While loop

Now let's look at examples of programs that contain the described command. For example, we need to find the sum of the first n positive integers. For this, we initially assume n (let it always be greater than zero). Then we start the cycle. It is important to understand that it must work until it reaches the value of n. There is not one option for solving the problem, but we will focus on the one that affects the use of the counter, which must be installed. By default, the i variable is used for this. Before you start working with it, you need to assign it the value "1". The counter acts as a term for each new cyclic action. Based on our task, we constantly need to add one to it. Thus, someday the numerical indicator of the counter will be equal to the value of the number n. This will be the signal to complete the program. To calculate the sum, we introduce the variable k. With each new repetition, the previous value plus the number i will be assigned to it. After the final calculation, you should only display the variable k on the screen. This is a brief explanation of the program in words. Now look at the program code.

Readln (n); '- read the number n.

i: = 1; k: = 0; '- we enter the counter into operation, zero the value of the sum.

While i <= n do begin '- set the condition for the operation of the loop.

k: = k + i; i: = i + 1; '- add the value to the total, update the counter.

end; '- complete the description of the While.

Writeln (k); - output data.

While pascal loop

Let's get acquainted with another example. Now we need the keyboard user to fill in n names that the text array will save. The principle here is similar to the past. Enter the number n, activate the While loop, set the condition. Next, we denote the input from the keyboard into the memory of an array cell. We install the counter, we complete repeated operations. Next, zero the counter and display the array. True, zeroing the counter in this context means assigning it a value of one, since you cannot display the zero cell of the array (it does not exist). The program code is as follows: from this program you will receive the sum of a series of positive integers that ends with the entered number n. It should be understood that if there is no counter, the While Pascal cycle will never end. If you suddenly forget about it, then when the program code is executed, the computer will start to freeze. This is treated with the "Pause Break" button. We use the variable c as a counter for a change.

Readln (n);

c: = 1;

While c <= n do begin '- setting the condition.

Readln (a [c]); c: = c + 1; '- read the data from the keyboard, add the value to the counter.

end; '- end the cycle.

c: = 1; '- return the initial value to the counter.

While c <= n do write (a [c]); '- display n names on the screen.

After that, you will get on the screen n names that were entered from the keyboard. This is where the introduction to the While loop ends. It is used by both beginners and advanced users. It does not have a counter, therefore it requires special attention of the programmer and an additional variable.

Source: https://habr.com/ru/post/K12831/


All Articles