Internet Windows Android

Branch command in full and not. Guidelines and tasks Algorithms with branching examples in BASIC

Topics: “Branching and looping programs in BASIC, work

with arrays."

The task consists of several parts:

- read the lecture material on the topic (pp. 1-5) and analyze the examples;

- executeexercise 1(p. 5);

- read the lecture material on the topic (pp. 6-8) and analyze the examples;

- executeexercise2 (With.8-9 );

- read the lecture material on the topic (pp. 9-14) and analyze the examples;

- executetask 3(p. 14);

- get acquainted with the concept of “matrix” and examples of working with matrices (p. 15-17).

Send solved assignments to the school's email address, in a letter to

In the “Topic” section, indicate “Kaverina N. E. computer science.” At the beginning of the text

letters indicate your last name, class, date of completion of the assignment and

job numbers (13 - 1 7 ). Copy the assignments and then write down the answers.

ATTENTION! The assignment must be submitted by January 12.

Branching in algorithms and programs.

Review the lecture material on this topic, analyze examples,

If you have any questions, ask them in a letter.

Branching algorithm is an algorithm in which, depending on the condition, either one or another sequence of actions is performed.

In many cases, it is required that one sequence of actions be performed under certain conditions, and another under other conditions.

The entire program consists of commands (operators). Commands can be simple or compound (commands within which other commands occur). Composite commands are often called control constructs. This emphasizes that these statements control the further course of the program.

Let's consider writing a conditional statement in Basic.

A simple form of the operator looks like this:

IF<УСЛОВИЕ>THEN<ОПЕРАТОР>

IF<УСЛОВИЕ>
<ОПЕРАТОР 1>
<ОПЕРАТОР 2>

<ОПЕРАТОР
N> END IF

If the condition is true, then the program performs the operator that comes after the keyword THEN(or a series of statements from the keyword THEN before END IF), and then follows the usual procedure. If condition not executed , then the operator after THEN(or a series of statements from THEN before END IF) Not performed, and the program immediately returns to normal operation.
Design IF...THEN allows, depending on the validity of the condition, to either execute the statement or skip this statement.
Design IF...THEN...END IF allows, depending on the validity of the condition, to either execute a group of operators or skip this group of operators.

Conditions are another type of Boolean expression. They use the following comparison operators:

more or equal

less or equal

To the right and left of the comparison sign there should be quantities belonging to the same type. The comparison results in a logical value that has the value TRUE or FALSE.

Example:
5<7 - ИСТИНА;
8=12 - FALSE (we check whether 8 is equal to 12, we are checking, not asserting, that 8=12);

Previous designs allowed you to bypass or execute a series of statements depending on the validity of the condition. It wasn't a branch yet. To allow calculations to branch out in several directions, the design IF...THEN...ELSE...END IF.

IF<УСЛОВИЕ>THEN
<ОПЕРАТОРЫ 1>
ELSE
<ОПЕРАТОРЫ 2>
END IF

If the condition is true (TRUE), then<операторы 1>(standing between THEN And ELSE), A<операторы 2>(standing between ELSE And END IF) will be skipped.
If the condition is not true (FALSE), then<операторы 1>are ignored and executed<операторы 2>.

IF - if, THEN - then, ELSE - otherwise.

If It's dark in the room, Then need to turn on the light.

If it will rain, Then I need to take an umbrella
otherwise, don't take an umbrella.

Example: Check whether the entered number is equal to a certain value, and if it is equal, display a message on the screen about the equality of the numbers.

REM compare a number with some value
INPUT "Enter a", a
IF a=7 THEN PRINT "The numbers are equal"
END

After starting the program, it is checked whether the entered value is equal to seven or not. If equal, the message “Numbers are equal” is displayed on the screen.

Example: Determine the larger of two numbers, display it on the screen, then multiply it by two and display the result on the screen.


INPUT "Enter a", a
INPUT "Enter b", b
IF a>b THEN
PRINT "Larger number: ", a
с=2*a
ELSE
PRINT "Larger number: ", b
с=2*b
END IF
PRINT "result: ", c
END

First the program asks for both numbers, then checks the condition a>b. If the condition is true, then the number a is displayed on the screen, then this number is doubled. Otherwise, the number b is displayed on the screen, then the number b is doubled. Finally, twice the value of the larger number is displayed on the screen.

Please note: the program has one drawback - it does not take into account the case when the entered numbers are equal. Let's fix this by using the nesting of one condition within another.

REM determine the larger of two numbers...
INPUT "Enter a", a
INPUT "Enter b", b
IF a=b TNEN
PRINT "The numbers are equal"
с=2*a
ELSE
IF a>b THEN
PRINT "Larger number: ", a
с=2*a
ELSE
PRINT "Larger number: ", b
с=2*b
END IF
END IF
PRINT "result: ", c
END

There are two conditional operators in this program, the first one checks the condition of equality of numbers and, if it is fulfilled, a message will be displayed about the equality of numbers, if the numbers are not equal, then the second condition is checked...

Example: Solving a quadratic equation.
The solution to a quadratic equation depends on the value of the discriminant.

REM Solving a Quadratic Equation
INPUT "Enter coefficient a: ", and
INPUT "Enter coefficient b: ", b
INPUT "Enter coefficient c: ", c
d=b*b-4*a*c
IF d<0 THEN
PRINT "No roots"
ELSE
IF d=0 THEN
x=-b/(2*a)
PRINT "root of equation: ", x
ELSE
x1=(-b-SQR(d))/(2*a)
x2=(-b+SQR(d))/(2*a)
PRINT "roots of equation: ", x1, x2
END IF
END IF
END

Exercise 1.

1. Write a program to test your knowledge of the multiplication table (one column of your choice). Use comments to explain the respondent's next steps.

2. 3 numbers are given (enter them from the keyboard with comments) a, b, c. Write a program that selects and prints the larger number.

3. Write a program to calculate the value of Y if you know:

o" appears in the text, it is necessary to go through all the letters. Despite the simplicity of this program, it is very difficult for a person to execute it, but for a computer this is a task that takes several seconds.

Round robin algorithm - a description of actions that must be repeated a specified number of times or until a specified condition is met.

The list of repeated actions is called the body of the cycle.

For example, in a physical education lesson you have to run a number of laps around the stadium.

Such cycles are called - countered loops.

In Basic they are written as follows:

FOR Counter=StartValue TO EndValue
loop body
NEXT [Counter]

The parameters indicated in square brackets are optional (they can be omitted). By default, the loop step is one, i.e., each time after passing through the loop body, the counter is increased by one.

Example: Display all numbers from 1 to 100. To do this, you could write the following program:

REM Outputs numbers from 1 to 100
PRINT 1
PRINT 2
PRINT 3
PRINT 4
PRINT 5
PRINT 6
PRINT 7
...
PRINT 98
PRINT 99
PRINT 100
END

Just some 102 lines :-). Although the same program can be written much shorter:

REM Outputs numbers from 1 to 100
FOR I=1 TO 100
PRINT I
NEXT
END

By slightly correcting the program, you can make it print all the numbers from a to b.

REM Print numbers a to b
a=55
b=107
FOR I=a TO b
PRINT I
NEXT
END

In this case, the counter on the first pass through the loop takes the value of the variable a, after which the statements up to the NEXT keyword are executed. After this, the counter is increased by one and compared with the value of the variable b; if the counter is less, then the loop is executed again.

It's easy to make the program print numbers in reverse order. To do this, the loop step must be equal to -1 (minus one). In this case, the counter value will be decreased by one each time the loop is completed.

REM Print numbers b to a
a=55
b=107
FOR I=b TO a STEP -1
PRINT I
NEXT
END

Example: Calculate the sum of two-digit natural numbers.

REM Calculate the sum of two-digit natural numbers
FOR I=10 TO 99
s=s+I
NEXT
PRINT "Result = ",s
END

The program cycles through numbers from 10 to 99 each time it performs the actions s=s+I. From a mathematical point of view, this is a completely meaningless entry, but let’s look at it more carefully.
The process of solving a computational problem is a process of sequentially changing the values ​​of variables. As a result, a result is obtained in certain variables. The variable gets a certain value as a result assignments. You remember that assignment is the entry of a certain value into a cell allocated for a variable as a result of executing a command.
As a result of the operation a=5 variable A gets the value 5.
As a result of the operation с=a+b variable With gets a value equal to the sum of the variable values A And b.
As a result of the operation s=s+I variable s gets a value equal to the sum of the previous value of the variable s and variable values I. That is, if before the assignment operation the value s was equal to 5, and the variable I is 3, then after the operation the value of the variable s will be equal to 8 (5+3, old value s+ value I).
This means that after executing our program, the variable s will store the sum of all two-digit numbers from 10 to 99.

REM Calculate factorial of a number
a=5
f=1
FOR I=1 TO a
f=f*I
NEXT
PRINT f
END

You, of course, noticed that before the start of the loop we assigned the variable f a value equal to one. Otherwise we would get zero as a result.

Task 2.

1. Using the operator FOR...NEXT, write a program for calculating and printing the value of the function Y=5/X when the argument X changes from -5 to +5 in increments of 0.5.

2. Using the operator FOR...NEXT, Write a program that will calculate the sum of the first 10 integers and display the result on the screen.

3. Write the result of executing the program:

10 FOR X=1.5 TO 0 STEP -0.5

Arrays . One-dimensional arrays.

When working with a large number of data of the same type, it is very convenient to use arrays.

So, what are arrays...
An array is a type of variable. It allows you to store as many values ​​as you want under the same name. Each specific array value must be accessed through a numeric index.

An array is a set of variables that have the same name (identifier), but differ in serial numbers (indices).

Typically, arrays are used to group variables that have many common properties. For example, if there are 30 students in a class, then the name of each student could be stored in a separate string variable: name1, name2, ... But introducing 30 new variables is extremely inconvenient. It can be done simpler: declare one array name() with 30 elements. An index is placed in brackets when it is necessary to refer to a specific element.

Counting array elements in many languages ​​starts from zero. Therefore, the name of the first (according to the class register) student will be stored in the name(0) variable, the second - in the name(1) variable, and the last (thirtieth) - in the name(29) variable.

In order to use an array, it must first be declared in the program. To do this, use the operator DIM. By default (if there is no operator DIM in the program) an array of 10 elements is considered given.

DIM a(100) AS INTEGER
This is an array of one hundred elements, each of which can be an integer.
DIM name(30) AS STRING
DIM mas(20)
This is an array of 20 elements, the variable type is not explicitly specified.

DIM mas1(10) AS INTEGER

Accessing array elements:

a(24)
name(5)
mas(2)
mas(3)

The main advantage of arrays over the usual set of variables of different names is that. that the index of the desired element can be written not as a number, but as a variable or even calculated using an expression. This makes it possible to use arrays inside loops - this is actually what they were invented for. If the program contains an array, then most likely you will also find a loop in it.

You can also declare an array this way:

DIM mas2(1 TO 10) AS INTEGER

or even like this:

DIM a2(5 TO 10) AS INTEGER

What is the difference? In this case, the indexing of array elements does not start from zero, but from the index you need (in the example, the array mas2 has indexes from 1 to 10, array a2- from 5 to 10).

Let's say there are 30 students in a class. Let's assume that an array is created to store their grades in a subject DIM mark(30) AS INTEGER. The following program will give each student a random grade from 3 to 5. Of course, you cannot assign grades this way, but this example shows that the program does not become more complex if there are not 30 students in the class, but one hundred and fifty million. The combination of arrays and loops allows for amazing simplicity.

REM Grading :)
DIM mark(30) AS INTEGER
FOR I=0 TO 29
mark(I)=3+INT(RND*3)
NEXT
END

To create random numbers in the Basic language, use the standard function RND. It creates a random number in the range from 0 to 1. Multiplying it by 3, we get a random number from 0 to 3. And taking the integer part from it (using the INT function), we get an integer random number in the range from 0 to 2. Adding to it the number 3, we give a random estimate that is not less than 3 and not more than 5.

Example: Create a program to fill an array of 15 elements with random numbers in the range from 1 to 10. Provide for displaying the array on the screen.


DIM mas(15) AS INTEGER
FOR I=0 TO 14
mas(I)=1+INT(RND*10)
NEXT
CLS
PRINT "Array output"
FOR I=0 TO 14
PRINT mas(I);
NEXT
END

CLS- cleaning the screen. Semicolon (;) in statement PRINT allows you to output array elements to a string.

The same task, but differing in the array declaration:

REM Filling and outputting an array
DIM mas(1 TO 15) AS INTEGER
FOR I=1 TO 15
mas(I)=1+INT(RND*10)
NEXT
CLS
PRINT "Array output"
FOR I=1 TO 15
PRINT mas(I);
NEXT
END

Everything is very simple. Which option to use is up to you.

Example: Print the number of negative array elements.

REM Print the number of negative elements

DIM mas(n) AS INTEGER
FOR I=0 TO n-1

NEXT
CLS
PRINT "Array output"
FOR I=0 TO n-1
PRINT mas(I);
NEXT
FOR I=0 TO n-1
IF mas(I)<0 THEN k=k+1
NEXT
PRINT
PRINT "Number of negative elements: ",k
END

The number of negative array elements is counted in a loop:
FOR I=0 TO n-1
IF mas(I)<0 THEN k=k+1
NEXT

Example: Write a program to calculate the largest element of an array and its number.

REM of calculating the largest array element and its number
INPUT "Enter the number of array elements", n
DIM mas(n) AS INTEGER
FOR I=0 TO n-1
INPUT "Enter an array element", mas(I)
NEXT
CLS
PRINT "Array output"
FOR I=0 TO n-1
PRINT mas(I);
NEXT
max=mas(0)
number=1
FOR I=0 TO n-1
IF mas(I)>max THEN
max=mas(I)
number=I+1
END IF
NEXT
PRINT
PRINT "Maximum element: ", max, " with number ", nomer
END

The task is performed in the lines:
max=mas(0)
number=1
FOR I=0 TO n-1
IF mas(I)>max THEN
max=mas(I)
number=I+1
END IF
NEXT First, let's take the largest element to be the first element of the array mas(0). Then, going through all the elements one by one, we compare them with the value max and if mas(I)>max, then we take this element as the largest.

Example: create a program to sort an array in ascending order.

REM array sorting
INPUT "Enter the number of array elements", n
DIM mas(n) AS INTEGER
FOR I=0 TO n-1
mas(I)=1+INT(RND*10)
NEXT
CLS
PRINT "Array output"
FOR I=0 TO n-1
PRINT mas(I);
NEXT
REM array sorting
FOR I=0 TO n-2
FOR J=I+1 TO n-1
IF mas(I)>mas(J) THEN
REM if we find a smaller element, then swap them
a=mas(I)
mas(I)=mas(J)
mas(J)=a
END IF
NEXT J
NEXT I
REM end of array sorting
PRINT
PRINT "Output sorted array"
FOR I=0 TO n-1
PRINT mas(I);
NEXT
END

Sometimes it is convenient to use operators to enter data DATA And READ.
DATA specifies values ​​to be read by subsequent statements READ. READ reads these values ​​and assigns them to variables. RESTORE allows READ recalculate the values ​​in the specified statement DATA.

DATA constants
READ variables

Example: entering an array using the DATA operator.

REM Data entry from DATA
DIM mas(5) AS INTEGER
DATA 2, -4, 1, 5, 9
REM array input
FOR I=0 TO 4
READ mas(I);
NEXT
REM array output
FOR I=0 TO 4
PRINT mas(I);
NEXT
END

Task 3.

1. Write a program to calculate the sum and product of array elements greater than 4. Array A consists of 15 elements.

2. Write a program to find the minimum element of array B, consisting of 20 elements.

3. Given arrays A and B, consisting of 20 elements. A) Write a program to obtain a new array X, consisting of 20 elements, if it is known that x(I)=2*a(i)-b(i). B) Write a program to obtain a new array Y, consisting of 15 elements, if it is known that y(I)=a(i)+3*b(i).

Arrays. Two-dimensional arrays.

Two-dimensional arrays can be thought of as tables whose cells store the values ​​of the array elements, and the indexes of the array elements are the row and column numbers.

Two-dimensional arrays are declared in the same way as variables and one-dimensional arrays. For example, an integer numeric array containing 3 rows and 4 columns is declared as follows:

DIM tabl(3 ,4) AS INTEGER

DIM tabl1(1 TO 3 ,1 TO 4) AS INTEGER

Using a two-dimensional 9x9 array and two nested loops, you can easily create a program that implements the multiplication table. The factors will be the values ​​of the row and column indices, and their products will be the values ​​of the array elements.

DIM tablum(1 TO 9 ,1 TO 9) AS INTEGER

REM Multiplication table
DIM tabum(1 TO 9, 1 TO 9) AS INTEGER
REM Filling an array - creating a multiplication table
FOR I=1 TO 9
FOR J=1 TO 9
tabum(I, J)=I*J
NEXT J
NEXT I

FOR I=1 TO 9
FOR J=1 TO 9
PRINT tabum(I, J);
NEXT J
PRINT
NEXT I
END

Example: In a 3x4 table, calculate the number of negative elements, the sum of even elements, and the product of the elements of the second row.

REM calculate quantity...
DIM tabl(1 TO 3, 1 TO 4) AS INTEGER
REM Array Filling
FOR I=1 TO 3
FOR J=1 TO 4
INPUT "Enter array element:", tabl(I, J)
NEXT J
NEXT I
REM Displaying an array on the screen as a table
CLS
FOR I=1 TO 3
FOR J=1 TO 4
PRINT tab(I, J);
NEXT J
PRINT
NEXT I
REM required calculations
k=0
s=0
p=1
FOR I=1 TO 3
FOR J=1 TO 4
IF table(I, J)<0 THEN k=k+1
IF tabl(I, J) MOD 2 = 0 THEN s=s+tabl(I, J)
IF I=2 THEN p=p*tabl(I, J)
NEXT J
NEXT I
PRINT
PRINT "result:"
PRINT "negative elements: ", k
PRINT "sum of even elements: ", s
PRINT "product of the elements of the second line: ",p
END

Lesson 3

Branching in algorithms and programs.

A branching algorithm is an algorithm in which, depending on the condition, either one or another sequence of actions is performed.

In many cases, it is required that one sequence of actions be performed under certain conditions, and another under other conditions.

The entire program consists of commands (operators). Commands can be simple or compound (commands within which other commands occur). Composite commands are often called control constructs. This emphasizes that these statements control the further course of the program.

Let's consider writing a conditional statement in Basic.

A simple form of the operator looks like this:

IF<УСЛОВИЕ>THEN<ОПЕРАТОР>

IF<УСЛОВИЕ> <ОПЕРАТОР 1>[:<ОПЕРАТОР 2>:…:<ОПЕРАТОР N>]

If the condition is true, then the program performs the operator that comes after the keyword THEN(or a series of statements from the keyword THEN before end), and then follows the usual procedure. If catchNot fair, then the operator after THEN(or a series of statements from THEN) Not performed, and the program immediately returns to normal operation.
Design IF...THEN allows, depending on the validity of the condition, either to execute the statement or skip this statement. Construction IF...THEN... allows, depending on the validity of the condition, to either execute a group of operators or skip this group of operators.

Conditions are another type of Boolean expression. They use the following comparison operators:

more or equal

less or equal

To the right and left of the comparison sign there should be quantities belonging to the same type. The comparison results in a logical value that has the value TRUE or FALSE.

5<7 - ИСТИНА;

8=12 -FALSE (we check whether 8 is equal to 12, we are checking, not asserting, that 8=12);

Previous designs allowed you to bypass or execute a series of statements depending on the validity of the condition. It wasn't a branch yet. To allow calculations to branch out in several directions, the design IF...THEN...ELSE...

IF<УСЛОВИЕ>THEN<ОПЕРАТОРЫ 1>ELSE<ОПЕРАТОРЫ 2>
If the condition is true (TRUE), then<операторы 1>(standing between THEN And ELSE), A<операторы 2>(coming after ELSE) will be skipped.
If the condition is not true (FALSE), then<операторы 1>are ignored and executed<операторы 2>.

IF - if, THEN - then, ELSE - otherwise.

If It's dark in the room, Then need to turn on the light.

If it will rain, Then I need to take an umbrella otherwise, don't take an umbrella.

Example: Check whether the entered number is equal to a certain value, and if it is equal, display a message on the screen about the equality of the numbers.

REM compare a number with some value
INPUT "Enter a", a
IF a=7 THEN PRINT "The numbers are equal"
END

After starting the program, it is checked whether the entered value is equal to seven or not. If equal, the message “Numbers are equal” is displayed on the screen.

Example: Determine the larger of two numbers, display it on the screen, then multiply it by two and display the result on the screen.


INPUT "Enter a", a
INPUT "Enter b", b
IF a>b THEN PRINT "Larger number: ", a:с=2*a
ELSE PRINT "Larger number: ", b:с=2*b
PRINT "result: ", c
END

First the program asks for both numbers, then checks the condition a>b. If the condition is true, then the number a is displayed on the screen, then this number is doubled. Otherwise, the number b is displayed on the screen, then the number b is doubled. Finally, twice the value of the larger number is displayed on the screen.

Please note: the program has one drawback - it does not take into account the case when the entered numbers are equal. Let's fix this by using the nesting of one condition within another.

REM determine the larger of two numbers...
INPUT "Enter a", a
INPUT "Enter b", b
IF a=b TNEN PRINT "The numbers are equal":c=2*a
ELSE IF a>b THEN PRINT "Larger number: ", a: с=2*a
ELSE PRINT "Larger number: ", b: с=2*b
PRINT "result: ", c
END

There are two conditional operators in this program, the first one checks the condition of equality of numbers and, if it is fulfilled, a message will be displayed about the equality of numbers, if the numbers are not equal, then the second condition is checked...

Example: Solving a quadratic equation.
The solution to a quadratic equation depends on the value of the discriminant.

REM Solving a Quadratic Equation
INPUT "Enter coefficient a: ", and
INPUT "Enter coefficient b: ", b
INPUT "Enter coefficient c: ", c
d=b*b-4*a*c
IF d<0 THEN PRINT "Корней нет"
ELSE IF d=0 THEN x=-b/(2*a) : PRINT "root of equation: ", x
ELSE x1=(-b-SQR(d))/(2*a): x2=(-b+SQR(d))/(2*a) : PRINT "equation roots: ", x1, x2
END

Logical conditions are called simple, if the branch has two branches and complex, if there are more than two branches.

AND and

OR or

NOT not

Calculate

Task 1.

Define is a triangle with sides A, B, C isosceles.

INPUT “Enter the dimensions of the sides of the triangle A, B, C”; A ,B ,C

A line of code in a Visual Basic program is called a program statement. A program statement is a combination of Visual Basic keywords, properties, functions, arithmetic or logical operators, and symbols that together form a valid statement recognized by the Visual Basic compiler.

93. Arrays and loop operations

Lesson 22. Loop operators For...Next and For Each

Loops in VB.NET are similar to loops in other programming languages. In this lesson we will look at two types of loops - For...Next and For Each loops. For...Next loops are executed a certain number of times. That is, if you know in advance how many times your loop should be executed, then we use the For...Next loop. The For Each loop is used to iterate over the elements of collections and arrays. For example, using For Each you can list (traverse) all the elements on a form or process all the rows in a database.

Here is an example for the For...Next operator:

Dim A(10) As Integer

Dim i As Integer

"Fill the array with squares of numbers

"Retrieving array elements

Console.WriteLine(A(i))

In this example, an array of 10 integers is first filled with squares of numbers, then the elements of the array are displayed on the screen. Instead of a line

You can write

In For...Next loops, the loop counter (i in this case) changes automatically.

In the example given, the counter is incremented by 1 each time the loop is passed. If you need a different step, then we use the construction with Step:

For i = 0 To 9 Step 2

"We're doing something

This loop will be executed 5 times.

Now let's look at an example using a For Each loop:

Dim z As Integer

Dim A(10) As Integer

"Fill array A

For Each z In A

If z > 10 Then

Console.WriteLine(z)

As you can see, in our loop we go through all the elements in array A (this can be seen from the In A construction). If the array element is greater than 10, we display it on the screen.

Typically, For Each loops are used to enumerate elements in various collections (for example, elements on a form or all fonts in Windows).

Using data sets when solving problems. Arrays in VB. Indexed variables. Description of arrays. The order in which the lower and upper bounds of array indices are assigned. Arrays of controls. The order of creating arrays of controls. An array can be a series of memory cells allocated to store an indexed variable. X1,Y2, Zt+1, Mi,j+1 – index variables. => X(1), Y(2), Z(i+1), M(I, f+1) – in YAPVU, designed for working with number series. dlina (1), dlina (2), dlina (3) – a one-dimensional array. Dim dlina (1 to 3) As Integer – description of the array in the program, with 1 being the lower bound of the array, 3 being the upper bound of the array. Indexing of arrays does not necessarily start at 0 or 1. An array of controls is formed using the Index property; by assigning the Index property, we tell the computer that this object is considered an element of the array. A control array is a series of numbered objects. Creation method: 1) Assigning the Index property at the design stage. 2) By copying to Win (copy/paste). 3) Programmatically (Load Text 1(1) - method). Load loads an object named Text1(1) onto the form. Two-dimensional arrays. They are specified similarly to one-dimensional ones: Dim dlina (1 to 3, 1 to 4) As Integer, where boundaries in two directions are listed in parentheses separated by commas, the first (1 to 3) boundaries are vertical, and the second are horizontal (1 to 4). Accessing an element of a two-dimensional array: dlina(1,3) – element in the first row, third column.

Data arrays are used to store multiple variable values:

Static x(2) As Integer (x=0,1,2).

If the size of the array cannot be determined in advance, then a dynamic array is used:

Dim Y() As Integer.

To define constants, the keyword is used:

Const Pi = 3.142.

Array as a procedure parameter

Rem Program_2.5 – Array as a procedure parameter

Dim n As Integer

Dim's As Single

Dim x(2) As Single

x(1) = 5: x(2) = 15: s = 0

Call sumir(x(), n, s) ‘ call the procedure for summing array elements

MsgBox "s = " & Str(s)

Sub sumir (x() As Single, n As Integer, s As Single)

Dim i As Integer

A function is understood as a group of operators united under an original name. The last character in a function name determines the type of value it returns.

User-defined functions (not library functions composed by the user) are declared by entering in the code editing window a header consisting of the Function keyword followed by the function name with the required arguments in parentheses. After this, the following template automatically appears:

Function FUNCTION_NAME

The necessary statements are inserted into the space between the function name and the End Function keywords.

If, when calling a function, any values ​​(arguments) must be passed to it, then in the header after the function name, the corresponding variables must be listed, separated by commas.

The general syntax for defining a function is:

Function FUNCTION_NAME(PARAMETER_1[, PARAMETER_2]...)

OPERATOR_1

[OPERATOR_2]

FUNCTION_NAME = EXPRESSION

where PARAMETER is a data element passed to the function when it is called; Static – if this keyword is present in a function declaration, local variables declared in the body of the function retain their values ​​between calls to this function.

Methods for calling functions: X = fun1(Y), or Call fun1 Y.

If a function must return a character (string) value, then the last character in its name must be the $ sign.

Each function always returns only one value.

Examples of functions:

1. Passing a parameter from a function

Rem Program_2.6 – Passing a parameter from a function

Dim x As Single, y As Single, s As Single

s = sumir(x, y) ‘ call the function of summing two numbers

MsgBox "s = " & Str(s)

Function sum! (x!, y!) ‘ definition of the function of summing two numbers

sumir! = x! +y!

In addition to simple variables, you can use arrays to store values. An array is a collection of variables with the same name and different indices. Each such variable is called an array element. The number of elements stored in an array is called the size of the array. The size of the array is limited by the amount of RAM and the data type of the array elements. All array elements have the same type. However, if the array is of type variant, then individual elements may contain data of different types. For example, some elements can be numbers, others can be strings or objects. In Visual Basic, there are fixed-size arrays and dynamic arrays. A fixed-size array has a fixed size that is specified when it is declared. Dynamic arrays can change size during execution. What is a two-dimensional array? This is a set of data of the same type, the location of each element of which is determined not by one index, but by two. For example, for those who have played “sea battle” since childhood, it will not be a discovery that each cell of the playing field is designated by two symbols - a letter and a number, for example, A5 - “missed”, I10 - “hit”, Z7 - “killed” ". Only in BASIC it is customary to use integers as indices. A real-life example of the use of two-dimensional arrays is cinema or theater tickets, which have two coordinates for each viewer - a row and a seat."

Similar arrays are described in BASIC using the same dim operator, after which the two dimensions of the array are indicated in parentheses - the number of rows and the number of columns.

PROGRAMMING LANGUAGE VISUAL BASIC. PROGRAMMING BRANCHES

Branching in Visual Basic is organized using:

  • conditional IF statement;
  • built-in IIF function;
  • CASE selection operator.

To test one condition and execute a statement or block of statements, use conditional statement IF...THEN. This operator can be used with different syntaxes: single-line (linear) and multi-line (block).

The linear operator has the following syntax:

If<условие>Then<операторы!>

The block operator has the following syntax:

If<условие>Then
<блок операторов 1>
End If

If the given condition is True, the statement block is executed, otherwise the statement block2 is executed. If the Else clause is not specified, ifWhen the condition is met, control is immediately transferred to the next operator after If.

The If statement can be nested, that is, located inside statement blocks. To test more than one condition and execute one of several blocks of statements, use an extended conditional statement of the form:

If<условие 1>Then
<блок операторов 1>
Else<условие 2>Then
<блок операторов 2>
Else<условие n>Then
<блок операторов n>
End If

To select one of the values ​​depending on the satisfaction or failure of some condition, use the IIF conditional function, which has the following syntax:

IIF(<условие>, <значение1>, <значение2>)

This function returns value1 if the condition is true and value2 if the condition is false.

You can use a Boolean expression as a condition that returns True or

False, or any arithmetic expression (a zero value is equivalent to False, and a non-zero value is equivalent to True).

SELECT CASE statement used to test a single condition and execute one of several blocks of statements.

Operator record format:

Select Case<проверяемое выражение>
Case<список выражений 1>
<операторы 1>Case<список выражений 2>
<операторы 2>Case<список выражений 3>
<операторы 3>
Case Else
<операторы группы Else>
End Select

The expression being tested is evaluated at the beginning of the Select Case statement. This expression can return a value of any type (boolean, numeric, string).

A list of expressions is one or more expressions separated by a standard delimiter character (semicolon).

When the operator is executed, it is checked whether at least one of the elements of this list matches the expression being tested.

These expression list alimony may take one of the following forms:

  • <выражение>- checks the coincidence of a given expression with one of the expressions - list elements;
  • <выражение 1>That<выражение 2>- checks whether a given expression falls within the specified range;
  • < Is <логический оператор> < выражение>- checks the fulfillment of the specified condition for a given expression.