• What is readln in Pascal. Using Var, Integer, Readln. Arithmetic operations with integers and integer variables in Pascal

    Subject: Input - output. Operators Read (Readln), Write (Writeln). The simplest linear programs.

    Let's solve the problem by commenting on each of our actions in curly brackets. Let us remind you that the comment is not perceived by the computer, but we need it in order to better understand how the program works.

    Task . Write a program that clears the screen and calculates the product of two numbers entered by the user.

    ProgramProizv2;
    Uses
    Crt; (Connect the Crt module)
    Var
    number1, (variable that will contain the first number)
    number2, (variable that will contain the second number)
    result (variable that will contain the result)
    :integer;
    Begin
    ClrScr;(We use the screen clearing procedure from the Crt module)
    Write("Enter the first number");
    Readln(number1);
    (The number entered by the user is read into the number1 variable)
    Write("Enter the second number");
    (We display the characters written between apostrophes)
    Readln(number2);
    (The number entered by the user is read into the number2 variable)
    result:= number1 * number2;
    (We find the product of the entered numbers and assign it to the rezult variable)
    Write("The product of numbers ", number1, " and ", number2, " equals ", result);
    (We display a line containing the answer to the problem)
    Readln;(Screen delay procedure)
    End.

    To better understand the operation of the program, type it on your computer and test its operation. Answer the questions:

    • why was the program called Proizv2?
    • Why was the Crt module placed in the Uses section?
    • what is the purpose of the variables number1, number2, result?
    • what type are these variables? what does it mean?
    • If you assign the variables number1 and number2 the values ​​5 and 7, respectively, then what line will the computer produce when executing the last Write procedure? Write it down in your notebook.
    • In what lines are the user asked for variable values?
    • Which line does the multiplication of numbers occur?
    • what does the assignment operator do in this program?

    Exercise . Modify the program so that it prompts the user for another variable and prints the result of the product of three numbers.

    Write and WriteLn Operators

    We have already used the Write and WriteLn operators, but we need to take a closer look at the rules for using these operators.

    Write (English: write) - an operator that is used to display information on the screen. The WriteLn operator performs the same action, but since it also has the ending Ln (line - English line, line), then after displaying the desired message on the screen, it additionally moves the cursor to the next line.

    General view:
    Write (list of expressions)
    WriteLn (list of expressions)

    The Write and WriteLn procedures are used not only to output the result, but also to output various messages or queries. This allows you to conduct a dialogue with the user, inform him when he needs to enter values, when he receives a result, when he made a mistake, etc.

    For example, when executing the procedure WriteLn('Found number ',a), the line enclosed in apostrophes will be printed, and then the value of the variable a will be printed.

    The WriteLn operator can also be used without parameters. In this case, a line consisting of spaces will be printed and the cursor will be moved to another line. Sometimes we need this for better perception of data input.

    Read and ReadLn Operators

    Let us remember that the main purpose of a computer is to save human labor. Therefore, it is necessary to ensure that, once a program has been written, it can be used repeatedly, entering different data each time. This flexibility in the language is provided by the Read and ReadLn operators. These operators enter information from the keyboard.

    General view:
    Read(variable, variable...)
    ReadLn(variable, variable...)

    When the Read procedure is executed, the values ​​listed in parentheses are expected to be entered. The entered data must be separated from each other by spaces. Assignment of values ​​proceeds in turn.

    For example, if the values ​​53 and X are entered, then when the Read(a,b) operator is executed, variable a will be assigned the number 53, and variable X will be assigned the letter X. Moreover, we note that in order to avoid an emergency situation, you need to correctly define the data type in the section Var; in our case a:integer, and b:char.

    There are no particular differences when reading and writing in the use of the Read and ReadLn operators. Often the ReadLn procedure without parameters is used at the end of the program for a delay: until the key is pressed the result of the program execution remains on the screen. This is very useful to do for analyzing the results.

    Note . When you set the screen delay, pay attention to the previous input. If the data was requested by the Read procedure, there will be no delay.

    Let us solve a problem in which we consider all possible uses of these procedures.

    Task . Find the average of three numbers.

    Note . To find the average of several numbers, you need to add these numbers and divide the sum by the number of these numbers.

    Type the text of the problem and carefully consider each line. The program name Srednee reflects the content of the task. By the way, let's agree that the name of the program and the name of the file that contains this program coincide. Next comes the connection of the Crt module. The Var section describes First, Second, Third as variables of integer type, and Sum as real type. The operator section begins with the standard ClrScr (Clear Screen) procedure, which is located in the Crt module. Next, using the Write operator, we display the message ‘Enter the first number’, upon receiving which the user must enter the number.

    Now the computer must read the entered characters and store them in the First variable, this will happen when executing the next ReadLn(First) statement. Then, using the Write operator, we request the values ​​of two more numbers and read them into the Second and Third variables. Then we calculate their sum and assign the resulting number to the Sum variable. To find the average, you now need to divide the resulting number by 3 and store the result in some variable.

    It is not at all necessary to describe another variable to save the result. You can, as in our program, divide the value of the Sum variable by 3 and assign the result again to the same Sum variable. Now you can display the result of calculations on the screen using the Write procedure. And finally, the last ReadLn procedure will delay our output on the screen until a key is pressed.

    Press the keys +. Enter the values ​​of variables 5, 7 and 12, you will see the following on the screen:

    The average of 5, 7 and 12 is 8.00

    Look carefully at this line and compare it with the result output line in our program. Test the program a few more times for different variable values.

    With your teacher, choose problems to solve from the following list:

    1. Enter two numbers a and b. Use the assignment operator to exchange their values:
      a) using an intermediate variable (x:=a; a:=b; b:=x);
      b) without using an intermediate variable (a:=a-b; b:=a+b; a:=b-a).
    2. Write a program that asks the user for an integer, a real number, an arbitrary character, and a string, and then prints everything on one line.
    3. Display your last name, first name and patronymic, and after two lines - your date of birth.
    4. Write a program to print one of the shapes with stars:
      a) Christmas trees (several Christmas trees);
      b) snowflakes (several snowflakes);
      c) a house. For example,

      *
      * *
      * *
      ***********
      * *
      * *
      * *
      * *
      ***********

    5. Create your own business card.


      * Ivanov Sergey *
      * Proletarskaya 74 sq. 55*
      * Phone 45-72-88 *
      *******************************

    6. Compose a dialogue between the user and the computer on an arbitrary topic.
      For example, the machine asks two questions: “What is your name?” and “How old are you?”; after entering the name (Anton) and number (15), it displays “Yes... In 50 years you will already be 65 years old, and your name will not be Anton, but grandfather Anton”
    7. Ask the user for two numbers and display the result of the sum, difference, product and quotient of these numbers as a complete answer.
    8. Ask the user for two numbers and display the result of integer division and the remainder of the integer division in the form of a table. For example, when entering the numbers 5 and 3, the following table should appear on the screen:

      **************************
      *X*Y*div*mod*
      **************************
      * 5 * 3 * 1 * 2 *
      **************************

    9. Write a program that asks for the name of an animal and a number, and then displays a phrase like “A squirrel will eat 10 mushrooms” (when you enter the word “squirrel” and the number 10).
    10. Organize a dialogue between the seller (computer) and the buyer (user) when purchasing any product according to the following scheme: offering the product at a certain price, requesting the quantity of the product being purchased, determining and displaying the amount of money that the buyer must pay for the purchase.

    The Readln procedure is used for more than just screen delay. Its main task is to enter data from the keyboard. In this article we will learn how to enter numbers from the keyboard and then display them on the screen. To do this, we will need to get acquainted with the section for describing Var variables, as well as one of the data types used in Pascal.

    Program number3; uses crt; var n:integer; begin clrscr; write('Enter a number from the keyboard:'); readln(n); writeln('You entered a number',n); readlnend.

    In line No. 3 we write the service word Var. It is used to declare variables. Variables are different values, numbers or words that can change during program execution. When we enter numbers or letters from the keyboard, they are written to variables. After the word Var, we indicate the variable identifier separated by a space (that is, its name, which we come up with ourselves). Variables are not function words; the programmer sets them himself. In this case, we have set one variable “n” and in the future we will only use this variable. After writing a variable, the data type is indicated by a colon. There are several data types. One of them is Integer. It tells the program that our variable "n" can only be an integer ranging from -32768 to 32767. The use of different data types depends on the specific needs of the programmer when writing the program. The most important thing at this stage is to understand that if you are going to use a number in your program, then you need to specify a variable for it (in our case, “n”) and a data type (in our case, Integer).

    In line No. 7 we write the operator for entering data from the Readln keyboard. This statement calls the built-in data entry procedure and at this stage the program stops and begins to wait for data input. We have already used this operator to delay the screen. In this program, after the Readln operator, our variable “n” is indicated in parentheses. The number that we enter from the keyboard will be written to this variable. Accordingly, this number must correspond to the parameters of the variable, i.e. must be an integer in the range -32768 to 32767. After the program reaches the 7th line, it will display the message “Enter a number from the keyboard: ” and wait. At this stage we must enter some number and press Enter.

    Line No. 8. Here we write the Writeln screen output statement. It will display the message “You have entered a number”, and will also display the value of our variable “n” (i.e. the value that we enter from the keyboard). Please note that in line No. 8 we put a comma before the variable “n”, and the variable itself is not enclosed in apostrophes.

    Now let's type the program in Pascal.

    Launch (Ctrl+F9). Type a number, for example, 5 and press Enter.

    The read instruction is intended for entering variable values ​​(initial data) from the keyboard. In general, the instructions look like this:

    read(Variable!, Variable2, ... VariableW where variables is the name of the variable whose value must be entered from the keyboard during program execution.

    Here are examples of writing a read instruction:

    Read(a); read(Cena,Kol);

    When the read statement is executed, the following happens:

    1. The program pauses its work and waits until the required data is typed on the keyboard and a key is pressed .

    2. After pressing the key the entered value is assigned to the variable whose name is specified in the statement.

    For example, as a result of executing the instruction

    Read(Tempérât);

    and entering line 21 from the keyboard, the value of the Tempérât variable will be the number 21.

    A single read statement can retrieve the values ​​of multiple variables. In this case, the entered numbers must be typed on one line and separated by spaces. For example, if the type of variables a, b and c is real, then as a result of executing the instruction read (a, b, c); and enter the line from the keyboard:

    4.5 23 0.17

    the variables will have the following values: a = 4.5; b = 23, o; c = 0.17.

    If there are more numbers on a line than there are variables specified in the read statement, then the remainder of the line will be processed by the next read statement. For example, as a result of executing the instructions:

    Read (a, B) ; read(C);

    and keyboard input string

    10 25 18

    the variables will receive the following values: a = 10, b = 25. Read instruction (C); will assign the value 18 to variable c.

    The readln instruction differs from the read instruction in that after selecting the next number from the string entered from the keyboard and assigning it to the last variable from the list of the readln instruction, the remainder of the line is lost, and the next read or readln instruction will require new input.

    For example, as a result of executing the instruction:

    Readln (a, B) ; read(C);

    and entering the line from the keyboard

    10 25 18

    the variables will receive the following values: a = ω, b = 25. After which the program will wait for a new number to be entered in order to assign it to the variable c.

    Each read or readln instruction should be preceded by a write instruction to tell the user what data the program expects from him. For example, a fragment of a program for calculating the cost of a purchase may look like:

    Writeln("Enter the initial data."); write("Product price:"); readln(Sepa); write("Quantity in batch:"); readln(Kol); write("Discount:"); readln(Skidka);

    If the type of data entered from the keyboard does not match or cannot be cast to the type of the variables whose names are specified in the read (readln) statement, then the program crashes (instructions following read are not executed) and a message about error.

    Read And ReadLn read information from the standard input device. In console applications, this device can be, for example, a keyboard (more precisely, data entered from the keyboard); in graphical applications, it can be a file on disk.

    That is, these procedures are “antipodes” - they perform actions opposite to them.

    The Read and ReadLn procedures perform similar actions. The main difference between them is the following: the ReadLn procedure, after completing the input, performs a line feed (and in the case of files, reads the file line by line). And the Read procedure reads data in a row - without a line feed.

    NOTE:

    I don’t remember this in Turbo Pascal (maybe I just forgot), but keyboard input can only be performed using the ReadLn procedure, and for some reason the Read procedure does not work.

    Syntax for console output:

    procedure Read(Args: Arguments);

    Syntax for output to file:

    procedure Read( var F:Text; Args: Arguments);

    Arguments ( Arguments) may be different. If several variables are used, they are listed separated by commas. For example:

    Var x, y: integer; z:real; str:string; begin WriteLn("Enter three space-separated integers:"); ReadLn(x, y, z); WriteLn("You entered: ", x, ", ", y, ", ", z:0:2); ReadLn(str); WriteLn(str + str); ReadLn; end.

    As already mentioned, when entered from the console, these variables can be of different types. But, unlike the Write/WriteLn procedures, it is not allowed to use (and this is logical))).

    IMPORTANT!
    When entering data, be aware that if the value entered by the user is of a different type than the type of the variable into which this value is entered, a run-time error will occur. If, for example, in the example above, the user enters a real value (such as 3.14) as the first number, the program will crash because the variable x is of integer type.

    When reading from a file, you can work with both typed and text files.

    If F(see syntax) is a typed file, then variables passed as parameters (Args) must have the same type as specified for the file F. Untyped files are not allowed. If the parameter F is not specified, it is assumed that reading is performed from the standard input device.

    If the file F has type Text, then the variables must be of type , or .

    If, when reading a file, there is no data available for reading, then an empty value is returned to the variable F (0 for , an empty string for strings).

    When using the ReadLn procedure, that is, when reading data line by line, the end of the line is indicated by a certain sequence of characters (which ones exactly depend on the operating system, for DOS/Windows these are two characters - #10 and #13).

    The end-of-line marker is not part of the line read and is ignored.

    If an error occurs during the Read/ReadLn procedure, a run-time error is generated. This behavior is not always acceptable (for example, while reading a file). Therefore, in some cases, error generation is disabled. This can be done using .

    NOTE:

    In various debugging and training programs, the ReadLn procedure is often used to prevent the console application from automatically closing after execution. To do this, at the end of the program simply write (as in the example above):

    That is, just the name of the procedure without parameters. In this case, the program will wait for the ENTER key to be pressed. Therefore, the program will not end until the ENTER key is pressed, allowing you to see the result of the program. Of course, in the operating system

    Just like for information output operators, the read and reeadln operators are operators for accessing built-in information entry procedures.

    The operators read (read) and readln, which comes from two English words read (read) and line (line), are used in programs to enter information into computer memory and " reading" values ​​into a variable.

    Let's consider the work of these operators and information entry procedures.

    Our program has a procedure readln(a). When executing a program, encountering a readln statement, the computer will pause while waiting for input. After we enter the value of variable a - 16 from the keyboard, the computer will assign this value to variable a, i.e. will send it to a memory location named a and continue executing the program. We call this process " reading" values ​​into a variable.

    So, the read and readln procedures “read” the values ​​of variables and assign them to the variables that are written to them.

    There can be several such variables, then they are written in these operators separated by commas, for example:

    read(a, b, c, n, g, j, i), readln(e, f, k, p, d), etc.

    What is the difference between the work of the read and readln procedures?

    The read procedure will require input or output of information on one line after itself, and the readln procedure allows you to enter and output information after itself from the beginning of a new line.

    For example:

    In the program: write("Enter the values ​​of a and b "); read(a, b);

    write("Enter information on one line");

    When this part of the program is executed, everything written in the first write statement will be displayed on the screen, then the cursor will be located on the same line, and the computer will wait for the values ​​a and b to be entered. Let's enter their values ​​- 2 and 3, separating them with a space or, in other words, separated by a space. After this, the same line will display the information written in the next write statement.

    On screen:

    Enter the values ​​of a and b 2 3 Enter information on one line

    In the program:

    writeln("Enter the values ​​of a, b and c); readln(a, b, c);

    writeln("Input and output information from the beginning of the line");

    On screen:

    Enter the values ​​of a, b and c

    Entering and outputting information from the beginning of the line

    Arithmetic operations with integers. Integer type variables. Real type

    The Pascal language uses integers, which include all natural numbers formed in the process of counting objects: 1, 2, 3, 4, 5, 6, ...; negative numbers: ..., -6, -5, -4, -3, -2, -1 and the number zero: 0. Integers form the following series:

    6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, ...

    Pascal allows a range of integers from -32768 to 32767.

    Variables, taking integer values, are written in the description section indicating the type integer (integer).

    For example: var a, b, c, a1, b34, nomb: integer;

    Values ​​of a different type for these variables in the same program cannot be assigned.

    Arithmetic operations with integers and integer variables in Pascal

    The "_" sign means a space. Spaces between variable names and the operation name (div) are required. (Derived from the English division - division).

    The remainder when a is divided by b. a_mod_b