Loops

Loops


 


1. What are the advantages of loop operators in the program?

Sometimes solution algorithm may be implemented so that it is necessary to perform the same operation several times. The algorithm, in which specific sequence of commands is repeated several times is named as cyclic.

The loop operators are used for convenient organizing of cycle process in the program.


2. What are the types of loop operators in Pascal?

In Pascal there are three kinds of loop operators:

– the cycle with a parameter (fortodo);

– the cycle with a precondition (whiledo);

– the cycle with a postcondition (dowhile).


3. What is the general view of loop operator with a parameter?

The loop with a parameter has two variants of realization.

Variant 1. The general view of loop with a parameter:

for Counter := InitialValue to EndValue do
begin
  // some instructions

end;

where Counter is the variable of enumerated type, which in the cycle has increment equal to 1; InitialValue – the initial value of parameter “Counter“; EndValue – the final value of parameter Counter.

If InitialValue is more then EndValue, instructions between “begin” and “end” is not executed. If is only one instrutction between the “begin” and “end” then keywords “begin” and “end” can be omitted.

Variant 2. The general view of loop with a parameter:

for Counter := InitialValue downto EndValue do
begin
  // some instructions

end;

In this case the value of parameter Counter doesn’t increased at 1, but is decreased at 1 (the step of change is 1).

An example of using the operator of loop with a parameter.

A code snippet of the factorial calculation. The factorial is calculated by the formula:

n! = 1 * 2 * 3 * … * n.

 

...

var 
  i,n:integer;
  f:real;

...

begin

  // n - is given

  f:=1;

  for i:=1 to n do
    f:=f*i;

  // f - the result of factorial

end.


4. What is the general view of operator of loop with precondition?

The general view of operator of loop with a precondition:

while condition do
begin
  // some instructions

  // ...
end;

An example of using the operator of loop with precondition. A code snippet where is calculated the following sum:

S = 2 + 4 + 6 + … + 200

 

...
s := 0;
i := 0;
while i<200 do
begin
  i:=i+2;
  s:=s+i;
end;

...


5. What is the general view of operator of loop with postcondition?

The general view of operator of loop with postcondition:

 

repeat
  // some instructions


until condition;

Operators that are placed between the keywords “repeat” and “until” will run until, when the logical statement, which specified after “until“, does not take a “true” value.


6. An example of calculating the sum by using different loops.

Task

Using the loop you need to calculate the sum:

S = 1 + 2 + … + 100

Solving 1. Using the loop with a parameter for.

// variable definitions
var
  s, i : integer;
begin
  ...

  s:=0;
  for i := 1 to 100 do
    s := s + i;

  ...
end.

Solving 2. Using the loop with precondition “while“.

// variable definitions
var
  s, i : integer;
begin
  ...

  s := 0;
  i := 0;
  while i<100 do
  begin
    i := i + 1;
    s := s + i;
  end;

  ...
end.

Solving 3. Using the loop with postcondition repeat.

// variable definitions
var
  s, i : integer;
begin
  ...

  s := 0;
  i := 0;
  repeat
    i := i + 1;
    s := s + i;
  until i>=100;

...
end.


7. What are the operators of the loop completion?

For all the loop statements, exit from the loop is carried out as a result of the natural ending of the cycle operator, and with the help of exit statements.

In the classic Pascal are defined the standard operators Break and Continue.

Operator Break makes unconditional exit from the loop. Operator Continue carries out the transition to the new iterate.


8. An example of using the “break” statement.

Sometimes, it is convenient to exit the loop not by check result, which is realized at the beginning or ending of the cycle, as in some another way. This possibility gives the “break” statement.

A code snippet that detects the presence of a given element in the array m1. The value of entry is determined in the variable x. If the element is in the array then is inexpedient to search in the array further. In this case is used the “break” statement.

...

f_is := false; // variable that determines whether there is an element x in the array m1

for i := 1 to 100 do
begin
  if m1[i]=x then
  begin
    f_is := true; // item x in the array
    break; // exit from the loop, inexpedient to search on further,
    // because the item has already been found
  end;
end;

...


9. An example of using the continue statement.

The “continue” instruction is used, if you need stop the current iteration and go to the next iteration.

Sample code snippet, which multiplies by 2 all the positive elements of an array of 100 integers. If the negative element occurs, it is skipped.

...

for i := 1 to 100 do
begin
  if m1[i]<0 then // skip the negative element
    continue;     // and go to the next iteration
  m1[i] := m1[i]*2;
end;

...