How to set up smartphones and PCs. Informational portal
  • home
  • Errors
  • In object-oriented programming languages, variables play the same important role as in procedural languages. Variable type, name and value

In object-oriented programming languages, variables play the same important role as in procedural languages. Variable type, name and value

In Visual Basic and Gambas and in OpenOffice.org Basic variables are used to store and process data in programs.

Variables are set names that define the areas of the computer's RAM in which meaning variables. Variable values ​​can be data various types(integers or real numbers, character sequences, logical values, etc.).

Variable in the program it is represented by a name and is used to refer to data of a certain type, the specific value of which is stored in a cell of RAM.

Variable type.
The type of a variable is determined by the type of data that can be the values ​​of the variable. The values ​​of variables of the numeric types Byte, Short, Integer, Long, Single, Double are numbers, of the Boolean type - the values ​​"true" (True) or "false" (False), of the string type String - a sequence of characters. Variable type designations are language keywords and therefore stand out.

Different types of data require a different number of cells (bytes) to be stored in the computer's RAM.

Variable name.
Variable names define areas of the computer's RAM in which the values ​​of variables are stored. The name of each variable (identifier) ​​is unique and cannot change during program execution. In the languages ​​under consideration, a variable name can consist of various characters (Latin and Russian letters, numbers, etc.), but it must necessarily begin with a letter and must not include a dot "." The number of characters in a name cannot exceed 1023; however, for convenience, it is usually limited to a few characters.

Variable declarations.
It is necessary to declare variables in order for the executor of the program (computer) to "understand" what type of variables are used in the program.

To declare a variable, use Dim variable declaration statement... Using one operator, you can declare several variables at once, for example: Dim A As Byte, B As Short, C As Single, D As String, G As Boolean

Assigning values ​​to variables.
You can set or change the value of a variable using assignment operator... When an assignment statement is executed, the variable whose name is specified to the left of the equal sign gets the value that is to the right of the equal sign.
For instance:
A = 255
B = - 32768
C = 3.14
D = "informatics"
G = True

The value of a variable can be specified as a number, string, or boolean value, and can also be represented using an arithmetic, string, or boolean expression.

Let's analyze the process of executing a program by a computer (to be specific, written in the Visual Basic language). After starting the project, the Dim variable declaration operator will allocate the required number of cells in RAM for storing them:

  • for a non-negative integer variable A, one cell;
  • for an integer variable B, two cells;
  • for variable single precision C, four cells;
  • for a string variable C, two cells per character;
  • for the logical variable G, two cells.

Thus, in the memory for storing the values ​​of variables, 31 cells will be allocated, for example, cells from 1st to 31st.

Control questions:

  1. What is the difference between the type, name and value of a variable?
  2. What are the main types of variables used in the Visual Basic 2005 programming language? Gambas? OpenOffice.org Basic?
  3. Why is it recommended to declare variables before using them in a program?

Tasks:

  1. Determine the number of memory cells required to store the values ​​of the variables of the first seven types of the Visual Basic language listed in the first table.
    (We look at the column "Occupied memory". How many bytes are written - so many memory cells will be.)

Variables.

  • In object-oriented programming languages, and in particular in the Visual Basic language, variables play the same important role as in procedural programming languages. Variables are intended for storing and processing data.

  • Variables are defined by names that define the memory area in which the values ​​of the variables are stored. Variable values ​​can be data of various types (integers and real numbers, character sequences, logical values, etc.)


Variable definition.

  • A variable in the program is represented by a name and is used to refer to data of a certain type. The specific value of the variable is stored in the memory cells.


Variable type.

    The type of a variable is determined by the type of data that can be the values ​​of the variable. Values ​​of variables of numeric types ( Byte, Integer, Long, Single, Double) are numbers, logical ( Boolean) – True /False, string ( String) -sequences of characters, etc. Variable type designations are language keywords and therefore stand out.


Storing various types of data in the computer's memory.

  • Integers ranging from 0 to 255 ( Byte)- 1 byte

  • Double precision real number ( Double) - 8 bytes

  • Character strings ( String) - 1 byte per character.


Variable types.


Variable name.

  • The name of each variable (identifier) ​​is unique and cannot change during program execution. A variable name can consist of various characters (Latin and Russian letters, numbers, etc.), but it must necessarily begin with a letter and must not include the "." (Period) sign. The number of characters in the name cannot exceed 255.

  • A numeric variable can be named for example A or Number, and a string variable A or String. However, the developer of the Visual Basic language, Microsoft, recommends to include a special prefix, which denotes the type of the variables. Then it is advisable to write the names of variables as intA, or intNumber, and string names - strA and strString.


Variable type declaration.

  • It is important that not only the developer of the program (programmer) understands what type of variables are used in the program, but that the executor of the program (computer) can also take this into account.

  • The second is even more important, because if the computer does not know what type of variable is used in the program, it will consider it a variable of the generic type. Variant and will allocate 16 or more bytes for storing it in memory. This will lead to inefficient use and slow down the program.


Variable type declaration.

  • The variable definition operator is used to declare the type of a variable. The syntax for this statement is as follows:

  • Dim VariableName [ As Variable Type]

  • Using one operator, you can declare several variables at once:

  • Dim int Number As Integer, str String As String

  • Variables whose values ​​do not change during program execution are called constants. The syntax for declaring constants is as follows:

  • Const ConstantName [ As Type] = ValueConstant


Arithmetic, string and logical expressions. Assignment.


Arithmetic expressions.

  • In addition to variables of a numeric type, arithmetic expressions can also include numbers; various arithmetic operations can be performed on variables and numbers, as well as mathematical operations expressed using functions.

  • The order of calculation of arithmetic expressions corresponds to the generally accepted order of performing arithmetic operations (exponentiation, multiplication and division, addition and subtraction), which can be changed using parentheses.


String expressions.

  • String expressions can include variables of the string type, strings, and string functions.

  • Strings are any sequences of characters enclosed in quotation marks. For instance,

  • "Informatics", "2000", "2 * 2"

  • An operation can be performed on variables and strings concatenation. The concatenation operation is to concatenate a string and the value of string variables into a single string. The concatenation operation is indicated by the "+" sign, which should not be confused with the addition sign of numbers in arithmetic expressions.


Logical expressions.

  • In addition to logical variables, logical expressions can also include numbers, numeric and string variables, or expressions that are compared with each other using comparison operations (>, =,

  • A logical expression can take only two values: "true", "false", for example,

  • 5> 3 - true

  • 2 * 2 = 5 - false

  • Boolean operations can be performed on elements of logical expressions, which in the Visual Basic language are denoted as follows: logical multiplication - And, logical addition - Or, logical negation –Not. When writing complex logical expressions, parentheses are often used. For instance,

  • (5>3) And(2 * 2 = 5) - false

  • (5>3) Or(2 * 2 = 5) - true


Assigning Values ​​to Variables

  • A variable can get and change a value using an assignment operator. The syntax for this statement is as follows:

  • VariableName = Expression

  • Keyword Let not used in most cases. When an assignment statement is executed, the variable whose name is specified to the left of the equal sign receives a value equal to the value of the expression (arithmetic, string, or logical) that is to the right of the equal sign.

Integer variables can only store small integers that range from –32768 to +32767. Arithmetic operations on such numbers are performed very quickly. The symbol "%" is used to denote this type.

This type was first used in the QuickBASIC language. These variables contain integer values ​​from –2 147 483 648 to +2 147 483 647. It is denoted by the “&” symbol. Arithmetic operations on the given numbers are also very fast, and in the case of working with a 386DX or 486DX processor, only a small difference in the computation speed between Long Integer and Integer is found.

Single Precision

The symbol "!" Is the identifier for such numbers. This type of variable makes it possible to store fractional numbers, the precision of which is up to the seventh digit. That is, if the result is 12345678.97, then part 8.97 is not accurate. The result could be, for example, 12345670.01. Numbers can be 38 characters long. The products of mathematical operations with these variables will also be approximate. In addition, arithmetic operations are performed more slowly than with integer variables.

Double Precision

Variables of this type make it possible to store numbers with an accuracy of up to 16 digits and a length of up to 300 4b characters. The identifier is "#". Calculations with them are also approximate, and their speed is not very high. Variables of the Double Precision type are most often used for scientific calculations. Currency

This type did not exist in the GW-BASIC and QuickBASIC versions. It is used to avoid mistakes when converting decimal numbers to binary form and vice versa. This type can have up to 4 digits after the decimal point and up to 14 before it. Within this range, calculations are accurate. The identifier of such a variable is the "@" symbol. Since all arithmetic operations, except for addition and subtraction, are performed as slowly as in the case of double-precision variables, this type is more preferable for financial calculations.

With this data type, you can store time and date values ​​from midnight on January 1, 100 to midnight on December 31, 9999. Such values ​​in the text of programs are indicated by symbols "#", for example: Millenium = #January 1, 2000 #.

When you enter only a date value, Visual Basic assumes the time is 00:00.


  • Types variables... Integer. Integer variables are capable of storing only not very large integers, which range from –32768 to +32767.


  • Its use instead of integers variables is a good programming style.
    Variable type variant is capable of containing data of any type.


  • Procedures and functions for variables string type... A sequence of characters of a certain length is called a string.


  • Announcement types variables when handling events before using them - of course, with comments ...


  • Visual Basic is capable of working with 14 standard types variables... You can also define your own a type data.


  • Dynamic variable not explicitly indicated in descriptions variables, and you can't go to her
    Variable reference type contains the address of the dynamic variable in mind.

To use the preview of presentations, create yourself a Google account (account) and log into it: https://accounts.google.com


Slide captions:

Preview:

CLASS 9

LESSON №17. "Variables: type, name, value".

Lesson plan:

  1. Org. moment. (1 minute)
  2. Updating and testing knowledge. (5 minutes)
  3. Theoretical part. (15 minutes)
  4. The practical part. (15 minutes)
  5. D / z (2 min)
  6. Lesson summary. (2 minutes)

Org. moment.

Greetings, checking those present. Explanation of the course of the lesson.

2. Updating and testing knowledge.

In the last lesson, we began to get acquainted with the concept of an algorithm and the basics of programming.... Remind me what an algorithm is, what properties does it have, how is an algorithm written, what is a program?

3. Theoretical part.

It is known that any algorithm is compiled for a specific performer. Now, as an executor, we will consider a computer equipped with a programming system in a certain language.

The computer - the executor works with certain data according to a certain program. A program is an algorithm written in a programming language. Data is a multitude of quantities.

In order for the program to have universality, actions in it must be performed not on constant, but on variables quantities. Therefore, an important programming concept is the concept of a variable.

The computer works with information stored in its memory. A separate information object (number, symbol, string, table, etc.) is called size.

Values ​​in programming, as in mathematics, are divided by variables and constants ... The value of the constant remains unchanged throughout the entire program, the value of the variable can change.

Each variable has name, type and current value ... Variable names are calledidentifiers(from the verb "to identify", which means "to designate","symbolize"). Variable names can be letters, numbers, and other characters. Moreover, there may be not one letter, but several. Examples of identifiers: a, b5, x, y, x2, summa, bukva10 ...

There are three main types of quantities that a computer works with: numeric, symbolic, and logical. The data type characterizes the internal representation, the set of valid values ​​for this data, as well as the set of operations on them. Depending on the typea variable in the computer memory will be allocated a certain area.

A variable can be visualized as a box in which you can store something. The variable name is the inscription on the box, the value is what is storedin it at the moment, and the type of the variable indicates what is permissible to put in this box.

Any algorithm is built on the basis of the system of commands of the executor for whom it is intended.

Regardless of what programming language the program will be written in, the algorithm for working with values ​​is usually composed of the following commands:

  1. assignment;
  2. input;
  3. conclusion;

Variable values ​​are set usingassignment operator... The assignment command is one of the main commands in the algorithms for working with values. When assigning a value to a variable, the old value of the variable is erased and it gets a new value.

In programming languages, the assignment command is usually denoted either ": =" (colon and equal) or "=" (equal). The ": =" (or "=") sign reads " assign ". For instance:

z: = x + y

The computer first evaluates the expression x + y, then assigns the result to the variable z to the left of the ": =" sign.

If before executing this command, the contents of the cells corresponding to the variables x, y, z were as follows:

A dash in cell z indicates that the seed can be anything. It is irrelevant to the result of this command.

If there is a numeric variable to the left of the assignment sign, and a mathematical expression to the right, then such a command is called an arithmetic assignment command, and the expression is called arithmetic.

In a particular case, an arithmetic expression can be represented by one variable or one constant.

For instance:

x: = 7

a: = b + 10

c: = x

The values ​​of the variables that are the initial data of the problem being solved, as a rule, are specified by input. On modern computers, input is most often performed in a dialogue mode with the user. On an input command, the computer interrupts the execution of the program and waits for user action. The user must type the input values ​​of the variables on the keyboard and press the key. The entered values ​​are assigned to the corresponding variables from the input list, and program execution continues.

Input commands in the descriptions of algorithms it usually looks like this:

input

or

input ()

Here is the flowchart for the above command.

1. Memory before command execution:


When performing point 3, the entered numbers must be separated from each other by some separators. These are usually spaces.

Therefore, we can conclude:

Variables are given specific values ​​by executing an assignment command or an input command.

If no value has been assigned to a variable (or not entered), then it is undefined. In other words, nothing can be said about the value of this variable.

The results of solving the problem are communicated by the computer to the user by performing output commands.

The output command in the descriptions of the algorithms usually looks like this:

conclusion

or

conclusion ()

For example: output (x1, x2).

With this command, the values ​​of the variables x1 and x2 will be transferred to the output device (most often this is a screen).

4.Practical part.

Let's compose an algorithm for calculating the perimeter of a triangle. We need 4 variables to store the lengths of the sides of the triangle and its perimeter. The perimeter is the sum of all sides.

Algorithm for calculating the perimeter of a triangle
variables a, b, c, p - integers
Start
input (a, b, c)
p: = a + b + c
output (p)
the end

First, the computer will ask the user for the values ​​of the variables a, b, c, then it will perform calculations and display the result on the screen.

Line variables a, b, c, p - integers- is called variable description. Some programming languages ​​require a mandatory description of all variables before using them in the program, some are more loyal.

The resulting algorithm has a linear structure.

  1. D / z.
  2. Lesson summary.

These Guidelines outline the basic concepts, operators, and constructs of the VisualBasic (VB) programming language. In many ways, they repeat the elements of the QBasic programming language. Therefore, students who are familiar with QBasic will easily master or remember the stated programming elements.

The guidelines are intended primarily for those who are completely unfamiliar with programming. However, you need to be able to work in the Windows operating system, to know the basic principles of the device and the functioning of a computer.

Particular attention is paid to clarifying the most important concept of programming - variable, then - arrays, as well as select operators, switches and loop operators.

Simultaneously with the study of programming elements, it is necessary to study the basic concepts of the object-oriented programming language VisualBasic: controls, their properties, events, methods. Know the basics of designing a form interface, customize control properties, and programmatically set values ​​for them.

Writing a program is preceded by the development of a problem algorithm. In the Methodological Instructions, verbal descriptions of algorithms and fragments of programs of the main typical educational programming problems are given. Flowcharts of algorithms are not shown here at all. But before building such schemes, it is necessary to be able to verbally formulate the entire algorithm or only its idea. Without this, it is impossible to meaningfully construct a block diagram of the algorithm.

The algorithm of the problem can be developed by analogy with a well-known typical problem, or just guess. But for such a guess to be successful, knowledge of programming techniques and fluency in the basic programming elements, which are listed above, are required. You need to learn to think with VB operators.

At the end of the Methodological Guidelines, in section 10, using an example of a simple problem, it is shown how knowledge of programming elements allows you to choose and develop various algorithms for solving a problem.

Basic programming elements

Familiarity with the basic elements of VisualBasic programming begins with the concept of a variable. Anyone who is already familiar with this concept from the QBasic programming system will easily discover the differences that variables have in VisualBasic.

1. Variables, the assignment operator

In programming languages, one of the most important concepts is the concept variable.

Variable definition

Let's remember elementary algebra and solve a simple problem.

Task... Calculate function

for the values ​​of the coefficients and the argument:

a = 3; b = - 4; c = 5; x = 2.

Without any programming, we will substitute the values ​​for a. b, c, x into the formula

Y = 3٠2 2 - 4٠2 + 5,

we will perform the appropriate arithmetic operations and get as a result Y = 9.

When programming this task, the program fragment will look like this:

Y = a * x ^ 2 + b * x + c

Print “Y =“; Y

All calculations here coded in accordance with the rules of the VisualBasic language. To make it clearer, let's remember that codingit is an expression of one data type through another data type.

The same calculations can be programmed, coded like this:

Y = k1 * x ^ 2 + k2 * x + k3

Print “Y =“; Y

The result of calculating Y will not change.

Both versions of the program practically repeat the formulation of the problem. However, this is only in appearance. So, equal signs in a program are not equal signs, but assignment operators ... With the help of them, the coefficients are assigned numerical values ​​written to the right of the assignment sign. Multiplication, exponentiation encoded with special characters * and ^ .

Operator Print displays information on forms (or in graphic fields). Everything after the Print statement is written in double quotes, this is the text.It is displayed unchanged what is written is displayed. If a variable is written further, then its value is displayed... Therefore, the form will display: Y = 9. In the Print statement, the text is followed by a semicolon (;). This is a separator. In this case, the value of the variable is displayed immediately after the text. If you put a comma separator after the text, then the output of the variable value will occur after 14 characters, counting from the edge of the form.

However, when calculating Y one word, letters are multiplied by others (almost like in an algebraic expression), words, letters are squared and words and letters are also added. But if the result is correct, therefore, at some stage of the program execution, the numerical values ​​of the coefficients and the argument X are transferred to the formula. This happens at the time of the program execution after its translation (translation) into machine codes.

When programming in machine codes, performing the simplest arithmetic operation on two numbers requires specifying the operation code, the addresses of the RAM cells where these numbers are stored, and specifying the address of the memory cells where the result of the operation will be stored. When such a program is executed, at the command of the processor, data is transferred from these cells to the processor registers (the so-called processor memory cells), where a certain operation is performed on them. Sometimes, according to the algorithm of the task, the result can be placed, written into the same memory cell in which one of the numbers on which the operation was performed was stored. In this case, the old data value will be overwritten by the new one.

In high-level programming languages analogue of these memory cells isvariable. Hence the following definition.

Variable it named memory area ,where data is stored, with which certain operations and actions are performed in the program, and which can be changed during the execution of the program.

The name itself "variable" suggests its variability.

The most important position of programming languages:

After assigning a value to a variable, you can use the variable instead. This is tantamount to using the values ​​themselves.

This is the main point of introducing variables in programming languages.

In the above example, in the first version of the program, the variables are: a, b, c, x, Y. In the second version of the program, the names of some variables are changed and have names: k1, k2, k3, x, Y. It depends only on the programmer what names the variables will have.

V calculation tasks we can distinguish variables that are initial data, variables for storage intermediate results and resulting variables, for the calculation of which the problem is formulated.

It is the use of variables in a program that allows one and the same program to be executed with different initial values ​​of variables without changing the program code.

After the program is translated into machine codes, each variable in the computer is allocated a certain area of ​​RAM with its own address. This is done by translators when translating program codes written by a programmer into machine codes and an operating system.

But when developing a program, the programmer does not care at all what the address of the allocated memory will be and the size of this memory for storing the values ​​of variables. The main thing is that if the programmer has somehow set, assigned some values ​​to the variables, then in any expressions of the program where these variables will be encountered, their values ​​will be substituted instead of variables.

Top related articles