Содержание:
Theory
Lecture # 1 in pdf format
Variable Definition & Assigning a value to it
- In Pascal abc.net variables can be defined within the body of the program between
begin
andend
keywords. The principle of locality: a variable is defined immediately before it is used. - When we define a variable, we specify its name and type:
- In traditional pascal:
- pascalAbc.net:
So we have two possible versions:
var n: integer; // variable declaration begin n:=1; // assignment statement |
1 method:
begin var n:integer; // variable declaration n:=1; // assignment statement |
2 method (canonical method when type is defined depending on the value):
begin var n:=1; // variable declaration and assignment statement => initialization |
Arithmetic operations and expressions
-
common method:
begin var a := 6; // Assigning value 6 a:= a + 2; // Increasing by 2 a:= a - 2; // substraction of 2 a:= a * 3; // Multiplication by 3 a:= a / 2; // division end. |
short method:
begin var a := 6; // Assigning value 6 a+= 2; // Increasing by 2 a-= 2; // substraction of 2 a*= 3; // Multiplication by 3 a/= 2; // division end. |
Data input
1-st way:
begin var n:integer; // n is a variable of integer type read(n); // input some value to store it in n variable |
begin var n:real; // n is a variable of real type - floating point number read(n);// input some value to store it in n variable |
2-nd way:
var n:=ReadInteger(); // n is a variable of integer type & we input some value to store it in n var x:=ReadReal(); // x is a variable of real type & we input some value to store it in x |
3-d way (tuple assignment):
var n1, n2: integer; // two integers are declared (n1, n2) := (1, 2); // 1 is assigned to n1, 2 is assigned to n2 |
4-th way:
var(n1, n2) := readInteger2; // n1 and n2 are the variables of int type & we input some values to store it in them |
Usually before data reading you must print the prompt with an explanation of what data you read:
var x := ReadInteger('Enter x:'); var y := ReadInteger('Enter y:'); var res := x + y; |
Data output
1-st way:
begin var n:integer; read(n); n: = n * n; writeln('n = ',n); |
2-nd way:
begin begin var n:integer; read(n); n: = n * n; print('n = ',n); |
For beautiful output, you should use formatted output with the WritelnFormat
procedure or Print
:
1. WritelnFormat:
begin var a:=1.2; var b:=4; var c:=a+b; WritelnFormat ('f ({0}, {1}) = {2}', a, b, c); end. |
The result will be:
f (1.2, 4) = 5.2
The first parameter in brackets and single quotes is a format string that specifies the format for outputting expressions.
So, if we want to output:
a + b = b + a = sum
then you just need to replace a
, b
with {0}
, {1}
:
WritelnFormat ('{0} + {1} = {1} + {0} = {2}', a, b, x + y) |
W
) of the output field of the expression N
(width in characters): {N,W}
. For example, the operatorWritelnFormat('x = *{0,5}*', x); // 5 means 5 charecters for displaying x |
works this way:
x = * 6* x = * -3* x = * 123* x = *-9876*
2. Print:
var x := ReadInteger('Enter x:'); var y := ReadInteger('Enter y:'); var res := x + y; Print($'Sum of {x} and {y} is {res}'); |
Swapping Variable Values
We have:
var (x,y) := (3,5); |
To do: To swap values of variables:
Solution 1. Using temporary variable:
var t := x; x := y; y := t; |
Solution 2. Using multiple assignment:
(x,y) := (y,x); |
x := y
and y := x
are carried out simultaneously! Notsequentially!
Labs and tasks
Follow the rules to make the tasks
- Save your files with names as it is given in tasks (e.g.
task-04.pas
). - Give meaningful names to your variables.
- Use comments to make the program clear.
- Give the task of the program as a comment before the program code. Use curly braces for comments:
- Give the results of your program (log) as a comment after the program code. It’s easy to do just by copying. Use curly brackets to add comments:
To do: Calculate the expression. The values of
x
, y
and z
are entered.
Expected output:
Input x 3 Input y 4 Input z 5 The result = 1.77800712886037
[Program name: L1sample1.pas]
✍ Algorithm (how to do):

{0.2}
Task 1:To do: Calculate an average of two variables
a
and b
(formula for calculation: a + b)/2
). Values of variables are provided (a=5
, b=6
). You should do this task twice with different ways of assigning and output.
Note: it is better to use here formatted output.
Expected output:
(5 + 6) / 2 = 5.5
[Program name: L1task00.pas and L1task01.pas]
{0.2}
Task 2:To do: Assign the values to two variables (
a=-0.80
, b=-8.0
). Calculate the sum, substruction, product and quotient of the variables’ squares.Note 1: To specify a particular number of digits after the floating point you can use format expression of
writeln
function:
writeln('result = ', x:5:3) 5 means total number of signs to output the number, 3 means the number of digits to output after floating point.
Note 2: it is better to use here formatted output.
Expected output:
a^2 + b^2 = 64.64 a^2 * b^2 = 40.96 a^2 - b^2 = -63.36 a^2 / b^2 = 0.01
[Program name: L1task02.pas]
To do: The side of a square (variable name is side
) is entered. Calculate its perimeter: P = 4·a
. Use different methods of assigning, input and output.
Expected output:
please enter the side length of a square: 5.6 Perimeter P = 22.4
[Program name: L1sample2.pas]
✍ Algorithm (how to do):
-
1-st way:
begin // Variable declaration to store the value of the side length var a := ReadReal('please enter the side length of a square:'); var P := 4 * a; // Perimeter calculation Writeln('Perimeter P = ', P); end. |
2-nd way:
begin Writeln('please enter the side length of a square:'); // Variable declaration to store the value of the side length var a: real; readln(a); var P := 4 * a; // Perimeter calculation Print('Perimeter P = ', P); end. |
{0.3}
Task 3:
To do: The side of the square (variable name is side
) is entered. Calculate an area of the square: S = a2
. You should use different methods of assigning, input and output.
Note: To calculate square of a number you can use sqr()
standart function, for example:
sqrX:=sqr(x); |
Expected output:
enter a side length of a square: 2.90 Area S = 8.41
[Program name: L1task03.pas]
{0.3}
Task 4:
To do: The sides of the rectangle are entered (a
and b
). Calculate an area of the rectangle (S = a*b
) and its perimeter (P = 2 (a + b)
).
Note: To specify a particular number of digits after the floating point you can use format expression of writeln
function:
writeln('S = ', S:0:2); // :2 means the number of digits to output after floating point |
Expected output:
Enter the values of two sides: 12 13 result: S = 156.00 P = 50.00
[Program name: L1task04.pas]
{0.4}
Task 5:
To do: A diameter of a circle (variable name is d
) is entered. Calculate its length (formula L = π·d
). The value of π
is 3.14. Use different methods of assigning, input and output.
Note 1: π
has a constant value. In pascalAbc we can declare constant before the begin
section of the program:
const pi = 3.14; begin // ... end. |
Note 2: Make the program using the same style of coding as in sample 2.
Expected output:
please enter a diameter of a circle: 6.7 the length of a circle is: 21.038
[Program name: L1task05.pas]
To do: Calculate hypotenuse and perimeter of a right-angled triangle; legs of the triangle are entered (square root of (a2 + b2)
).
Note: To calculate square root of a number you can use sqrt()
standart function, for example:
sqrtX:=sqrt(x); |
Expected output:
Input the values of triangle legs: 3.0 6.0 hypotenuse = 6.70820393249937 perimeter = 15.7082039324994
[Program name: L1sample3.pas]
✍ Algorithm:
-
Here is an example of right program which is clear for user:

{0.4}
Task 6:
To do: A length of a cube side is entered (a
). Calculate a volume of the cube (V = a3
) and its surface area (S = 6·a2
). Give the program log in the form of a comment.
Note: To specify a particular number of digits after the floating point you can use format expression of writeln
function:
writeln('V = ', v:5:3) 5 means total number of signs to output the number, 3 means the number of digits to output after floating point.
Expected output:
enter a cube side length: 9.000 V = 729.000 S = 486.000
[Program name: L1task06.pas]
{0.4}
Task 7:
To do: Assign a value to integer variable x
(x = 5). Calculate the value of the function:
y = 4(x-3)6 - 7(x-3)3 + 2
Note 1: To calculate the power of a number you can use the power(x:real, y:real)
function. For example:
//2 in the power of 5 = powNumb = power (2,5); |
Note 2: It is better to use an auxiliary variable for (x-3)3
.
Expected output:
for x = 5 we have y = 202
[Program name: L1task07.pas]
{0.4}
Task 8:
To do: Calculate a distance between two points with the given coordinates x1
and x2
on the number axis; the coordinates are entered. The formula is |x2 − x1|
.
Note: To calculate the absolute value of a number you can use abc(x:real)
standart function:
abs(x2 - x1); |
Expected output:
x1 = 3.2 x2 = 2.5 the distance between two points: 0.7 |
[Program name: L1task08.pas]
{0.4}
Task 9:
To do: Calculate a distance between two points on the plane; coordinates (x1
,y1
) and (x2
,y2
) are entered. The distance is calculated by the formula:
Note 1: Verify the correction of your program by using «simple» values that are easy to calculate. For example:
d((0, 0); (6, 0)) = 6; d((0, -4); (0, 1)) = 5; d((-1, 1); (2, 5)) = 5:
Note 2: Display the results of your program (log) in the form of a comment after the program code. It’s easy to do by copying and pasting. You should use curly brackets for comments:
Expected output:
enter x1 of the first point: 0 enter y1 of the first point: 0 enter x2 of the second point: 6 enter y2 of the second point: 0 The distance equals 6 |
[Program name: L1task09.pas]
{0.3}
Task 10:
To do: The temperature in Celsius is entered, convert temperature to Fahrenheit. Celsius and Fahrenheit scales are related by the ratio:
and opposite:
Expected output:
enter the temperature in Celsius 56 The temperature in Fahrenheit 132.8 |
[Program name: L1task10.pas]
Swapping Variable Values
{0.2}
Task 11:
To do: Swap the values of variables A
and B
and print out the new values to the console.
Expected output:
Enter A: 5.7 Enter B: 3 Result: A = 3, B = 5.7 |
[Program name: L1task11.pas]
{0.2}
Task 12:
To do: The values of variables A
, B
, C
are entered. Swap their values to make A = B
, B = C
, C = A
, and display the results.
Expected output:
A = 3.4 B = 2 C = 1.5 Result: A = 1.5, B = 3.4, C = 2 |
[Program name: L1task12.pas]