While loop

Another loop, which we will become familiar with, will be the loop with the condition at the beginning - the While loop. In programming, we use its design to solve the problems in which we do not have a predetermined number of repeats. Unlike the For loop, its structure can be in some cases more advantageous and more transparent. Everything we can do with the For loop can be programmed with the While loop as well.

As with the For loop:

  • The initial value for the control variable has to be set
  • The condition is used to ensure loop termination (the body of the loop is performed only until the condition is met) - the value is true
  • Set the control variable to changeable value (the For loop ensures itself)

The While loop first tests the expression (condition) and if the value is true, the command will be executed.

Syntax of the While loop:

while condition do command;

If we want to repeat a sequence of commands, we will use the compound command (the commands which are to be executed in repetition have to be enclosed between the words begin and end).

while condition do begin

command1;

command2;

... end;

Implementing the While loop will be as follows:

  • First, the condition (logical expression) is evaluated. If the condition is met (valid), a command (or a compiled command) is executed in the order it is written. If the condition is not met (not valid), the orders are not executed. This process is repeated until the condition is valid. The end of the loop occurs when the condition first time will be invalid (it will become false).
  • The condition is always tested at the beginning, that is, before the commands are executed. If the condition is not fulfilled, the loop is terminated. In this case, the body of the loop is not once performed.
  • It is necessary to realize that we must ensure that from a certain moment the condition ceased to apply. Otherwise, there would be an endless loop and the program would never finish.

We said that the commands "wrapped" in the While loop are executed when the condition is met. Under the condition we usually mean some mathematical expression whose value is either true (commands are executed), or false (commands are not executed). Such a term is called a logical expression. It is often a comparison of some numerical values, while in Pascal, respectively in Lazarus, we have these options of comparisons:

a < b - a is smaller than b

a <= b - a is smaller than or equal b

a > b - a is bigger than b

a >= b - a is bigger than or equal b

a = b - a equals b

a <> b - a is not equal b

By using the While loop we draw growing squares so that the last (largest) completely fits in the graphical area. How many squares will be drawn then depends on the size of the first square, the size of the graphical area, and how much each square will grow. The first square will have the page size = 10, and it starts drawing itself in the X coordinate X = 10 (Figure 64).

Figure 64: Increasing rectangles drawn by While loop
Figure 64: Increasing rectangles drawn by While loop
Figure 65: While loop
Figure 65: While loop

Before the program draws the square, it checks if the condition is true, that is the right side of the square is smaller than the size of the graphical area (this check is done before drawing each square).

Note that in addition to drawing a square, we wrote the commands for increasing the X coordinate value and enlarging the side of the square. These values will always change after drawing each additional square (similarly to the For loop), that is the second square will have an X coordinate of 20 and a side size of 20.

Try to change the constant 250 (in the condition) to Image1.Width to have the program more versatile, and it itself figured out how wide the graphical area is. 


Using the loop with the condition at the beginning, we can count for example:

  • The whole part of the square root
Figure 66: Root square calculation using the While loop
Figure 66: Root square calculation using the While loop

To enter the input (entry of the root number we want to produce) we have used a new component Edit1 from the Standard tab. This component can only work with text (sequence of characters) that is of the String type. In order to be able to read numeric values, the conversion is necessary to be done (a change of the data type). When entering a value into a variable N, first it converts the String to the Integer (StrToInt). When writing the result to the Label component (also of the String type), we have again used the conversion, this time we changed the Integer to the String (IntToStr). If we did not do the conversion, the program would alert us to the bug because it would not know what action we want to perform.

In the Object Inspector we can set the Text attribute of the Edit1 component to a zero value (we will erase Edit1) and when we run the program, we will see a blank input window ready for our inputs (Figure 67). Then we can enter the number from which we want to calculate the square root and get the result after pressing the button (Figure 68).

Figure 67: Program prepared for the Edit component entry
Figure 67: Program prepared for the Edit component entry
Figure 68: Edit component entry to extract the square root of the number into the Label component
Figure 68: Edit component entry to extract the square root of the number into the Label component
  • Factorial numbers
Figure 69: Factorial calculation using the While loop
Figure 69: Factorial calculation using the While loop
Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky