ComputersProgramming

A useful while loop for the programmer

A beginner programmer may not yet know one useful while loop. This is a typical cyclic operation in the Pascal environment. It is convenient to use in various tasks, so we will analyze how you can include this cycle in the program .

The While cycle, Pascal requires the following format:

  • While (Condition) do (begin if necessary).
  • Algorithm of action.
  • End (if there is a begin command).

The first line is the "cap" of the command. The condition can be the limit numeric value of the variable Integer (d <100), the value of a variable of type boolean (t = True) or string (tex = 'hello'). The "begin" command is set if more than one condition is to be fulfilled in the body of the loop. The following is a description of the actions that the program must perform, provided that the While loop is executed. There may be various commands, procedures, functions, like cycles. In the end, you need to put the command "end", if, of course, the first line was "begin".

Now consider 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 start the loop. It is important to understand that it must work until it reaches the value of the number n. There is more than one solution to the problem, but we will focus on the one that affects the use of the counter that needs to be installed. By default, the variable i is used for this. Before you start working with it, you must assign it a value of "1". The counter serves as a term for each new cyclic action. Based on our task, we constantly need to add one to it. Thus, someday the numeric counter will equal the value of the number n. This will be the signal to terminate the program. To calculate the sum, we introduce the variable k. With each new repetition, it will be assigned the previous value plus the number i. After the final count, you should only output the initial k to the screen. This is a brief explanation of the program in words. Now we are looking at the program code.

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

I: = 1; K: = 0; '- we put in operation the counter, we reset the value of the sum.

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

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

End; '- we finish the description of While.

Writeln (k); - output the data.

Let's get acquainted with one more example. Now we need the user to fill in n names from the keyboard, which will save the text array. The principle here is analogous to the past. Enter the number n, activate the While loop, set the condition. Further, we denote input from the keyboard into the memory of the cell of the array. We set the counter, we finish the repeated operations. Next, reset the counter and output the array. True, resetting the counter in this context means assigning it a value of one, since you can not display the zero cell of the array (it does not exist). The code of the program is as follows: from this program you will get the sum of a series of natural positive numbers, which ends with the entered number n. It should be understood that in the absence of a counter, the While Pascal loop never ends. If you suddenly forget about this, then when you execute the program code, the computer will start to hang. This is treated with the "Pause Break" button. As a counter for a variety, use the variable c.

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; '- we finish the cycle.

C: = 1; '- return the original value to the counter.

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

After that you will get n names on the screen, which were entered from the keyboard. On this acquaintance with the While cycle ends. It is used both by beginners and advanced users. It does not have a counter, therefore it requires special attention of the programmer and an additional variable.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

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