Repeating in Pascal

In Pascal programming language there are three commands for repeating. Command "while" will repeat a part of program while the condition is true; but if the condition is not true at the beginning, the part of program will not run. Command "repeat ... until" first starts the part of program, and then checks the condition; if the condition is true, repeating ends; that means the part of program will be repeated as long as the condition is not true. Command "for" is a simple repeating command, which can be used only if we know at the beginning, how many times do we want to repeat the part of program.

What would you do, if you had for example to jump five times? The usual way is to count while jumping "one... two... three... four... five" and after the "five" jump stop; mathematically speaking, we jump at the numbers less or equal to five, and after each jump we increase the number by one. Using command "while" we can write it like this:

var counter : Integer;

begin
  counter := 1;
  while (counter <= 5) do begin
    WriteLn('Jump!');
    WriteLn('Counter is now: ', counter);
    counter := counter + 1
  end
end

Command "for" is an abbreviation of the following code. If has a starting and ending value, and it automatically increases the variable by one.

var counter : Integer;

begin
  for counter := 1 to 5 do begin
    WriteLn('Jump!');
    WriteLn('Counter is now: ', counter)
  end
end

How would you modify this program, to make it not count numbers from 1 to 5, but for example from 1 to 7?

...

What if we need to write numbers from 101 to 105? One possible solution is to count from 101 to 105. Another solution is to count from 1 to 5, but before writing the number add 100.

var counter : Integer;

begin
  for counter := 1 to 5 do begin
    WriteLn('Number: ', 100 + counter)
  end
end

This method is useful in situations when the numbers do not follow each other closely, but we can find a formula. Command "for" only allows increasing by 1. But what if we want for example to write all even numbers from 2 to 10? We could use the numbers from 1 to 5 and multiply them by two.

var counter : Integer;

begin
  for counter := 1 to 5 do begin
    WriteLn('Number: ', 2 * counter)
  end
end

If we want to write this repeating using command "while", it will be longer, but more legible. Instead of formula we can directly write that we want numbers from 2 to 10, increasing by 2.

var counter : Integer;

begin
  counter := 2;
  while (counter <= 10) do begin
    WriteLn('Number: ', counter);
    counter := counter + 2
  end
end

We can also count in the opposite direction, that is from 5 to 1. Using command "while" the change is simple; instead of 1 we start with 5; in each step instead of adding one we subtract one; and instead of checking if number is less than 5, we check whether it is greater than 1.

var counter : Integer;

begin
  counter := 5;
  while (counter >= 1) do begin
    WriteLn('Number: ', counter);
    counter := counter - 1
  end
end

When using command "for" we again have to find a good formula... which will change the increasing sequence of numbers, for example from 1 to 5 to a decreasing sequence from 5 to 1. In this situation the formula is "6-x". (Think about why the number in formula is 6 and not 5; that would be a common error. What results would formula "5-x" give for values "x" being numbers from 1 to 5?)

var counter : Integer;

begin
  for counter := 1 to 5 do begin
    WriteLn('Number: ', 6 - counter)
  end
end

As we see in these examples, command "for" is a simplified version of command "while", used for the sequences of integers increasing always by one. If the numbers are not in such sequence, we can use some formula. Code with command "while" is longer, but for numbers from other sequences it may be easier to read.

Comments: 0


Google