Turbo Pascal MSX – Lesson 5

da | Dec 2, 2023 | Programming, Turbo Pascal | 0 comments

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

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

Turbo Pascal MSX – Lezione 7

Turbo Pascal MSX – Lesson 7

FILES (part 1) Turbo Pascal provides a complete set of instructions for managing files. The FILE type is represented by a sequence of objects of the same type, the size of a file is not determined by the type, but a pointer is provided that moves...

Turbo Pascal MSX – Lesson 6

MSX Turbo Pascal Column by Stefano Roperto (part 6) Chapter 6 PROCEDURES AND FUNCTIONS Turbo Pascal, as we saw in chapter 4, provides the programmer with a complete collection of procedures and functions that make the language modern and complete. In...

Turbo Pascal MSX – lesson 3

MSX Turbo Pascal Column by Stefano Roperto (part 3) Chapter 3 The data type defines the set of values that a variable can take on. The more complex data types that can be defined by the programmer are all derived from the standard types. The guys...

Turbo Pascal MSX – lesson 2

MSX Turbo Pascal Column by Stefano Roperto (part 2) Chapter 2 Introduction to the Turbo Pascal language We will now see how a program in Turbo Pascal is structured. We don't need to understand everything right now, we're just seeing the typical structure...