Contacts

Data entry operators ReadLn and Read. Topic: Input - Output. Read (Readln), Write (Writeln) statements. Simplest Linear Programs What is the Difference Between Readln and Read

The Readln routine 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 with 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); readln end.

In line # 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 into variables. After the word Var, we indicate the variable identifier (that is, its name, which we come up with ourselves), separated by a space. Variables are not service words, the programmer sets them himself. In this case, we have set one variable "n" and in the future we will use only this variable. After writing the variable, the data type is indicated with a colon. There are several types of data. One of them is Integer. It makes it clear to 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 some number in your program, then for it you need to specify a variable (in our case "n") and a data type (in our case Integer).

In line # 7, we write a statement for entering data from the keyboard Readln. This statement calls the built-in data entry routine and at this point the program stops and starts waiting for data entry. We have already used this operator for screen delay. In this program, after the Readln statement, our variable "n" is indicated in parentheses. The number that we will 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 will wait. At this stage, we must enter some number and press Enter.

Line number 8. This is where we write an output statement to the Writeln screen. It will display the message "You have entered a number", and will also display the value of our variable "n" (that is, the value that we enter from the keyboard). Note that in line # 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.

Run (Ctrl + F9). We type a number, for example, 5 and press Enter.

Topic: Input Output. Read (Readln), Write (Writeln) statements. The simplest linear programs.

Let's solve the problem by commenting each of our actions in curly braces. Recall 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 user-supplied numbers.

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

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

  • why was the program called Proizv2?
  • Why did you put the Crt module in the Uses section?
  • what is the purpose of the variables number1, number2, rezult?
  • what type are these variables? what does it mean?
  • If we assign the variables number1 and number2 to the values ​​5 and 7, respectively, what line will the computer produce when executing the last Write procedure? Write it down in your notebook.
  • in which lines the user is prompted for the values ​​of the variables?
  • in 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 outputs the product of three numbers.

Write and WriteLn Statements

We have already used the Write and WriteLn operators, but we need to dwell in more detail on the rules for using these operators.

Write is 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 display the result, but also to display various messages or requests. This allows you to conduct a dialogue with the user, tell him when he needs to enter values, when he gets a result, when he made a mistake, etc.

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

The WriteLn statement can 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 it for a better perception of data input.

Read and ReadLn Operators

Let's remember that the main purpose of a computer is to save human labor. Therefore, it is necessary to provide an opportunity, having once written a program, reuse it, each time entering different data. This flexibility in the language is provided by the Read and ReadLn statements. These operators enter information from the keyboard.

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

The Read procedure expects the values ​​listed in parentheses to be entered. The entered data must be separated from each other by spaces. The assignment of values ​​occurs in turn.

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

There are no special differences in 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 to delay: before pressing a key the result of the program execution remains on the screen. This is very useful for analyzing the results.

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

Let's solve a problem in which we will consider all possible uses of these procedures.

Task ... Find the average of three numbers.

Note ... To find the average of several numbers, 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 name of the Srednee program 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 are the same. Next comes the connection of the Crt module. In the Var section, First, Second, Third are described as variables of an integer type, and Sum is of a real type. The operator section begins with the standard ClrScr (Clear Screen) screen routine found in the Crt module. Then, with the Write statement, we display the message ‘Enter the first number’, after receiving which the user must enter the number.

Now the computer should read the entered characters and put them in the First variable, this will happen when the next ReadLn (First) statement is executed. Then, using the Write statement, 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 variable Sum. 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 declare 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 to the same Sum variable again. Now you can display the result of calculations on the screen using the Write procedure. Finally, the final ReadLn routine will delay our output to 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 mean of 5, 7 and 12 is 8.00

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

Choose with the teacher the 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 two lines later - your date of birth.
  4. Write a program for printing one of the shapes with asterisks:
    a) Christmas trees (several Christmas trees);
    b) snowflakes (several snowflakes);
    c) a house. For example,

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

  5. Compose your 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, a machine asks two questions, "What's your name?" how old are you?"; after entering the name (Anton) and the number (15) displays on the screen “Yes ... In 50 years you will 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 with a complete answer.
  8. Prompt the user for two numbers and display the result of an integer division and the remainder of an integer division in the form of a table. For example, when entering numbers 5 and 3, the screen should display the following table:

    **************************
    * 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 amount of the purchased product, determining and displaying the amount of money that the buyer must pay for the purchase.

You are in the section on Pascal programming. Before we start programming, we need to clarify some of the concepts that we will need at the beginning. You can't program just like that. We cannot write a program in words - the computer understands nothing but zeros and ones. For this, special symbols have been created in Pascal - the Pascal language, a set of reserved words that cannot be used in their programs anywhere else, except for their intended purpose. Let's list the basic concepts that we need at the beginning:

✎ 1) program - "program" in English, written at the very beginning of the code, followed by the name of the program in Latin letters and a semicolon. For instance: program Summa; - a program called Summa. But this part of the code, called the program header, need not be written - it is present only for clarity and shows what problem the given program solves. Here we have used the word "code" - this is the name of the textual record of the program.

✎ 2) integer - in English means "integer" (or just "integer") and is used in Pascal to denote 32-bit (8 bytes) signed integers with the range [-2147483648, 2147483647]. What these large numbers mean, we will analyze later.

✎ 3) real - from English “real”, “real”, “real”, “real”. In the Pascal language, this term denotes real numbers from the range [-1.8 ∙ 10 308, 1.8 ∙ 10 308]. These are very large numbers, but 15 - 16 significant digits are displayed. By the way, the integer and real data types in the PascalABC.Net programming environment are always automatically highlighted in blue.

✎ 4) const - analogue of English. "Constant" meaning "constant", "constant". In Pascal, it is a quantity that cannot be changed. It is written like this:


This entry should be perceived as it is written: the number N is 12, S is 5, "pi" is 3.14 (as in mathematics, only a dot is used instead of a comma in Pascal). In the last line, we used a double slash (two forward slashes) followed by the text - this is how comments are written in Pascal, and the program does not accept them. Everything that begins with a double slash and up to the end of the line is a comment that is written to explain the program and is always highlighted in a different color (in PascalABC.Net it is green, in Turbo Pascal this kind of comment is not used). There is another type of comment - this (text enclosed in curly braces, as well as here, also highlighted in green). This kind of comment can act several lines in a row - from the beginning of the parenthesis to its closing, and everything that is in the middle of such a construction is not perceived by the compiler as code and simply skipped.

In fact, the recording format const a little harder. According to the rules, we had to write:

1 2 3 4 const N: type integer;

Description:

")" onmouseout = "toolTip ()"> integer
= 12 ; // number N - integer type S: type integer;

Description:
Represents a 32-bit signed integer.

Value range: -2 147 483 648 .. 2 147 483 647")" onmouseout = "toolTip ()"> integer
= 5 ; // number S - integer type pi: type real;

Description:
Represents a double precision floating point number.

Size: 8 bytes
Number of significant digits: 15 - 16
Value range: -1.8 ∙ 10 308 .. 1.8 ∙ 10 308
")" onmouseout = "toolTip ()"> real
= 3.14 ; // number "pi" - real

After the declaration of each value, its type is indicated, and then a value is assigned. But the previous entry is also correct, since the Pascal compiler is configured so that it automatically detects the type of the constant. But the same cannot be said about the next type of numbers - variables.

✎ 5) var - comes from the English. "Variable" (or "volatile"), which in Pascal means a value that in the course of the program can change its value. It is written like this:


As you can see from the entry, there is no "=" sign - variables of the same type are recalculated (separated by commas) and only the type is indicated after the colon. Variables N, m (integers) and Q, r, t (real) in the program can change values ​​within integer and real, respectively. One more note: the description of variables always comes after the description of constants (constants) - first comes the const construction, and then var.

✎ 6) begin - translated from English means “to begin” and Pascal means the beginning of the main program in which commands (operators) are written. After the word begin no semicolon.

✎ 7) end - in English. "End", and in Pascal language means the same (end of the program). After the last word end there is always a point. We have highlighted the word "last" because the use of the construction begin - end possibly in one more case: these are the so-called operator brackets, which are used to combine several operations under one operator. But more on that later. Thus, the main program will look like this:

1 2 3 4 5 6 begin < оператор 1 > ; < оператор 2 > ; . . . . . . . < оператор N > ; end.

Here operators in the body of the program are different commands to the compiler.

✎ 8) write - in English means "to write". This operator displays the text placed in it, which is why it is called the output operator. The text placed in it is highlighted in blue and written like this:

Write ( "this text is displayed");

The message in brackets and quotes will be shown in the console window (just in brackets without quotes it is impossible). After executing this statement, we will see on the screen:

this text is displayed on the screen

In this form, the write operator is used in the case when it is necessary to show a hint, explanation, comment, etc. And from if it is also necessary to display a numerical value, say, S = 50 sq. m, then the format is used:

Write (, S);

As a result, we get the result on the screen:

The size of the area is: S = 50

And if it is necessary to display the units of measurement, you need to insert the text in quotes after S again:

Write ( "The area is equal to: S =", S, "sq. M");

After executing the last output statement, we get the output to the screen:

The size of the area is: S = 50 sq.m

✎ 9) writeln is the same as write, but after execution the cursor will be moved to the next line.

✎ 10) read - translated from English means "read", therefore read is called the operator of reading, or data input. It is written as read (N), which means that you need to enter the value N, where N is any number, or text, or any other type of variable. For example, if you need to enter the age of a person who is 32 years old, we can write it like this:


In the first line of this code, the program displays the question “ What is your age?"And moves the cursor to the next line (ending with ln); on the second line, we print "Year =" (at the beginning with a space); then we see the readln (Year) operator, which means the need to enter the age Year (number 32); Finally, we display the messages "My Age", "32" and "Years." "One by one. You need to keep a close eye on the spaces. As a result of executing this code, we will receive the message:

What is your age?
Year = 32
My age is 32

✎ 11) readln - the same as read, only with newline. Indeed, in the above example, after entering the number Year, we only write in the next line: “ My age is 32».

That's all for now. On the next page we will write the first program, and in Pascal programming these will be our

For typed files, reads the file component into a variable.

For text files, reads one or more values ​​into one or more variables

Notes:

For string variables:

Read reads all characters up to (but not including) the next end-of-line marker or until Eof (F) is True. Read does not move to the next line after reading. If the resulting string is longer than the maximum length of the string variable, then it is truncated. After the first Read, each subsequent Read calls will see the end-of-line marker and return a zero-length string.

Use multiple calls to ReadLn to read multiple string values.

When the Extended Syntax option is enabled, the Read procedure can read null-terminated strings into null-based character arrays.

For variables of type Integer or Real:

Read will skip any spaces, tabs, or end-of-line markers preceding a numeric line. If the numeric string is not
matches the expected format, an I / O error occurs, otherwise the variable is assigned the resulting value. The next Read will begin with a space, tab, or end-of-line marker that terminated the numeric string.

Example Read Procedure

Uses WinCrt, WinDos;
Var F: Text;
Ch: Char;
Begin
(Get the filename from the command line)
Assign (F, ParamStr (1));
Reset (F);
While Not EOF (F) Do
Begin
Read(F, Ch);
Write (Ch); (We display the contents of the file on the screen)
End;
End.

  • Readln
  • Write
  • Writeln

The read instruction is intended for entering the values ​​of variables (initial data) from the keyboard. In general, the instruction looks like this:

read (variable !, variable2, ... variable

Here are examples of writing a read statement:

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 the key is pressed .

2. After pressing the key the entered value is assigned to the variable named in the statement.

For example, as a result of executing the instruction

Read (Tempérât);

and typing line 21, the value of Tempérât is 21.

One read statement allows you to get the values ​​of several 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 read (a, b, c); and keyboard input string:

4.5 23 0.17

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

If the line contains more numbers than the variables specified in the read statement, then the rest 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

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

The readln instruction differs from the read instruction in that after selecting the next number from the line entered from the keyboard and assigning it to the last variable from the list of the readln instruction, the rest 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 typing the string

10 25 18

the variables will receive the following values: a = 10, b = 25. After that, 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 the program for calculating the cost of a purchase might look like:

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

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



Did you like the article? Share it