Czech version
logolink

< Back to the list of lessons

Introduction to Pascal

AlgortimyContent of the lesson:

  • About Pascal
  • The Structure of Program Written in Program
  • Variable and Its Data Type
  • Assigning Values to Variables
  • Input and Output
  • Simple Mathematical Formula Calculation Example

About Pascal

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).

The Structure of Program Written in Program

Each program in Pascal consists of three parts:

  • header of the program
  • declaration part where identifiers (variables) used in our program are defined
  • command section which contains the procedure of the computational process itself
A sample of a program in Pascal
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:

  • the program stars with so called keyword program and its name (you can set any name)
  • declaration part can contain also other commands but in this example it contains the var command only, followed by a list of variables and their data types (explained below)
  • command part starts with the begin command and end with the end command - every step of your program will be written between these steps
  • every line is ended by a semicolon (it is not a rule - begin command does not have a semicolon)
Generally the program has the following structure:
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.

Variable and Its Data Type

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.

Variable declaration in Pascal
 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.

Declaration of variables x,y,a,b in Pascal
 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.

Assigning Values to Variables

Once we have declared a variable we can start using it in the program. We can assign it a value using the := operator:

Assigning a value inside x variable in Pascal
 x:=7;

Input and Output

Input

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.

Entering the value for the variable x in pascal
 Program Zadani_a_vypis_x;
 var x:integer;
 begin
 readln(x);
 readln;
 end.

Output

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.

Writing Hello text on the screen in Pascal
 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.

Writing variable's value to console in Pascal
 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.

Writing variable and text into console in Pascal
 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.

Input and Output in Pascal
 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.

Simple Mathematical Formula Calculation Example

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:

  • + addition
  • - subtraction
  • * multiplication
  • / division

Example: Compute the capacity and the surface of a cube.

Capacity and surface or a cube in Pascal
 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.

Individual Task

Write a program to compute the capacity and surface of a block.

Questions

  1. What are the main parts of a program in Pascal?
  2. How can a variable be defined?
  3. How can user enter a value to a variable?
  4. How can text be written to console?
webdesign, xhtml, css, php - Mgr. Michal Mikláš