Content of the lesson:
The Pascal programming language was created by Niklaus Wirth at the University of Technology in Zurich in 1971. Pascal quickly gained a dominant position when teaching programming and it is also used as an example language for programming in Delphi (development software).
Each program in Pascal consists of three parts:
Header part
Program SectiDveCisla;
Declaration part
var a,b:integer;
Command part
begin
readln(a);
readln(b);
writeln(a+b);
end.
In this example, we can see a few basic things:
Header part
Program Name_of_program;
Declaration part
uses library_1;...library_n; (not required)
type defined_data_type_1;...defined_data_type_n; (not
required)
const constant_1=value;...constant_n=value; (not
required)
procedure defined_procedures; (not required)
var variable_1:data_type;
variable_2:data_type;
...
variable_n:data_type;
Command part
begin
command_1;
command_2;
...
command_m;
end.
To be able to keep a value in your program (for example, a number x, which will be equal to five) you need to declare a variable of some data type. For example, in mathematics the notation x=5 is often used. This notation means that x is equal to number 5. If we want to have a possibility to create such x in the program and save some value in it, we have to declare x which means to write x after the command var.
From the mathematical notation x = 5 we can also see that x can take any numerical value (in this case a natural number or a whole number 5). But a computer has to be told not only the name of the variable but also what values it can take (or what data type it is = what type of data it can store – simply if there will be numbers, characters, letters, ...). We can set it using the command x:integer; - this means that any whole number can be stored inside the x variable.
If we want to have a variable x in the program which can store a whole number data type, then we can declare it by the notation.
Program x_variable_declaration;
var x:integer;
begin
end.
We can declare an unlimited number of variables inside our program (we are limited by the RAM memory of course). In case we declare more variable of the same type, we can divide them using commas, we do not have to write them on new lines.
Program x_y_a_b_variables_declaration;
var x,y:integer;
a:integer;
b:integer;
begin
end.
Note that the key word var is mentioned ONLY once in the program at the beginning of the block of variables declaration.
Once we have declared a variable we can start using it in the program. We can assign it a value using the := operator:
x:=7;
By input we understand entering any data to the program (usually values for some variables). To require an input, use the read or readln functions. If you use the readln function without any parameters, it will wait until the enter key is pressed. If you use a parameter, the function will wait as well but after pressing enter it will save everything you have written before pressing enter to a variable.
Program Zadani_a_vypis_x;
var x:integer;
begin
readln(x);
readln;
end.
By output we mean the information provided by the program. It could be information about finished job or some kind of a text message. The output is written to console on the screen. To write anything to console, you can use the write or writeln functions. The only difference is that the write function writes the parameter to console without creating a new line and the writeln function creates a new line in the end. If you want to output a text, you should close it inside quotation marks.
writeln('Hello');
If you want to output any variable's value, you can set it as the parameter without using quotation marks. The following program writes 7 into the console.
Program Vypis_x;
var x:integer;
begin
x:=7;
writeln(x);
end.
You can combine variables and text when using write or writeln, you only have to separate them using the comma character.
Program Vypis_x;
var x:integer;
begin
x:=7;
writeln('Proměnná x má hodnotu',x);
nebo i s tečkou za větou
writeln('Proměnná x má hodnotu',x,'.');
nebo varianta když je hodnota jako prvni
writeln(x,' je hodnota proměnné x.');
end.
We should improve the program for entering a number because a user should know what to do when he runs the program. He also should be informed about the result.
Program Zadani_a_vypis_x;
var x:integer;
begin
writeln('Zadejte prosim hodnotu x:');
readln(x);
writeln('Proměnná x má hodnotu',x,'.');
readln;
end.
It is simple to write a program which makes a sum of two numbers as you saw but we want our computer to compute more difficult mathematical formulas. We will need some additional operands:
Example: Compute the capacity and the surface of a cube.
Program Objem_a_povrch_krychle;
var a:integer;
begin
writeln('Set the size a:');
readln(a);
writeln('Capacity for a=',a,' has value of ',(a*a*a),'.');
writeln('Surface for a=',a,' has value of ',6*(a*a),'.');
readln;
end.
Write a program to compute the capacity and surface of a block.