This is the same problem out there in real life. Every nation speaks its own language,
and the 'english' you speak in Canada
is different then
the one spoken in the USA. (color, colour)
So here is the problem, every computer speaks a form of binary, and a VIC20 speaks a different binary language then a MAC, and both of those are different from an IBM machine. Compounding to the confusion, not all machines made by the same company speak the same either. A Pentium computer speaks the same language as a 486 by Intel, but the Pentium knows a few more words then the 486, so it can run any program a 486 can, but the 486 can't run all Pentium programs.
So I want to make a program now, all I want to do is write my name on the screen. I will need to learn the binary language of the machine I want the program to run on. Then if I want it on another machine I have to learn a whole new binary language.
Not only would this be very time consuming just to learn, but also to program. This will be explained more in class. Lucky for us we don't need to learn all those binary languages, there is a much easier way. Compilers.
So what is a compiler? A compiler translate pseudo-english into binary. Then all we need to do is learn the pseudo-english and if we have a compiler for every computer we don't need to remake the program, we just recompile it.
Unfortunatly there are 100's of these different pseudo-english languages as well. So we choose one, and we will choose Pascal since its easy.
Here is a very simple program.
program hello(input, output);
var
name : string;
begin
write('Hello, I am the computer, what is your name? ');
read(name);
writeln('Hello ',name);
end.
The first thing you will of course notice is that the program is not commented. Comments
are english sentences which the compiler will ignore, they are there for the programmer
and future programmers to read if the code ever needs fixxing.
Before we can comment the program though, we need an understanding of the commands.
We are lucky that Pascal is such an easy language to learn because most of the commands
are very simular to english. For your assignments, all programs will ahve 3 main
sections. First is the HEADER, then comes the DECLARATIONS and then the
PROGRAM. We can use the above program as an example of these parts.
program hello(input, output); |_______ HEADER
|
var |_________________ DECLAIRATIONS
name : string; |
begin |
write('Hello, I am the computer, what is your name? '); |
read(name); |-----PROGRAM
writeln('Hello ',name); |
end. |
The header tells the compiler information it needs to know. In this example it is telling
the compiler that this file is a program and it will use 2 things external to the program
and it will call these two things input and output.
Decalirations are important because they tell the compiler about variables and other memory locations you will be using. In this case I will use a variable named name which will be a string (explained later). A variable is a memory locations and I can change its value at a later time, hence it varries.
The last part is the program itself.
Variables ect
Variables are names that I give to memory locations so I can access the information in that
location. So in our above program, I am using a variable named name which the computer
will use to store my name. There are several types of variables.
char is short for character. This is 1 keystroke from the keyboard.
string is a list of characters. It is not really a variable itself, but in fact a list of variables. Since strings are very common they have been included here.
int is short for integer, a whole number.
real are real numbers, numbers with decimal points.
bool is boolean, it is other TRUE or FALSE.
You can also have lists of any of these types of variables, these are called arrays and will be covered later in teh course.
Now to program you need to know this pseudo-english language. Here are some basics...
(anything writen in < > are not to be typed literally, but is information you add)
| COMMAND | SYNTAX | EXAMPLES | DEFINITION |
| write | write( < message > ); | write('Hello'); write('Hello ',name); | Writes information onto the screen or other output device. |
| writeln | writeln( < message > ); | writeln('Hello'); writeln('Hello ',name); | Writes information onto the screen or other output device and adds an end of line character to the end. |
| read | read( < input > ); | read( name ); | Reads information from the keyboard, other other input device, and stores the input into the varibale mentioned. |
| readln | readln( < input > ); | readln( name ); | Simular to read but will read until it finds an end of line character. |
| for | for < var > := < start > to < end > do < statement > | for i := 1 to 15 do writeln('Hi'); | Will repeat a given statement while incrementing a counter until the counter reaches its limit. |
| repeat | repeat < statement > until < condition > | repeat writeln('Hi'); until done(); | Will repeat a statement until a condition is met. |
| while | while < condition > do < statement > | while not done() do writeln('Hi'); | Will repeat a given statement while a condition is true. |
| if | if < test > then < statement > | if a = b then c := d; | Will check if a condition is true and execute a statement if it is so. Optional else. |
| SYMBOL | VALUE RETURNED | EXAMPLE | DEFINITION |
| < | TRUE or FALSE | if a < b then c(); | Less then comparison. |
| > | TRUE or FALSE | if a > b then c(); | Greater then comparison. |
| = | TRUE or FALSE | if a = b then c(); | Equal to comparison. |
| >= | TRUE or FALSE | if a >=b then c(); | Greater then or Equal to comparison. |
| <= | TRUE or FALSE | if a < =b then c(); | Less then or equal to comparison. |
| < > | TRUE or FALSE | if a < > b then c(); | Unequal to comparison. |
| := | none | a=b; | Assignment operation. |
program hello(input, output);
var
name : string;
begin
write('Hello, I am the computer, what is your name? ');
read(name);
writeln('Hello ',name);
end.
Of course it would have been easier to understand what it did if there were comments in the program.
Comments can be added by enclosing them in brackets like { } and the compiler will igore them. So
lets comment our program.
program hello(input, output);
{ Program by: Peter Sadlon
Purpose: Example for CPSC 215 labs
Date: xx/xx/xxxx }
var
name : string; { variable used to store our name }
{ The main body of our program is here, it will ask us to input the name and then
store the name in the variable 'name'. Then it will say hello and quit. }
begin
write('Hello, I am the computer, what is your name? ');
read(name);
writeln('Hello ',name);
end.
Notice I didn't comment every line. Keep in mind the purpose of commenting. It to help others
see what you did, or intended on doing with your code. DO NOT make comments like