Loop A Loop

 

Hi ! Nice to meet you ! We meet again in Loop a loop. This course suppose to be long and you feel quite bored, don't you ? Well, let's make it quite straight forward.

In many cases in programming, we need to repeat processes. In that case, we need some loops. Pascal understands it, therefore it provides three types of loops :

  1. For ... to ... do
  2. While ... do
  3. Repeat ... until

Well, let's look inside them :

1. For ... to ... do

Syntax :

For variable := fromwhat to what do
begin
  :
  :
  statements / commands
  :
  :
end;

Example : Suppose i is an integer

For i:=1 to 5 do
begin
  writeln(i);
  if i=5 then writeln('Finished !');
end;

It yields 1, 2, 3, 4, 5, then the message 'Finished !'. You see that inside the begin-end block, you could add any commands you want. If there is only one command inside it, you may omit the begin..end, just like if we have studied before. Suppose you omit the if i=5 ..., so inside the begin..end we have only writeln(i);. We can omit the begin..end, then it becomes :

For i:=1 to 5 do
  writeln(i);

If we want to count backwards, i.e. from 5 to 1, we just change to into downto, so check this out :

For i:=5 downto 1 do
  writeln(i);

How about if we want to step more than one, i.e. 1, 3, 5, 7, 9, ...? BASIC provides the STEP how about Pascal ? Well, in this case, Pascal DOES NOT provides such, but if you are creative, you are able to do it. This applies to fractional step as well. BUT, Pascal does include special feature. Check this :

{ Suppose c is a char variable }

For c:='A' to 'Z' do write(c,' ');

You may also write For c:='Z' downto 'A' do write(c,' '); as well. You may even write this : (Suppose b is a boolean)

For b:=false to true do writeln(b);

And many even stranger cases than this, if you have studied type statement.

2. While...do

Well, if you have learned BASIC, you will find that While...do is quite the same as WHILE ... WEND. The syntax is :

While conditions do
begin
  :
  (statements / commands)
  :
end;

Before we enter the begin..end block, FIRSTLY it checks whether the condition(s) are satisfied. If not, it just skips it out, or else it does any commands inside begin..end. When it reaches the end, it checks the condition again. If it is satisfied, then loop again. If not, go out. It goes on and on. The logic is like this :

While..do logic

For example :

i:=1;
While i<6 do
begin
  writeln(i);
  i:=i+1;
end;

You may also combine conditions with AND, OR, or XOR. If Pascal does not provide STEP like BASIC, you can change the for into while that has a lot more flexibilities.

3. Repeat ... Until.

The syntax is :

Repeat
  :
  statements / commands
  :
Until condition;

Unlike While...do, Repeat...Until does not check for conditions at first. It just enters the block and do anything inside. Beware that Repeat...Until does not require begin...end. AT THE END, it then checks whether the conditions are satisfied or not. If SO, it QUITs. That is the third difference from the While...do. If it does not, it simply loops back. So, the logic is like this :

While..do logic

For example :

(uses crt first)
Repeat Until Keypressed;

OR :

i:=1;
Repeat
  writeln(i);
  i:=i+1;
Until i>5;

In While...do and Repeat...until there is a chance to get an infinite loop. Simply give an impossible conditions. But remember, that this also a drawback if we don't use it carefully. Example :

While 2<4 do                 OR     Repeat
begin                                    (bla bla bla ...)
  (bla bla bla ...)                    Until 3>5;
end;

(2 is always less than 4, so           (3 is never be able to exceed 5, so
While...do get an infinite loop)        Repeat...until goes forever).

Or you might just:

While true do                   OR     Repeat
begin                                    (bla bla bla ...)
  (bla bla bla ...)                    Until false;
end;

(They yield the same effect as previous examples : infinite loops).

You may also nest the loop, i.e. a for inside another for, a While in another While, etc. And even, you may nest one form of loop inside another form, like a for inside Repeat...Until. Example :

Repeat
   :
   For ... do
   begin
     :
   end;
   :
Until ...;

It is perfectly legal.

How about if I want to bail out in a middle of loops ? Easy, use Break. Example :

   begin
      for i:=1 to 10 do
      begin
        writeln(i);
        if i=5 then break;
      end;
      writeln('Finished !');
   end.

It yields :

1
2
3
4
5
Finished

Got it ? Break means go out from current loop. It can also be applied on while...do and repeat...until. How about nested loop ? Break only applies on its begin...end block. Check out the program below :


   begin
      for i:=1 to 4 do
      begin
         for j:=1 to 5 do
         begin
           writeln(i,' ',j);
           if j=3 then break;
         end;
         writeln('Next i loop');
      end;
      writeln('Finished !');
   end.

It yields :

1 1
1 2
1 3
Next i loop
2 1
2 2
2 3
Next i loop
3 1
3 2
3 3
Next i loop
4 1
4 2
4 3
Next i loop
Finished !

Another example :


   begin
      for i:=1 to 5 do
      begin
         for j:=1 to 3 do
         begin
           writeln(i,' ',j);
         end;
         if i=2 then break;
      end;
      writeln('Finished !');
   end.

It yields :

1 1
1 2
1 3
2 1
2 2
2 3
Finished !

Break only does its function in its current scope (begin...end block) How about if I want to skip instead of quitting current loop ? Well, use Continue instead of Break. Replace all Break in previous examples with Continue and see what happens.

Extra !!!

Here we are, have additional lessons :

  1. Random and randomize
  2. Halt
  3. ASCII characters

Random and randomize

All of them are used to generate random numbers. Random numbers are always used to simulate the reality, where certainty is hardly guaranteed. We are here not to discuss how to make it, but how to use these two commands.

Randomize is used to start the random generator, so that it is only used at the first initialization, placed after the begin in the main block. It is enough to call it once, eventhough it may be called several times. Random is used to get the random number. Its parameter is the highest possible number that can be generated. Suppose : num:=random(50); It means that num will contain any integer number between 0 to 49. Look at next example :


var
  i : byte;
begin
  randomize;
  for i:=1 to 100 do
    write(random(30),' ');
end.

Let's remove the randomize. Run it for several times and note that when it runs, if you don't use randomize, the computer will result the same number. So, always use the randomize so that the computer generate quite a good random numbers.

Halt

Just stop and return to system. Usage : Halt; You may place it anywhere you want. If computer encounters the halt, it will stop the program execution and back to the operating system.

ASCII characters

There are 128 basic ASCII characters and 128 extended ASCII characters. Writing ASCII characters is as simple as using writeln; Example :

  writeln(#30); { Writing ASCII code 30 }

Try changing 30 with other numbers between 0 to 255.

That's all. We'll have a quiz. Well, it's not hard, actually, but it is harder than before. Bye for now !


Where to go ?

Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 4 about constants
To Chapter 6 about procedures and functions
My page of programming link
Contact me here


By : Roby Joehanes, © 1996, 2000