The read and readln operators in Pascal. Using Var, Integer, Readln. What is the readLn instruction?

The Pascal programming language uses instructions such as read and readLn. What are they?

What is a read statement?

This instruction is intended to provide input of various variable values ​​from the PC keyboard when using the Pascal language. The scheme for using the instruction in question looks simple: like read (“variable value”).

In practice, the read instruction is used to ensure that certain data is read from a file and the subsequent assignment of values ​​extracted from the corresponding data to the variables that are specified when calling the procedure.

If the user makes a mistake when entering data; it does not correspond to any type of variable reflected in the instructions, the program stops executing commands. At the same time, a message appears on the PC screen stating that an error has occurred in the application.

If the programmer uses several read instructions, then the data will somehow be entered on one line. A transition to the next one is possible only if the current line ends. However, you can read information located in another line using the readLn instruction. Let's take a closer look at its features.

What is the readLn instruction?

The essence of the readLn instruction is to set a condition in the program under which:

  • any number entered into the line is assigned to the last variable according to the instruction;
  • the remaining area of ​​the line is not processed by the program, while the next instruction will require new input.

So, you can enter the instruction:

readLn(C,D); read(E);

And if after this you enter the series 1 2 3 from the keyboard, then the variable C will acquire the value 1, D - 2. But the program will not assign a specific value to the variable E until the user enters a new number.

As with the read instruction, if the user enters an incorrect data type using the readLn command, the program exits and displays a message indicating that an error occurred.

Comparison

The main difference between readLn and read is that the first procedure involves the program moving to the line of the file following the one in which the instructions are written. The second procedure allows the program to read the data located in the next line only with the user's permission - if he presses Enter.

In practice, the readLn instruction is most often used to provide a delay between the result of an application's execution and the transition to the next instruction. The corresponding delay lasts until the user presses Enter.

Having determined what the difference is between readLn and read in the Pascal language, we record the conclusions in the table.

Author Alexander asked a question in the section Other languages ​​and technologies

Why does Pascal have read and readln if read also translates a line, although in theory it shouldn’t? and got the best answer

Answer from Skipy _[guru]
readln - takes a value from the input buffer into a parameter and clears the entire keyboard input buffer
and read - takes the value from the input buffer into the parameter and does not clear it into the buffer, but leaves the value!! !
Automatic line feed when typing - everywhere
i.e. readln is safe input
read - unsafe input
example:
var
a,b: integer;
begin
read(a); (user entered: 4, 5 a = 4)
(in the input buffer the number is 5!!}
read(b); (here he entered the number: 6, but b = 5)
(the number 6 remains in the input buffer!!}
---
readln(a);(user entered: 4, 5; a = 4)
(the input buffer is clear!!}
readln(a);(user entered number 6 and b = 6)
(the input buffer is clear!!}
readln(b);
end.
On the contrary, there is no automatic determination of the type of the input value; everything is determined by the conventions in the format specifier.

Answer from Yoali-Mali[guru]
It can be explained more simply, without too much confusion:
When the read procedure is executed, the value of the next data is read from the same line, and when the readln procedure is executed, the value of the next data is read from a new line.
Therefore, the operator Readln (b1,b2,...bn); provides data entry into the COLUMN.
After entering each variable b1, b2, ..bn, the cursor jumps to the beginning of a new line.
Operator Read(b1,b2,...bn); provides data entry into a LINE


Answer from VT-107 FIT[guru]
Here Dmitry got confused about something, but it’s not right. And about a completely purified and not purified buffer and about safety. Sali also answered incorrectly. There are no columns and rows, completely different rules.
Firstly, these procedures do not only work with console input, so two functions are needed.
procedure Read(F, V1 [, V2,...Vn ]);
reads all V1..Vn from the stream (exactly n parameters), all read data will be deleted. Everything that comes after this data will remain unchanged.
When reading a line, everything up to the newline or end of file will be read. Subsequent calls to read will return an empty string. And the newline character will not be removed from the stream.
If you read char, then depending on the settings, the newline character may be read as char(26)
When reading numbers, all spaces, tabs, and line breaks are skipped. And the next reading will begin with the symbol coming after the read number. Again, unnecessary characters may be skipped.
procedure ReadLn([ var F: Text; ] V1 [, V2, ..Vn ]);
reads from the stream V1, Vn (that is, it works like read) and then skips all characters up to and including the new line. All characters after a line break will remain unchanged.
The transition to a new line occurs because you press Enter in the console.
It's far-fetched about safety. These are functions with different purposes. Any incorrect use is not safe, any correct use is correspondingly safe.

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 the screen:

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

In a programme:

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 the 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

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), after displaying the desired message on the screen, it additionally moves the cursor to the next line.

General form:
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 form:
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 ​​occurs 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 determine 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?” 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. Prompt 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.
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);
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