The assignment operator in Pascal: what it is intended for, what actions it performs

Turbo Pascal is a simple programming language, but learning it is time consuming if you take this issue seriously. A novice user learns the basics of code from the simplest. So, for example, he first learns what actions the assignment operator performs, why it is needed, how to display a string or variables. Only then will the programmer turn to procedures and functions, work with character and string data types, files, and graphics. So what is an assignment operator? What is its role in writing code?

assignment operator

Organization of the Turbo Pascal application working window

Before starting to study the theoretical material, which actually includes the question of what is an assignment operator, you need to look around in the Turbo Pascal workspace.

The application menu consists of the following sections:

  • File - designed to perform basic commands with files (create, open, close, save, etc.).
  • Edit - this includes commands for working with text editing (copy, paste, cut, etc.).
  • Search - required to search and replace text throughout the text of the program.
  • Run - designed to run ready-made code, including for a step-by-step demonstration of work.
  • Compile - compiles the code.
  • Debug - necessary to facilitate the search for errors in the text of the program.
  • Tools - allows you to take advantage of some additional features of the application.
  • Options - sets the necessary options for working with the compiler and programming environment.
  • Window - required to perform operations with windows.
  • Help - required to find the answer to the question.

Now the Pascal ABC application is gaining more and more popularity . From the point of view of some users, it is much more convenient to work in this environment.

Turbo Pascal Elements

pascal assignment operator

In each programming language, the code structure is different, but there are common points. So, in Pascal, the design of the entire task will look like this:

Program primer; // program title

Uses crt; // List of used modules

Label metka1; // Label Description

Const number = 10; // Declare a constant

Type newtype = set of string; // Description of variable types

Var a: integer; b: real; c: newtype; d: boolean; e: char; // Declare variables

----------------------------------- // Description of procedures and functions

Begin

{program body} // Operator section

End.

The assignment operator takes its place of honor in the body of the program.

A brief digression into programming data types

Not only the assignment operator in Pascal raises questions from users. Data types also often create deadlocks.

All data in Turbo Pascal is divided into two categories: variables and constants. They can belong to one of the following types:

  • integer;
  • material;
  • symbolic;
  • string;
  • typed;
  • logical.

Also constants can be reserved.

Variables are described in program code as follows: var <variable name>: data type. A constant is declared like this: const <constant name> = value.

In the program code, you do not need to put the brackets <>.

The main operators of the Turbo Pascal language

The operator is a construct that shows what actions the program should perform. In writing code, in particular in Turbo Pascal, there are many operators:

  • conditional;
  • composite;
  • a choice of several options;
  • transition;
  • assignment;
  • over records and objects;
  • empty;
  • cycles (with parameter, precondition, postcondition).

Operators of programs or constructs that are components of program code are always executed in the order in which they are written, i.e. from top to bottom, from left to right. The exception is conditional construction. The assignment operator, like many others, requires the presence of a “;” sign at the end of a line, ie semicolons. Thus, this symbol divides the constructions among themselves and increases the visibility of the program. But there is one “BUT": the semicolon should never be placed before the word else.

The input operators are read (variables) and readln (variables). Example: read (a); readln (a, b); readln (d).

The output operators are write (variables) and writeln (variables). Example: write (a, g); writeln ('enter data'); write ('surface area S =', S); write (f: 6: 3).

To delay the screen, Turbo Pascal uses the readln operator at the end of the code before the last end. It is in this program that the absence of such a line will not display the contents of the solution. In PascalABC, writing readln at the end of the code is optional.

To clear the screen, the clrscr statement is connected, which calls the built-in crt module. Again, each program has its own nuances.

Assignment operator

Now we pass directly to our topic. The assignment operator in Pascal is the process of storing variable values ​​in memory cells. In general terms, it looks like a single spelling of the colon with an equal sign, i.e. ": =". These characters are responsible for the assignment operation.

What is the assignment operator for? The mechanism of its work is as follows: in the course of the program, an expression is calculated, the result of which must be entered into memory. The address of the cell where the data will be entered is determined by the variable located to the left of the “: =” sign.

Schematically, this can be indicated as follows:

  • variable ← the resulting expression.

As an example, here are a few expressions:

  • A: = b + c / 2;
  • b: = n;
  • n: = b;
  • x: = 15;
  • x: = x + 3.

It should be noted that the two expressions “b: = n” and “n: = b” perform different actions.

A feature of the assignment operator is that the data located on opposite sides of the “: =” sign (which reads as “assign”) must belong to the same type. In other words: their assignment compatibility should be 100%. For example, an integer expression can be assigned to a real variable, since a subset of integer variables falls into the fractional region. Those. the notation “real variable: = integer expression” is correct.

what is the assignment operator intended for

Examples of tasks

To understand which assignment operator has the form, it is necessary to solve several problems. Only practice will avoid gross and unforgivable mistakes in writing program code, as well as save time.

Exercise 1

The circle is given. By condition, the circumference L is entered by the user from the keyboard. It is necessary to calculate the area of ​​the circle S bounded by this circle.

Algorithm of actions:

  • Calculate the radius value based on the circumference formula.
  • Set the circle area formula.
  • Display the result on the screen.

assignment operator has the form

Task 2

A truncated cone is given. The user sets the values ​​of the radii of the bases Rb, Rm and height h. Using the formulas and set values, calculate the volume and surface area of ​​the figure.

what actions does the assignment operator perform

Algorithm of actions:

  • Enter the required data using the keyboard.
  • Define formulas for finding the volume and surface area of ​​a truncated cone.
  • Calculate V and S.
  • Display them on the screen.

Source: https://habr.com/ru/post/K9452/


All Articles