Basic Programming Structures

There are just three constructs in imperative languages: sequence, repetition and control.

Sequence

A sequence is simply one instruction after another: do this, now this, then this... Sequences in Pascal are made by placing begin and end around other commands.

Control or Selection

The third major type of programming construct is control or selection. This involves making a decision and changing the direction of the flow in a program. in Pascal this takes place through the if and case statements.

An if statement takes the form:

if condition then ...

The condition takes the form of a Boolean expression such as 'x=1' (does X equal 1?), 'not (x=1)' (does x not equal 1?), 'string='barry'' (is the value of string equal to 'barry'?), and so on.

A Boolean condition may return true or false and the if statement has an extension to deal with the alternative outcome:

if condition then
 statements
else
 alternative statements

For example:

If x > 10 then
 showmessage ('x < 10')
else
 showmessage ('x > 10');

Notice there is no ';' before else, a common source of compilation errors.

This structure can be extended to include further conditions:

if x < 5 then
 showmessage ('x < 5')
else if (x >= 5) and (x <= 20) then
 showmessage ('x between 5 and 20')
else
 showmessage ('x > 20');

Case...of

The case structure can be used where the choice item is not a Boolean condition but a value of ordinal type such as an integer or a char type. In this example the index of the item selected in a combobox is taken and used in a case structure to provide information about the item selected:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 case combobox1.ItemIndex of
  0:edit1.text:='Mercury is closest to the sun';
  1:edit1.text:='Venus has a hostile environment';
  2:edit1.text:='Earth is our home planet';
 end; 
//case
end;

Repetition

Repetition involves doing the same thing more than once. in Pascal it takes three main forms, the for loop, repeat..until and while..do.

For

The for loop uses a counter to carry out the action within the loop a specific number of times. For example:

for i := 1 to 10 do
 showmessage('Value of i is ' + inttostr(i));

'i' is declared as an integer variable. The first line says that i will start at 1 and proceed through 2,3,4... to 10. For each value of i the second line of code will be executed, which in this case does nothing more than display a message giving the current value of i.

The value of i in the for statement can be any two valid integers, for example 0, -20, 65,500, and so on. For larger ranges numbers of type Int64 could be used. If the first number is larger than the second the keyword 'do' is replaced by 'downto', as in:

for i := 10 downto 1 do

The second line could be expanded to any number of instructions by using a begin..end structure:

for i := 1 to 10 do
 begin
  showmessage('Value of i is ' + inttostr(i));
  square:=i*i;
  showmessage('Value of i squared is ' + inttostr(square));
 end;

While... do

A second type of loop construct is the while loop. This takes the form:

while (condition) do
 statements

The condition takes the form of a Boolean expression such as 'i < 10' or 'x <> y' or 'string='please''.

The while structure might be used with a counter to simulate a for loop:

i:=1;
while i <= 10 do
 begin
  showmessage('Value of i is ' + inttostr(i));
  inc(i);
 end;

An example can be seen in the searching programs described here. In both sample programs a while loop is used to test whether the value being searched for has been found. This is done by setting a Boolean value 'found' to false and then, if the required item is found, setting it to true. The while loop is set to repeat until 'found' becomes true and so terminates if the item is found (or if some other condition is met):

i:=1;
found := false;
while not (found) do
 if item_required = numlist[i] then found:= true
 else inc(i);

A while loop is often used when processing sequential files, in particular to read a file:

while not eof (filename) do
 begin
  read(filename, recordname);
  if recordname.fieldname = ... then
  begin ...
  end;
 end;

The while loop is distinguished by the fact that the condition comes before the statements so the statements may not be executed at all.

Repeat.. Until

A third type of loop structure is repeat..until. This is distinguished from a while loop by the fact that the condition comes after the statements so the statements will be executed at least once. For example:

repeat
 inc (i);
 showmessage (inttostr(i));
 until i >=10;

If i starts at 1 then the code will display the showmessage box 10 times, once for each value of i. If i starts at 20 the code will run once and will output a message that i equals 21.

Back