MSX Turbo Pascal Column by Stefano Roperto (part 5)
Lesson 5
Control loops and instructions
In turbo Pascal there are the usual iterative structures present in all high-level languages, the FOR DO loop and the REPEAT UNTIL and WHILE DO structures. Let's look at them in detail and comment on them.
FOR loop:
Program For loop;
var i:integer;
begin
FOR i:=0 to 10 DO
writeln(i);
end;
….
….
End.
The FOR loop does not have many differences, except in the syntax, compared to the basic MSX and is a loop used when it is necessary to repeat a series of instructions a known number of times
REPEAT cycle
Program cycleRepeat;
var i,j,k:integer;
begin
j:=100;
k:=0
REPEAT
i:=i+k*2;
k:=k+1;
writeln(i);
UNTIL (k=j);
end;
…
…
End;
The REPEAT loop is an iterative structure that allows a series of instructions to be executed until (UNTIL) a certain condition occurs, so it is not known a priori how many times the instructions will be executed.
WHILE loop
Program loopWhile;
var i,j,k:integer;
begin
j:=100;
k:=0
WHILE (k=j) C
Begin
i:=i+k*2;
k:=k+1;
writeln(i);
end;the loop
…
…
End.
The WHILE loop is similar to the REPEAT loop, but in this case the loop instructions are executed while (WHILE) the condition is verified and also in this case the number of iterations is not known a priori.
As you can see, there are apparently no differences between the REPEAT structure and the WHILE structure, but in reality there is a fundamental difference that determines when to use one or the other structure. In the REPEAT loop the statements in the loop are executed at least once, in fact the control statement is at the end of the loop, while in principle the statements in the WHILE loop may never be executed as the control statement is at the beginning of the cycle and, obviously this makes the difference in choosing the structure to use in accordance with your algorithm.
As regards turbo control instructions, Pascal provides the IF – THEN – ELSE instruction and the CASE OF instruction. Let's also see a variety of examples here:
IF instruction
program statementIf;
var i: integer;
begin
readln(i);
IF (i=1 )THEN
instructions;
ELSE
instructions;
end;
…
…
End.
Let's consider another slightly more complex example:
IF statement (version 2)
program statementIf;
var i: integer;
begin
readln(i);
IF (i=1) THEN
instructions;
IF (i= 2) THEN
instructions;
IF (i= 3) THEN
instructions;
ELSE
Instructions;
end;
…
…
End.
Here we have a complex control which, although correct from a syntactic point of view, is impractical from a practical point of view. To overcome problems like this turbo Pascal provides the CASE OF control statement:
CASE OF statement
program instructionCase;
var i: integer;
begin
readln(i);
HOMES the OF
1: instructions1;
2: begin
Instructions2;
instructions2;
instructions2;
end;
3:instructions3;
ELSE begin
Else_statements;
end;
end;
…
…
End.
The CASE statement therefore provides some flexibility to control structures and allows you to elegantly resolve situations in which it is necessary to control many different values of the same variable. As we can see, turbo Pascal provides the programmer with complete and efficient iterative and control structures.



0 Comments