Be sure to read the requirements for labs.

[`task-01.pas` ] Examine the example, create a file with this program and run it.

// The program that outputts the greeting - this is a comment to the program
begin
  Writeln('Hello world');
end.

Contents:


Definition of variable

A variable is a memory location  characterized by its name (identifier), data type, and stored value.

  • In Pascalabc.net variables may be defined within the body of the program between begin and end.  The principle of locality: a variable is defined immediately before its use.
  • When we define a variable, we specify its name and type:

So we have two possible approaches:

1 approach:

begin
  var n:integer; // variable declaration
  n:=1; // assignment statement
2 approach (canonical method when type is defined depending on the value):

begin
  var n:=1;

Arithmetic operations and expressions

common method:

begin
  var a := 6; // Assigning value 6
  a:= a + 2; // Increase by 2
  a:= a - 2; // Reduction of 2
  a:= a * 3; // Multiplication by 3
  a:= a / 2; // division
end.

short form:

begin
  var a := 6; // Assigning value 6
  a+= 2; // Increase by 2
  a-= 2; // Reduction of 2
  a*= 3; // Multiplication by 3
  a/= 2; // division
end.

Priority :

  1. brackets
  2. multiplication and division
  3. addition and substruction

Standard functions

  • abs(x)  — absolute number
  • sqrt(x) — square root
  • sin(x)  —    sine in radian
  • cos(x)  — cosine in radian
  • exp(x)  — exponent ех
  • ln(x)   — natural logarithm
  • power(x,y) — x^y
  • floor(x) — rounding «down»
  • ceil(x)  — rounding «up»

Data input

1st way:

begin
  var n:integer;
  read(n);

begin
  var n:real;
  read(n);

2nd way:

var n:=ReadInteger(); // n is a variable of integer type & we input some value to store it in n
var n:=ReadReal(); // x is a variable of real type & we input some value to store it in x
3d 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
4th 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 integer x:');
var y := ReadInteger('Enter integer y:');
var res := x + y;

Data output:

1st way:

begin
  var n:integer;
  read(n);
  n: = n * n;
  writeln('n = ',n);
2d way:
begin
    var n:integer;
    read(n);
    n: = n * n;
    print('n = ',n);

What does formatted output mean?

For beautiful output, you should use formatted output with the Print procedure or WritelnFormat :

1. Print:
var x := ReadInteger('Enter x:');
var y := ReadInteger('Enter y:');
var res := x + y;
Print($'Sum of {x} and {y} is {res}');

Expected output:

Enter x: 2
Enter y: 5
Sum of 2 and 5 is 7
2. WritelnFormat:

begin
  var a:=1.2;
  var b:=4;
  var c:=a+b;
  WritelnFormat ('f ({0}, {1}) = {2}', a, b, c); 
end.

Expected output:

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 ab with {0}{1}:

WritelnFormat ('{0} + {1} = {1} + {0} = {2}', a, b, x + y)
  • You can specify the width (W) of the output field of the expression N (width in characters): {N,W}. For example, the operator
WritelnFormat('x = *{0,5}*', x); // 5 means 5 charecters for displaying x

works this way:

x = *    6*
x = *   -3*
x = *  123*
x = *-9876*

Swapping Variable Values

We have:

var (x,y) := (3,5);


To do: To swap values of the variables:
Solution 1. Using temporary variable:

var t := x;
x := y;
y := t;

Solution 2. Using multiple assignment:

(x,y) := (y,x);
Two assignments x := y and y := x are carried out simultaneously! Not sequentially!