VARIABLES. OUTPUT FORMATS


Variables
A computer would not be needed if it did not have the ability to store various information in its memory and be able to process information of the same type using the same algorithms. 
In order to create more interesting programs, one must learn how to store information in the computer's memory. At the same time, we need to learn how to somehow access the memory cells of the computer. 
In programming, as in life, in order to refer to any part of the computer's memory, it occurs by name. Using this name, you can both read information and write it there.
 
A variable is a location in computer memory that has a name and stores some value corresponding to type.

The word "variable" tells us that its value can change during program execution.  When a new variable value is saved, the old one is erased.


For a computer, all information is data in its memory - sets of zeros and ones (to put it simply, any information in a computer is just numbers, and it processes them in the same way). However, we know that integers and fractional numbers work differently. Therefore, each programming language has different types of data, which are processed using different methods.

For example,
integer variables – type integer (from English integer – whole), occupy 2 bytes in memory;
real variables that can have a fractional part (type real – from English real numbers - real numbers), occupy 6 bytes in memory;< br /> - characters (type char – from English character – symbol), occupy 1 byte in memory.

Let's try to add some variable to our program.
Before using a variable, you need to tell the computer to allocate space in memory for it. To do this, you need to declare a variable, that is, specify what type of value it will store, and give it a name. To do this, at the beginning of the program you need to write:

var <comma-separated variable names>: <the type of these variables>;
       <names of variables of another type separated by commas>: <the type of these variables>; 

 
Example
var a, b: integer; // declared two variables a and b of integer type. Until we initialize them so we don't know what's in memory. begin a := 6; // variable a was assigned the value 6 end.

Display
Let's try to write a calculator for prime numbers. Our task is to display some arithmetic expression on the screen and make the computer calculate it.
For example: 
5+7=12
Moreover, instead of 5 and 7, there can be different numbers, depending on the values ​​of the variables a and b in the program.
In the output statement, you can display not only text, but also the values ​​of variables, as well as the result of an arithmetic expression. Moreover, the output sequence may be different. For example, in order to display the above expression, you need to write this: writeln(a, '+', b, '=', a + b); If we want to display the value of a variable, then we just need to specify its name without quotes. If we want to display the result of an arithmetic expression, then it is enough just to write the arithmetic expression correctly.

Special attention should be given to the operation of division of integer numeric data types. In Pascal, two division operations are allowed, which are respectively denoted by  '/' and div< /strong>. You need to know that the result of dividing '/' is not an integer, but a real number (this is true even if you divide 8 by 2, i.e. 8/2=4.0). Division div – this integer division, i.e. result type is integer (i.e. 8 div 4 = 4).

Pay attention!
Variables, text and arithmetic expressions are separated from each other by a comma.


Be sure to do the exercises, so you can quickly consolidate the knowledge gained in practice!

Enter statement
In order for the user to be able to set the value of the variable himself, it is necessary to be able to enter values ​​from the keyboard. 
The input operator is written like this:
 
read(a);

After this statement, the data that is entered from the keyboard is stored in a certain variable.
You can also combine several read() statements into one.
For example, the entry read(a); read(b); performs the same actions as writing read(a, b); that is, the first entered data is entered into the variable a, the second into the variable b.

*** in-depth material: for those interested in the Pascal language ***
Additional output specifiers allow you to control the output of numbers in certain formats.

1. For integer data (integer etc.)  ;- the format is set by one number, which determines the number of positions assigned to this number.
Example:
Writeln(i:5);
    Displays the value of the i variable using 5 positions for this (even if the number occupies less than 5 positions, free positions will still be displayed).
    If the variable i:=34, then 3 empty positions will be displayed on the screen (they are shown with an underscore for clarity) and the number 34, i.e. only 5 positions.

_ _ _ 34
    If the variable i:=2345666, then this format (i:5) cannot be used (the displayed number takes more positions) and will simply be ignored and the entire value of the variable will be displayed on the screen.

2345666


2. For real data (real etc.) - the format is specified either by a single number that determines the number of positions assigned to this number in exponential form; or two numbers, the first of which indicates the total size of the field reserved for this number, the second - the number of decimal places, i.e. precision.
Example.
Writeln(p:12);    or      Writeln(p:6:2);
    If the variable p:=1234.6789, then in the first case, the screen will display

_1.2346E+004, and in the second 1234.68
Example.
Let the value of the X variable be 123.45678, then the operator
         Write(X);       ;   will output         "_1.23456780000000000E+02"
         Write(X:8:2);    will output        "_ _123.46"
         Write(X:10:5);  will output        "_123.45678"
         Write(X:10);     will output        "_1.23E+002"
         Write(X:8);       will output        "_1.2E+02"
         Write(X:1);       will output     ;    "_1.2E+002"



Let's try to use this information in practice.