Turbo Pascal Level I

Course Notes
These notes belong to:
written by Pierre Tsui and Ricky Tam
modified by Ricky Tam

Program
Hello_World;{This program outputs "Hello World" to the screen.}
uses wincrt; {tells the computer you need to the "wincrt" unit}
Begin {beginning of main program}
Writeln (‘Hello world!’);
End.
var
Counter : integer; {declares an integer variable}Name, Name2 : string; {declares two variables of type string}
255 characters
once the Enter key has been pressed.
return character.
Program structure with variables :
Program
Namer;uses wincrt;
Var Name : string;
Begin
Writeln (‘Hello, what is your name?’);
Readln (Name);
Writeln (‘Nice to meet you ‘, Name, ‘.’); {note how variables and strings are separated with comas!}
End.

Program
Display_Procedures;uses wincrt;
Procedure Opening_Line;
Begin
Writeln (‘Hello, I’’m located in the procedure!’);
End;
Begin {beginning of main program}
Opening_Line; {calls the procedure "Opening_Line" as declared above (no variables)}
Writeln (‘Now I’m in the main body of the program!’);
Writeln (‘The end!’);
End. {end of main program}
Program
Use_Function;uses wincrt;
Var Num1, Num2, Answer : integer; {declares three variables of the same type together}
Function Add (Num1, Num2 : integer) : integer; {":integer" = function Add returns an integer value}
Begin
Add := Num1 + Num2;
End;
Begin
Writeln (‘Give me a number.’);
Readln (Num1);
Writeln (‘Give me another number.’);
Readln (Num2);
Answer := Add (Num1, Num2); {calls function Add, with variables Num1, Num2 in brackets}
Writeln (‘The sum of ‘, Num1, Num2, ‘is ‘, Answer);
End.
Program Dummy;
uses wincrt;
Procedure Need_Val (Words : string);
Begin
Writeln (‘I am spitting out random characters.’);
Writeln (Words);
End;
Begin {Main Program}
Need_Val (‘asdfi c;vna jvioenf’);
End. {Main Program}
- Variable parameters are open to both the procedure and the main program.
However because they are variable, they can be changed by the procedures.
Program
Dummy2;uses wincrt;
var number: integer;
Procedure Change_Value (var num: integer);
var temp: integer; {a local variable that ceases to exist after Procedure Change_Value is done}
begin
num := 5;
end;
begin {main program}
number := 1;
writeln (‘Before entering procedure, the number is ’, number);
Change_Value (number);
writeln (‘After executing the procedure, the number is ‘, number);
end.
What happened? Now, try the same program, only this time, leave out the word var in the line:
Procedure Change_Value ………

- Loops are really good things in programming. They can also cause a great deal of headaches, and long nights. We’ll start introducing some now…
For Loop:
Program
For_loop;uses wincrt;
Var counter: integer: {a counter is needed for all for loops}
Num: integer {some number I’ll be adding to}
Begin
Num := 0; {initializing the variable num}
For counter := 1 to 10 do begin {1 to 10 = 10 repetitions}
Num := num + 5; {for every repetition of the loop, I’m adding 5 to Num}
End;
writeln (num); {what number should we get?}
End.
- Basically, for loops can be considered to be "definite loops" where we know how many times the program will go through the loop.
- The variable name counter is NOT important -- it can be changed to num, Ricky or sldfj (as long as it is declared in the var section).
- 1 to 10 specifies the lower and upper limits. It doesn’t have to be an integer, it can
actually be of type char, just make sure that the counter is of the same type as your upper and lower limit. For instance :
Program
For_loop2;uses wincrt;
Var ch: char;
Begin
For ch := ‘a’ to ‘z’ do begin
Writeln (ch);
End;
End.
Program For_loop2 uses a counter with the name "ch" and the loop is repeated 26 times from ‘a’ to ‘z’.
- Instead of counting from lower limit to upper limit, we can actually count from upper to lower limit with the expression downto.
for counter := 10 downto 1 do begin…. End; This will count from 10 to 1!
- The for loop limit and loop counter CANNOT be changed in the body of the loop!! Meaning, once the loop has started, it will go on for however many number of times specified. A command which changes the value of the counter in the body of the loop will have an effect on the number of repetition of the loop.
- Lastly, it is possible to have nested for loops where there is a for loop inside another for loop. This means for every repetition of the outside for loop, we’ll have to go through every repetition of the inside for loop.
Repeat … Until Loop:
Program
Repeat_Until;uses wincrt;
Var name: string;
Begin
Repeat
Writeln (‘What is your name?’);
Readln (name);
Until name = ‘Ricky’; {loop termination evaluated here}
End.
- Repeat…Until is an example of an indefinite loop where we don’t know exactly how many times this loop to repeat, it all depends on what the user’s responds are.
ie. num := 1; {integer variable ‘num’ is assigned the value of 1}
ie. if num = num2 then …… {num is being compared to num2}
While Loop:
Program
While_loop;uses wincrt;
Var name: string;
Begin
Writeln (‘What is your name?’);
Readln (name);
While name <> ‘Ricky’ do begin {loop termination evaluated here}
Writeln (‘What is your name?’);
Readln (name);
End; {end of while}
End.
- Note that if on the first try, the user types in "Ricky", we do not enter the loop. We only enter the loop name does not equal to "Ricky". The important thing to know about While loops is that the condition which causes the loop to terminate is evaluated at the BEGINNING of the loop.
- Similarly, this is another example of an indefinite loop. We don’t know how many times this loop will repeat itself.
Infinite Looping:
- They are undesirable!!
- In a loop, the body of the loop has the power to change conditions so that eventually, the loop termination condition will be met. An infinite loop is a loop where the body of the loop does not bring the loop closer to termination.
- Simply put, make sure that conditions which cause a loop to terminate can occur
- For example, we will have an infinite loop if we are adding 2 to an odd number and the loop termination condition is for the number to be an even number.
Program
Infinite_looping;Var X: integer;
Y: integer;
Begin
Y := 15;
Repeat
X := X + 1;
Until Y = 20;
End.

- The easiest way to think of selection statements is ‘a fork in the road’. If you want to go to one place you either take the left fork or the right fork and so on. The simplest selection statement in Turbo Pascal is the if…then… statement. More complex selection statements are variations of the if…then…statement.
- Selection statements involve conditions. That is, if something holds then you proceed on, otherwise do something else. Here’s an example :
Program
Year_Verifier;{This program asks for your year of birth and determines if you’re part of the techno generation}
uses wincrt;
Var Year : integer;
Begin
Writeln (‘So what year were you born in?’);
Readln (Year);
If (Year > 1980) then begin
Writeln (‘Congratulations! You qualify for our free computer memory giveaway!’);
End;
End.
- Often you might want to compare values. In this case, logical operators (e.g. AND, OR, NOT, <, >, =) become critical. Here are a few and how they work:
Or : <Expression 1> or <Expression 2> …
If any one of the expressions is TRUE, then the entire statement is true.
And : <Expression 1> and <Expression 2>
Both expressions have to be true in order for the entire statement to be true.
Not : not <Expression1>
This is the rather funky logical operator. This is true if the expression is false to begin with and false if the expression was true to begin with. In a nut shell, it takes the opposite of the expression.
<,>,=: These are the same as the equivalent math symbols. If you don’t know what these are, we have some nice CD-ROM math tutorials to refer you to.
- So what are some more complex selection statements? The next one to know about is the if…then…else…statement. It’s basically the same as the if…then… statement, but the else part instructs Pascal on another course of action. For example, if you’re 16 and over and have a car then drive to school, else take a bus.
- So suppose you have a lot of selection statements! You really don’t want to type out 15, 20, 30, or 4,000,000 if…then… statements. That’s where the case statement comes in handy. Here’s a sample use :
Program
Age_Requirements;{This program takes your age and gives you key characteristics of people in that age range.}
uses wincrt;
Var Age : integer;
Begin
Writeln (‘So, how old are you?’);
Readln (Age);
Case Age of
1: Writeln (‘Have you been toilet trained yet?’);
2..10 : Writeln (‘Aww…you’re just a young one!’);
11..17 : Writeln (‘You probably like (or know someone who likes) the Backstreet Boys!’);
18..25 : Writeln (‘You are asking the supreme question : What am I going to do with my life?!!!!’);
26..100 : Writeln (‘Readings are unclear today. Please come back later!’);
End; {end of case statement}
End.
- Notice that at the end of the case statement is the End statement. Another thing to remember is that case is restricted to ordinal types (in other words you can’t use real or string types, values must be singular in nature).

- So why use file handling?
Advantages:
- Data can be updated without changing any part of the program
- Data is "universal" to many different program
- A more efficient method of data input
Disadvantages:
- Data files are "sequential structures", components of a file cannot be accessed
randomly.
Steps to writing a text file:
var Diskfile: text; {text is an actual data type, just like integer, char…}
assign (Diskfile, ‘a:\text.txt’);
3. Open file for re-writing
rewrite (Diskfile);
4. Writing to file
write (Diskfile, {data goes here});
e.g. Write (Diskfile, ‘this is test data which will be saved to the file’);
close (Diskfile);
Steps to reading a text file:
var Diskfile: text
Ch: char; {note, we defined Ch as type char, so that content of file can be retrieved char by char}
assign (Diskfile, ‘text.txt’);
reset (Diskfile);
Read (Diskfile, Ch); {reading in content of Diskfile, Char by Char}
Write (ch); {this is to display what the computer just read}
close (Diskfile);
EoLn and EoF:
- EoLn is true when we are at the end of the line.
- EoF is true when we are at the end of the file.
- Basically, EoLn tells the computer where the end of the line is, and EoF tells the
computer where the end of the file is.
- Using a while loop, we can get the computer to read the file char by char and
display it back on the screen.
While not EoF (Diskfile) do
Begin
Read (Diskfile, Ch);
Write (Ch);
End; {read char by char till the end of the file.}
- Use Readln (Diskfile) to skip EoLn markers. Note, it is important to specify
(Diskfile) because it tells the computer where to get data from (in this case, from the file)
- Similarly, use Writeln (Diskfile) to skip EoLn for display as well.
- You’ll often hear that in programming, you do the coding last. As with everything else, planning is crucial!
- This is where Pseudocode (soo-do-code) comes into play. Pseudocode (or fake coding) helps you to formalize the logic involved in your program. So what are the steps? What does it involve?
- Firstly, break down the whole task or program into simple sub-tasks (these will become your procedures).
- Inside each procedure / sub-task, use a combination of English and some easy programming language to describe what is to be done. This gives you, the programmer, a rough idea of what you’ll have to code when the time comes.
- Finally, go through and make sure the logic is reasonably correct and begin programming (it might also be a good idea to have some caffeine handy…I hear Jolt is a good candidate!).
- Remember pseudocode cannot be used in the actual programming. In other words don’t type in your pseudocode directly into Turbo Pascal. It won’t run!!

- You’ll often see that other tools (along with pseudocode) are used in the planning process (e.g. flowcharting, structural diagram). Which do you use? Which ever one helps you see the structure of your program best and whichever one helps you the most.
Program
Store_Numbers_In_Array;uses wincrt;
Type ArrayTYPE = array [1..100] of integer;
Var counter : integer; {used to move through the array}
Storage : ArrayTYPE; {used to store the numbers in a 1D array}
Begin
Writeln (‘Begin entering 100 numbers and press enter at the end of each number.’);
For counter := 1 to 100 do begin
Readln (Storage [counter]);
End;
Writeln (‘Here are the numbers that you entered.’);
For counter := 1 to 100 do begin
Writeln (Storage [counter]);
End;
End.
To setup an array, follow these steps: