Basic Course of VBS

Time:2019-9-28

VBScript (Microsoft Visual Basic Script Editon). Microsoft Visual Basic Script Editon. Microsoft Visual BASIC script version. As its literal information reveals, VBS (further abbreviation of VBScript) is a scripting language based on Visual Basic. Microsoft Visual Basic is a set of visual programming tools produced by Microsoft, whose grammar is based on Basic scripting language. Instead of compiling into binary files, the source code is directly interpreted and executed by the host. In short, the program you write does not need to be compiled into. exe. Instead, it sends the source code of. VBS directly to the user, and the user can execute it.

The first one (six in total)

I know what tool rookies are most concerned about is to develop VBS program. The answer is: Notepad. I’m not kidding. In fact, any text editor can be used to develop VBS, but Notepad is brought by the system itself, which is easier to find. Nevertheless, I suggest you download one. A professional text editor, because these tools can provide “grammar highlighting” and other functions, more convenient to develop, with which you like, I prefer Edit Plus Script Cryptor is also good OK, let’s first write a VBScript program warm-up.

REM input and echo your name 
Using InputBox and MSgbox functions 
'(You don't have to write to the source code above and in this line, but the code that runs below) 
Dim name,msg 
Msg= "Please enter your name:" 
name=Inputbox(msg) 
Msgbox name

Enter the program list above into notepad, and save it as a file with the extension of. VBS (if there is no “. ***” on the file name, open the control panel – folder option – View – cancel the extension of the known file type). Then double-click to observe the results of the operation.

Note: In order to make you learn better, it is recommended that you enter the program list and try not to copy – > Paste it.

Let me explain this program. The first and second lines start with “REM” and “REM”. These two things have the same function. The following words are annotations, that is to say, nothing is done after the symbol. They are just used to explain the function of the program, copyright information and so on. The annotation line is one of the most important parts of the program. First, although it is not necessary, it is good for others to read the source code and analyze the source code themselves. A good habit is to add clear and concise comments where necessary.

Dim is used to declare a variable. In VBS, the type of variable is not so important. That is to say, VBS will help you automatically identify the type of variable. Variables need not be declared before they are used. The program will dynamically allocate variable space. In VBS, you don’t need to consider whether name stores an integer or a decimal number (the scientific name is “floating point”). So the third line can be deleted and the effect will not change, but I strongly oppose it. The basic principle of a variable is: first declare, then use. Variable names start with letters, you can use underscores, numbers, but you can’t make them. Words already defined in vbs, such as dim, cannot be pure numbers.

The next line is called “assignment”. The “=” is an assignment symbol, not an equal sign in mathematics, although it looks the same. This is an orthodox understanding. You have to understand that there is nothing wrong with equalization. The left side of the assignment number is a variable, and the right side is the value assigned to the variable. After assignment, the variable MSG is equivalent to “please” in the program. Enter your name: “This string, but when MSG is reassigned, the original value will disappear. Not only the string, but also any other variable, such as a = 2, B = 12.222, etc.

Next, Inputbox and MSgbox are built-in functions of VBS. A function is equivalent to a “black box” with input (parameter) and output (return) values. You don’t need to know how the function works. Just know what the function can do, we can also define our own functions, but that’s to wait until later. We just need to understand that a function can have return values or no parameters or no. For example, Inputbox is a function with return values. We use variables on the left of the assignment number to “catch” the return value of InputBox, which is what you enter. In parentheses on the right of the inputbox, there is a list of parameters, and each parameter uses “. Separation, each parameter has different functions. For example, the first parameter will be displayed in the prompt. We pass the variable MSG as the first parameter to the Inputbox function, and msg= “Please enter your name:”, so we will see in the prompt bar of the dialog box that “Please enter your name:” The second parameter is the dialog box label. Inputbox also has many parameters, such as adding a “” after the “name” and then entering a random string of characters (string, a string wrapped in double quotation marks is called string) and running. Look at the results. You will find that the text box for input has a default value, which is the function of the third parameter.

Msgbox function is a function for output. There is no special output function in VBS (print in BASIC, printf in C), so we can only use the dialog box to observe the output. There is only one necessary parameter for Msgbox, which is the content to be output. In this case, we do not need to pay attention to the return value of msgbox. About Msgbox and I. Nputbox, which we will discuss later, is just warming up today, so far.

Main points:

1) Annotation lines (starting with REM or `REM’do not work in programs, but they make it easier for others to read your programs.
2) A variable is like a box, or a code, which can represent what you want to represent. Variable assignment uses “=”
3) Characters wrapped in “” are called “strings”.
4) The function is like a “black box” with parameters and return values, which can be accepted by the variables on the left of “=”
5) The Inputbox function pops up an input dialog box, while Msgbox is used for output.

Homework:

1) Test the third parameter of Inputbox
2) Write a program to output your age
3) Write a program and input it three times. Enter your name and your parents’name (requiring prompts), and output it three times.

Chapter 2 (six in total)

Today we will talk about various quantities and basic operations.

Let’s start with constants, which is relatively simple.

What is a constant? A constant is an invariable value.

Constants are divided into two kinds: the first is natural constants. This is called because they are constants themselves. How do you change the value of 21? It’s always 21, it can’t be 46.

If you use “21 = 46” in your program, such a statement will cause an error. Similarly, strings are constants (remember strings? A string wrapped between “). Hello World is an example. If you use”Hello World”=”Bye”, such a statement can also cause an error. Are there more examples of natural constants?

The second is our own defined constants, which also use code names and are assigned, but the difference with variables is that they are assigned at the time of definition and can not be changed later. An attempt to change will cause an error. To define a constant, we use the keyword “const” (the meaning of the keyword). Thought is that the system defines words with special functions and cannot be used as variable names or constant names.

The format is: const constant name = constant value.

For example:

  const PI=3.1415962
  const NAME=”developpaer”

In this way, we define two constants, PI and NAME. Generally speaking, the names of constants are all capitalized, but they can also be used, as you like. It is a good habit to define some values that do not need to be changed in the program as constants, which can prevent unnecessary accidents. In addition, using custom constants can also reduce your workload. Such as:

  msgbox “Hello World”
  msgbox “Hello World”
  msgbox “Hello World”
  msgbox “Hello World”
  msgbox “Hello World”

This program outputs Hello World five times. If you want to change the output to Bye-Bye, you must modify the whole program. Of course, you can modify it five times manually, but if you want to output 1000 times, constant can solve this problem for us.

  const hw=”Hello World”
  msgbox hw
  msgbox hw
  msgbox hw
  msgbox hw
  msgbox hw

So when you want to modify the output, you just need to modify the value of hw.

Well, now let’s look at the first important “cornerstone” of programming: variables. The best way to explain variables is, I think, a box. A variable is like a box. It can only contain one thing. When you want to put something else in it, you have to take out the original thing. The box is named, when you are in the process. When variables are used in the sequence, the system opens the box to take out the contents and let them participate in the processing, not the box. Some languages rely heavily on what’s inside the box to find the right “box” (such as C language), but VBS provides me with a “magic box” that can automatically scale. We don’t need to use it. Concerned about what’s in the box, VBS automatically adjusts the size of the box. For example:

  Dim a1,a2,a3
  a1=14
  a2=12.23
  a3=”Hello”

It doesn’t have to be as troublesome as C: or the formal declaration of VB (VB can or can not be used):


int a1; Dim a1 as integer 
float a2; Dim a2 as Double 
char* a3; Dim a3 as string 
a1=14; a1=14 
a2=12.23; a2=12.23 
a3="Hello"; a3="Hello"

Um… Gone too far…

What’s the use of variables? Wow, that’s great. Simplest, you can’t determine the value of variables when the program runs. For example, the program we wrote in the previous lesson, you can’t determine what InputBox returns (remember the return value of Inputbox? That’s what you input), so you can’t deal with all kinds of things. But we use the name box to put the user’s name in. When we use it, we only need to know the name of the box. The system will open it by itself and take out the contents. For example, we write a program to calculate the area of a rectangle, such as this program to be sent to elementary school. Raw use:


dim a,b,s 
a=15 
b=12 
s=a*b 
msgbox s 

In this way, we can find the rectangular area which is 15 long and 12 wide. Is it simple? Of course, this program can also be written as follows:


  dim s 
  s=15*12 
  msgbox s 

It looks like the program is much shorter and saves memory, but neither is encouraging. Why? See below.
Now, our programs need to look like they are. Whose programs have to be written and someone else has to modify the source code to use them?
So, we have to accept user input, remember? InputBox function.
The revised procedure is as follows:

dim a,b,s 
A = InputBox ("Please enter the length of the rectangle:") 
B = InputBox ("Please enter the width of the rectangle:") 
s=a*b 
msgbox s

OK, no matter what data the user enters, we can calculate the area of the rectangle. If you use s = 15*12, can you change it? Of course not.

I think you have found that the mathematical calculation in VBS is no different from real arithmetic, +, -,*, /, (), [], {} are the same usage, for example:


dim ans 
ans=12+32/4+[(23-10)*2] 
msgbox ans 

The four rules of arithmetic work equally well in programming, and you can regain the fun of elementary school in programming (you hate math? Don’t learn computer).

An interesting operator in programming is “mod”, which is called “redundancy operator”, which is to get the remainder of a division, for example.

  dim a
  a=16 mod 5

Do you know how much a equals? Bingo! Yes, that’s 1. Because 16/5 = 3… 1, the result of mod calculation is 1.
Another operator is “^” (a small arrow on the keyboard “6”), which means “power multiplier” (or “square”), for example:


dim a,b,c 
a=2 
b=a^2 
c=a^3 
msgbox b 
msgbox c 

Then B = a * a = 4, C = a * a * a = 8

Well, let’s not talk too much at one time. That’s all for this time. Now let’s sum up.

Main points:

1) Constants are classified into natural constants and custom constants. The values of constants can not be modified.
2) Variables are like boxes. We don’t care what’s in the box, but we must know the name of the box.
3) Four arithmetic operations are no different in programming.
4) MOD is a redundancy operation

Homework:

1) Write a program to calculate the area of the circle. The radius is given by the user (using Inputbox) PI value is 3.14159.
2) Write a program to get 20/3 of the remainder

Chapter 3 (six in total)

First of all, let me settle a few questions about the last course.

First, that residual problem, 16/5 = 3… 1. Because I changed the front part and the back part. I’m sorry.

Second, please take a look at the list of procedures:

  (1)

Dim a,b,c 
A = InputBox ("a is:", "input radius") 
B = Inputbox ("b is:", "input radius") 
c=a*2+b*2 
msgbox c

This input is 6 when 1 or 2

  (2)

Dim a,b,c 
A = InputBox ("a is:", "input radius") 
B = Inputbox ("b is:", "input radius") 
c=(a+b)*2 
msgbox c

This input is 24 for 1 and 2.

Why is it different? Mathematically, c=(a+b)*2 and c= a*2+b*2 are equivalent, and so is in VBS. The problem lies in “+”. In VBS, + not only means to add a sign, but also means to connect two strings, such as “Hello”+ “World”= “Hello World”. Have you learned? Do you remember the return value of InoutBox function? Strings! That shows the problem, in programming. “1” is not equal to (<>) 1, and “1” is a character, while “1” is a number, so a and B are string variables, “1”+ “2”= “12”, which is like when we joke with partners when we ask them 1+1=? We always laugh and say “wrong, should be 11”. But why, can a*2 not make mistakes? At this time, VBS is more intelligent performance, if the content of the string is a number and to him. If the string represents a number but does not participate in the mathematical operation, it is treated as a string. So you see a + B = 12, when the result of a + B (12) is a string, when it multiplies by 2, it is forced to convert to the number 12, so I get the result 24.

How to modify this program? We need to use another built-in function: int, the function of the int function is to convert the input value into an integer value, so we modify it:

c=(int(a)+int(b))*2

This means that if you pass a as a parameter to the int function, the int function will return that integer (your input value), and then let the return value participate in the operation, so you get the correct answer. So if you use the InputBox function in the future, you’d better use the int statement to process it: for example, C = int (c)’c is your own variable.

In addition, there is a function: CDbl. If you want to convert a character to a number, but don’t take the whole time, use this:

Dim a,b,c,d,e 
  A = InputBox ("a is:", "input radius") 
  B = Inputbox ("b is:", "input radius") 
  c=CDbl(a) 
  d=CDbl(b) 
  e=(c+d)*2 
  msgbox e

When you enter 1.2, 1.3, you will output 5.

The above examples can also be synthesized as follows:

Dim a,b 
  A = CDbl (inputbox ("a is", "input radius") 
  B = CDbl (Inputbox ("b is", "input radius") 
  Msgbox (a+b)*2

When you input 1.2 and 1.3, you still get 5. However, this writing is more suitable for friends who have studied VBS for a period of time.

Do you think this course is a bit boring? Well, that’s true for the variable and operator parts, but just practice more. This time, let’s write something really interesting: process control statements. This part starts with real programming.

First, the judgment structure is introduced.

Before that, let’s introduce a simple variable type: Boolean, which has only two possible values: True, Flase, which is true or false. This variable is useful in some situations (e.g. “switch”). We define a Boolean variable in the same way as other variables, and assignment is the same, for example:

  dim a,b
  a=true
  b=false 

Note that true is different from “true” (double quotation marks), “true” is a string, true is a Boolean value, and must not be confused.
Back to the if statement, let’s take a look at the simplified version of the if statement: if judgment then statement body. Let’s take an example:

  dim a,b
  a=12
  b=13
If b > a then msgbox “B is greater than A”

Let’s just look at the last line. The expression a > B has a return value, which is Boolean type. Because there are only two possibilities for this expression: B is greater than a and B is not greater than a, so there are only two possibilities for this expression, that is, true or false. The if statement determines whether the return value of this expression is true or false, and if it is true, it insists. If the statement after line then is false, it will not be executed. You change the value of a to 14 to see if the dialog box will pop up again.

When we want to execute a multi-line statement after judgment, we need to solve it with a statement block, which can be called block if.

   dim a,b
  a=12
  b=13
  if a<b then
Msgbox “A is less than B”
Msgbox “B is bigger than A”
  end if 

Two msgbox functions are sandwiched between if and end if. This part is the statement block. Before each statement in the block, please leave 4-8 spaces (a < Tab > key). This is not necessary, but it is a good habit to see the structure of the program. So we can run more than one statement. Please note that if… Then… End if these three statements. OK, I put a question, input a number, output “error” if less than 100, and output “correct” if more than 100. I have two versions of the program here:

dim a 
  A = InputBox ("Please enter a number greater than 100") 
  A = int (a)'inputbox returns a string, which we turn into an integer:' 
  If a > 100 then msgbox "correct" 
  If a < 100 then msgbox "error"

There’s a simpler one.

dim a 
  A = InputBox ("Please enter a number greater than 100") 
  A = int (a)'inputbox returns a string, which we turn into an integer 
  if a>100 then 
  Msgbox "right" 
  else 
  Msgbox "error" 
  end if

See the addition of an else. The purpose of an else is to execute when the expression to be judged is false. This allows the program to handle two different situations. Don’t forget to end with an end if.

Hey hey, I’m a “pervert.” Now I want you to deal with three situations, <100,= 100,> 100, and write in an if structure. What do you do? I’ll give you the answer:

dim a 
  A = InputBox ("Please enter a number greater than 100") 
  A = int (a)'inputbox returns a string, which we turn into an integer 
  if a>100 then 
  Msgbox "right" 
  else if a=100 then 
  Msgbox: "Boss, are you kidding me?" 
  else 
  Msgbox "error" 
  end if 
  end if

The other if statement can appear many times in the if structure in order to judge different situations flexibly (if you want to judge too much, please use the “select structure” later), when all the elseifs have been processed, and when they do not meet the situation, then execute the statement in the else. Another example:

Dim a,b,c,d 
  A = InputBox ("a is:", "input radius") 
  B = Inputbox ("b is:", "input radius") 
  D = Inputbox ("Answer:", "Enter Answer") 
  C = a * 2 + b * 2'There's no problem here, it's automatically converted. 
  if d=c then 
  Msgbox "how smart you are" 
  else 
  Msgbox: "Hello Pig Head's own question is not yet!" 
  end if

Ha-ha, no matter how correct your answer is, you are a pig’s head, not me playing with you, or the return type of InputBox at the beginning of the article is playing with you. D is the return value of inputbox, it is a string, and C is the result of an integer calculation, it is an integer. A string is not equal to an integer anyway, although They are literally the same: “8” <> (not equal to sign) 8, so the value of if’s judgment is always false, always execute the statement in the else section. We can modify it this way.

Dim a,b,c,d 
  A = InputBox ("a is:", "input radius") 
  B = Inputbox ("b is:", "input radius") 
  D = Inputbox ("Answer:", "Enter Answer") 
  d=int(d) 

  'Here we take the value of d, turn it into an integer, and put it back in the box of'd'. 
  c=a*2+b*2 
  if d=c then 
  Msgbox "how smart you are" 
  else 
  Msgbox: "Hello Pig Head's own question is not yet!" 
  end if

That’s a success. It’s also a nuisance of Inputbox functions. There’s no other good way to input vbs.

When it comes to if, we have to talk about logical operators. Today, we introduce two kinds of logical operators. After “and” and “or” have learned the if statement, let me give you an example. You can see it at a glance.

dim a,b 
  A = InputBox ("input a number > 10") 
  B = InputBox ("enter another number > 10") 
  a=int(a) 
  b=int(b) 
  if a>10 and b>10 then 
  Msgbox "right" 
  else 
  Msgbox "error" 
  end if

This program allows you to enter two values, which must be greater than 10, and output errors as long as one is not greater than 10.

dim a,b 
  A = InputBox ("input a number > 10") 
  B = InputBox ("enter another number > 10") 
  a=int(a) 
  b=int(b) 
  if a>10 or b>10 then 
  Msgbox "right" 
  else 
  Msgbox "error" 
  end if

This program allows you to enter two values, as long as one is greater than 10, then return to success. In fact, and and or are very well understood. I read the sentence “if a > 10 or b > 10 then”, as in Chinese: “If A is greater than 10 or B is greater than 10, then…”, is that very easy to understand?

OK, let’s look at a new structure again. After learning this, today’s class will be over. It’s midnight and I’m exhausted.

When your program is dealing with many different judgments, elseif. then makes the program look cluttered, so there is a select case structure to deal with this situation. The grammatical structure of select case is very simple:

Selectcase variable name
Case value
Sentence
Case value
Sentence
  case else
Sentence
  end select
Let’s give an example to illustrate it very simply:

dim a 
  A = InputBox ("enter a value of 1-3") 
  A = int (a)'Handles the problem of InputBox returning strings 
  select case a 
  case 1 
  Msgbox "one" 
  case 2 
  Msgbox "two" 
  case 3 
  Msgbox "three" 
  case else 
  Msgbox "input error" 
  end select

This example converts the three Arabic numerals 1, 2, 3 into Chinese capital numerals. This program is written in the form of if… elseif as follows.

dim a 
  A = InputBox ("Please enter a value of 1-3") 
  a=int(a) 
  if a=1 then 
  Msgbox "one" 
  elseif a=2 then 
  Msgbox "two" 
  elseif a=3 then 
  Msgbox "three" 
  else 
  Msgbox "input error" 
  end if

Well, please, just select.
OK, that’s all for today. To sum up:

Main points:

1) Inputbox returns a string, not a number, which must be converted to a number in the form of a = int (a)
2) there are only two values of bool variable: true and false.
2.5) If the expressions on both sides of and are true, then one of the expressions on both sides of true. or returns true, and the other returns true.
3) Format of if statement
4) The format of select… case

Homework:

1) Use three bool values to store whether your three siblings are male (hint: sister1male = false)
2) Given a number greater than 10 and less than 20 output “correct”, otherwise output “wrong”
3) Input 12, or 15, output “correct”, otherwise output “wrong”
4) Convert all positive integers less than 5 into larger numbers in China
5) Design a program to apply today’s knowledge.

Chapter 4 (six in total)

Hello, everybody. Today I’m going to write Chapter 4: Cyclic Structure

First of all, we need to know how many times we buy and sell so that we can control the input. Number of times, but this design is very inefficient, every day to redesign the program. Assuming five transactions today, the following is the source program:

dim sum 
  Sum = 0'initialization variable 
  Sum = sum + int (inputbox ("please enter the transaction volume") 
  The form of'sum = sum + X'is to take out its own value, perform an operation, and then put back itself. This method is very useful. 
  'Here we use function nesting to transfer the return value of the InputBox directly to the int function and convert it into an integer. 
  Sum = sum + int (inputbox ("please enter the transaction volume") 
  Sum = sum + int (inputbox ("please enter the transaction volume") 
  Sum = sum + int (inputbox ("please enter the transaction volume") 
  Sum = sum + int (inputbox ("please enter the transaction volume") 
  msgbox sum

See, I designed the program by copying the calculation process five times. This program can be used in places where the number of transactions is small, such as automobile exchanges. If we put it in supermarkets, we should copy and paste it thousands of times. What we are talking about today can overcome this shortcoming. First, let’s talk about Do… Loo. P statement.

The structure of do… loop seems very simple: do… loop, that’s all, it keeps executing the sentences between do and loop (the scientific name is loop body) and never stops. For example:

  do
Msgbox “This information will continue to recur. To stop the program, please use Task Manager (Ctrl + Alt + Del) to abort the Wscript process”
  loop

Running this program, when you click on one dialog box and immediately come out another, you can never click on it, there will always be the next. Who will run such a program? Unless it’s messing up someone else (I’ve done that), there’s another statement in the do… loop structure: exit do, which will terminate the loop and jump to the statement after the loop. Continue to execute. For example:

Dim a'Note: Constants do not need to be declared in dim, otherwise they can cause errors 
  Const pass= "123456"'This is a string wrapped in ". Set the password to be constant and unchangeable 
  do 
  A = InputBox ("Please enter your password") 
  if a=pass then 
  Msgbox "Password Check Successful" 
  exit do 
  end if 
  loop

This program will keep asking you for your password until you have entered the correct password. (If can be nested in another if or in a loop, so indentation must be used to distinguish the various parts of the program.) This program is very classic. Early programs did this. But we are Ha. Cker, so we understand the security of the system. This infinite authentication program can easily be exhaustively cracked. We need to limit the number of authentication. The modification procedure is as follows

dim a,ctr 
  CTR = 0'Set the counter 
  Const pass= "pas123_"'The one above is a weak password, this time it's stronger. 
  do 
  A = InputBox ("Please enter your password") 
  if a=pass then 
  Msgbox "Authentication Success" 
  exit do 
  else 
  if ctr=3 then 
  Msgbox "Up to the authentication limit, the authentication program is closed" 
  exit do 
  else 
  CTR = CTR + 1 Note: This sentence is an assignment sentence. Read it from right to left, that is, add CTR to 1 every time you make a mistake, and then put it back in ctr, so that the constant is added to 1. 

  Msgbox "authentication error, please check password" 
  end if 
  end if 
  loop

Run this program for a try. When you make three errors, you stop asking for the password again and shut down the program. The program telnet authentication uses to limit the number of times is much the same as this. Note that the nested if statement, please read this program carefully, it may be difficult to understand, and try to design similar programs yourself. Program.

In fact, in order to add validation function to do… loop, we don’t have to use if, we can directly use do. Let me introduce the key word “while”, which can be placed behind do or loop, and then an expression, when the value of the expression is true (the expression holds), it runs the loop body. Let’s take a look at the repair. The revised procedure“

dim a,ctr 
  ctr=0 
  const pass="pas123_" 
  do while ctr<3 
  A = InputBox ("Please enter your password") 
  if a=pass then 
  Msgbox "Authentication Success" 
  Msgbox 
  exit do 
  else 
  CTR = CTR + 1'Increase the error authentication count once if the password goes wrong 
  Msgbox "authentication error, please check password" 
  end if 
  loop

The function of this implementation is exactly the same as that of the previous example.

dim a,ctr 
  ctr=0 
  const pass="pas123_" 
  do 
  A = InputBox ("Please enter your password") 
  if a=pass then 
  Msgbox "Authentication Success" 
  Msgbox 
  exit do 
  else 
  CTR = CTR + 1'Increase the error authentication count once if the password goes wrong 
  Msgbox "authentication error, please check password" 
  end if 
  loop while ctr<3

The function is the same. Why put it behind loop? You can change the value of CTR to 3. The program behind do will exit directly, while after loop it will allow an authentication, and it will end only after loop. Contrary to while, until is the same as while, but it only has the value of false when the expression behind it.( The loop is executed only when the expression is not valid. Please try it out by yourself.

OK, let’s look at another loop structure, for…. Next, which is based on counting and is the most common loop structure in programming.


  dim i 
  for i=0 to 5 
  msgbox i 
  next 

See? Every output of I is incremental, but we didn’t explicitly point out that I is incremental. When I reaches 5, the loop ends. Because it starts from 0, the loop body executes six times. This is very important. Most things start from 0 instead of 1. This program can also be written as well.

The form of do:

dim i 
  i=0 
  do while i<5 
  msgbox i 
  I = I + 1'Because do cannot count automatically, it must be added manually 
  loop

Well, for is better. For is useful in programming. Let’s give another example, by the way, nested loops.

dim i,j 
  for i=1 to 9 
  for j=1 to 9 
  STR = STR & I * J & "'" & is a symbol of and parallel strings 
  Next'Each next corresponds to one for 
  next 
  msgbox str

(The result is one-time, if you need to appear in turn, bring msgbox STR up to next)
See if the results remind you of your math teacher in elementary school (ugly face).

Note that there is a “big” for and a small for. When a small for completes a cycle, the big for is executed only once (in other words, the big for is executed once, and the small for is executed nine times), so it has been executed ninety-eight times altogether. Let’s modify the source program:

dim i,j 
  for i=1 to 9 
  for j=1 to 9 
  str=str & i * j & " " 
  Next'Each next corresponds to one for 
  STR = STR & vbCrlf'vbCrlf is equivalent to the return key on the keyboard, because you can't input on the keyboard, so the system defines a default constant. 
  next 
  msgbox str

After this operation, the output is divided into multipliers, and every small for runs once, it changes to a new line (through vbcrlf).

In addition, I saw many people in the forum asking, “What tools are VBScript to use?” I was very angry. In the first article, I have explained: edit the source code with notebook, and save it as a program with the extension of. vbs. Please do not use other tools to write, otherwise it will easily lead to errors.

In addition, a domestic garbage software “Super X Hegemony” has seized the extension of vbs, please unload that garbage.
Let’s sum up:

Main points:

1) Use of do.. Loop and exit do
2) while executes the loop body when the expression true, until conversely
3) For… next is a counting loop, each time the counter is incremented.
4) The Function and Writing of Nested Cycles
4.5) & for connecting strings
5) vbCrLf is equivalent to the return key on the keyboard

Homework:

1) There is a question in the classic mathematical book “Nine Chapters of Arithmetic” in our country: one hundred chickens for one hundred dollars, five cocks for one cock, three hens for one hen and three chickens for one chicken) to find out how many ways to buy these chickens. Male: $5, mother: $3, small: $1 for 3, let you find a total number of sales methods (how to match the mother and father). Please use the cycle to solve this problem.

Chapter 5 (Six in all)

Today we come to the last topic of language itself: arrays.

To understand the concept of “array”, I think another translation is easier for learning: “array”. Yes, an array is an array, an array of data. The simplest example is a database system. Suppose you want to store 20 students’English scores. If you don’t use an array, you need to create 20 different variables. An array is a set of data (or n arrays) of the same type (important!), which is used to store related quantities. The simplest array is a one-dimensional array. Let’s learn about it first.

What is a one-dimensional array? Under three dimensions, you can use geometric knowledge to understand the concept of “dimension”. One dimension is equivalent to a line, two dimension is a rectangle, and three dimension is a cuboid. I know it’s very abstract. Let’s first give an example of one-dimensional array.

Dim a (9)'Start from scratch 
  for i=0 to 9 
  A (i) = I'Fill in each array element 
  Msgbox a (i)'output array elements 
  next

We can see that the method of defining an array is no different from that of defining a variable. The same way is to use dim statements. The method of defining a one-dimensional array is as follows:

Dim array name (element number), here we should note that the number of elements defined here is always one less than you want, because an array starting point is data 0 rather than 1, so you must be careful: you need 10 data, define “9”, need 100 to define 99, and so on. As independent variables, you can use them as independent variables. The amount of array elements may be irrelevant. For example, the first element stores your age and the second element stores the sales of watermelon this year. But this is not encouraged, even unacceptable. Don’t do that. Define independent variables. The for statement can be considered a powerful tool in arrays, remember for? It adds up a variable that we can use to read or fill elements in arrays in order. The example above is a simple thing (BASI again). In C language, the difficulty of arrays is how to tamper with these loops and let them run as you want. Wait until two-dimensional arrays, let’s see how to fill arrays manually.

If you can’t imagine this, you’ve learned it for nothing.

There are eight students in dim name (7), str'. The STR variable is used to store them as a string for output. 
  for i=0 to 7 
  Name (i) = InputBox 
  str=str & " " & name(i) 
  next 
  msgbox str

In this way, we have a small database whose data can be arranged as follows:
  name(0),name(1),name(2)…..name(7)

See, so I said that we can think of it as a “line”, and when we learn how to manipulate files, we can export them to files. One-dimensional arrays have many uses. Let’s look at a complicated example. We need to store three kinds of data: names, height and grades of students, because the names are Strings, while height may be floating-point numbers (with decimal points) and grades may be integers, so we can’t store them in an array (don’t forget, number organizations can only store the same kind of data), so we need to build three arrays, the following is the routine:

Dim name (2), high (2), mark (2)'defines three groups to store three people's names, height and scores, respectively. 
  Dim ctr'counter 
  for ctr=0 to 2 
  Name (ctr) = InputBox 
  High (ctr) = InputBox ("please enter number" & "ctr + 1" & "individual student's height") 
  Mark (ctr) = InputBox 
  next

OK, we have filled in the data, now our small database can only be entered in order, we want to make it look like a dot, let’s design the query function for him:

'Next to the above procedure 
  Dim cname, temp's name to query, and a temporary variable to store the location of the data 
  CNAME = InputBox ("Please enter the name you want to query:") 
  For CTR = 0 to 2'traverse all members of the name array to find the name to query 
  if name(ctr)=cname then 
  Temp = CTR'Record data location 
  Exit for'Exit the loop, just like exit do 
  End if'Don't forget end if 
  next 
  Msgbox "name:" & name (temp) & "height:" & high (temp) & "score:" & mark (temp)

Hey hey, interesting, in fact, in this program, that temp variable is completely unnecessary, just to explain the problem more clearly. Because after exit for, the value of CTR variable will not change. It stores exactly the position of the corresponding data in the array. This temp variable is written to take care of the friends who want to learn C++ later (C++). New variables can be declared in the for statement, which is valid only in this for structure, so they can’t be accessed externally. That is to say, it can be simplified as follows:

dim cname 
  CNAME = InputBox ("Please enter the name you want to query:") 
  for ctr=0 to 2 
  If name (ctr) = CNAME then exit for'because only exit for does not require block if 
  next 
  Msgbox "name:" & name (ctr) & "height:" & high (ctr) & "score:" & mark (ctr)

This is the most direct way. Read all the above source code carefully, then write a few programs, fully understand one-dimensional arrays and their applications and then look at the following two-digit arrays. Two-dimensional arrays seem to be a collection of one-dimensional arrays, just like “line product into surface”, two-dimensional arrays composed of N one-dimensional arrays, which is Beginners have a better understanding of the method (more precise is to use the concept of “number” to understand, because more than four-dimensional arrays are more difficult to explain with Euclidean geometry concept). Two-dimensional arrays are very well explained, let’s see:

Dim a (2,2)'Starting from scratch, there are 3 X 3 = 9 data 
  Dim i, J'requires two counters 
  for i=0 to 2 
  For J = 0 to 2'Use nested loops 
  a(i,j)="X" 
  next 
  next

We created such two-dimensional arrays (those “,” which I used to divide elements, do not exist), understand the two-dimensional arrays? Not clear, let’s talk about it again.

No. 012
  0 X,X,X
  1 X,X,X
  2 X,X,X

Does a two-dimensional array look like a rectangle? (You can’t see such a rectangle in memory, but it’s easy for you to understand). Each data has a number and is located by two numbers. This is very similar to (very similar) looking for a lattice on a chessboard. We use “vertical coordinates” like “C6” and “A2”. Bit, yes, “Vertical coordinates”, very precise. When we use a two-dimensional array element, we can use it as a common variable, as long as we specify the “locating point” of the array element, such as a (0) (1) = 1, B (2) (1) = “Hello”, and so on. It should be noted that two-dimensional arrays can only store elements of the same type, and superscripts.( The starting point is also zero. To calculate the number of elements in a two-dimensional array, just multiply the two subscripts + 1 (to get the actual value), which is very similar to finding the area of a rectangle.

If we need to store the same type of data, we can use two-dimensional arrays, for example, to store the names, nationalities and nationalities of five people, we can use two-dimensional data.

Dim info (4,2)'There are five people, and there are three types of data to be stored. 
  dim i,j 
  for i=0 to 4 
  for j=0 to 2 
  Dim opt'Defines a variable for storing data item prompts 
  Select case J'To determine what data should be entered 
  case 0 
  Opt= "name" 
  case 1 
  Opt= "nationality" 
  case 2 
  Opt= "nationality" 
  end select 
  Info (i, j) = InputBox ("please enter -& I + 1 & personal" & opt) 
  next 
  next 
  'Output is too much trouble. I'm too lazy to move. You know that's all.

This eliminates the need to define three one-dimensional arrays.

Multidimensional arrays (more than three-dimensional) are defined and used in the same way as two-dimensional arrays, but they are not well explained in Euclidean geometric space. Fortunately, we do not often use so many-dimensional arrays. Define a three-digit array: dim a (1, 2, 3)’with a total of 24 array elements.

Main points:

1) One-dimensional arrays are “lines”, two-dimensional arrays are “faces”, and three-dimensional arrays are “bodies” (multi-dimensional arrays are messy).
2) Subscripts of arrays start at 0
3) For loops play an important role in the application of arrays. Two-dimensional arrays need nested loops.

Homework:

Do you like the “Hundred Chickens Question” last time? We will do this kind of question which needs brainstorming in the future. We must do more for those simple practices! __________
1) Define an array of five elements, all random integers (random input), and ask them to be arranged in order from large to small.
2) There are two two two-dimensional arrays a (4,4) and B (4,4) (element values are arbitrary), exchanging two arrays (all element values of original a become b, all element values of B become a)

Chapter 6 (Last)

Today, we will learn the last part of the basic chapter: custom functions and processes. We deal with functions every day. Inputbox () is a function, msgbox is a function, int (is also a function… these functions are built-in in the system, we can only change them. Today, I will teach you how to make a function by ourselves.

First of all, we need to understand why we use functions, let’s use “examples” to talk about, first look at an example: Give two numbers, output the larger one.

dim a1,a2,b1,b2,c1,c2 
  A1 = 2: A2 = 4':"allows you to write multiple statements on one line 
  b1=32:b2=67 
  c1=12:c2=898 
  if a1>a2 then 
  msgbox a1 
  elseif a1<a2 then 
  msgbox a2 
  end if 
  if b1>b2 then 
  msgbox b1 
  elseif b1<b2 then 
  msgbox b2 
  end if 
  if c1>c2 then 
  msgbox c1 
  elseif c1<c2 then 
  msgbox c2 
  end if

How troublesome, we copied the same comparison process several times. When the early languages were not structured (without processes and functions), programmers did so. In those days, there was no clipboard. Everyone re-entered the code. Later, the work was simplified:

dim a1,a2,b1,b2,c1,c2 
  a1=2:a2=4 
  b1=32:b2=67 
  c1=12:c2=898 
  msgbox co(a1,a2) 
  msgbox co(b1,b2) 
  msgbox co(c1,c2) 
  Function Co (t1, t2)'We use function to define a new function. 
  if t1>t2 then 
  Co = T1'returns the result by the method of "function name = expression" 
  elseif t2>t1 then 
  co=t2 
  end if 
  end function

Here we use a new keyword: funciton, which represents the beginning of a new function in a format:
Function function name (parameter 1, parameter 2… parameter n) list can be empty, but parentheses can not be omitted, parameters between the use of “,” partition.
  …
Exit function’End function, not necessary
  …
  end function

A function is a module that runs only when you call it. That is to say, when you write a function and then don’t call it in a program, then the function never runs. Generally speaking, we write programs according to:

Main procedure
  ……
  ……
  ……
Function 1
  ……
  ……
Function 2
  ……
  ……

Explain in detail: the most important parameters and return values in a function. Parameters are defined in () after the name of the function, using “,” “segmentation, we use parameters when we use”,”segmentation. Speaking of this, I remember one thing. Yesterday a friend sent me a message asking me:

  msgbox name1,name2,name3

What’s wrong with this? Why can’t you show three variables at the same time? That’s because you use “,”. This symbol indicates that three quantities you input are passed to the msgbox function as three different parameters. The msgbox () function only shows the first parameter, and the second parameter appears in the title bar. So you should use “,”.“ & “Or” + “Connect three string variables and pass them as the first parameter to the msgbox () function. When programmers say parameters, they often refer to”formal parameters”,”such a slang as real parameters”. Let me explain it. The abbreviation of”formal parameters”is”formal parameters”,”real parameters”is the abbreviation of”actual parameters”. A real parameter refers to when you call a function. Quantities passed to functions can make variables or constants (direct variables), such as 12,24 in CO (12,24) be arguments. Formal parameters are variables that you define when defining a function. These variables are used to “catch” the passed quantities, such as function Co (t1, t2t1, t2) are formal parameters.

In VBScript, parameter passing is a kind of value passing, not an address passing (it doesn’t matter if you don’t understand it, just learn the pointer of C language), so our parameter passing is actually a variable assignment, such as when we call Co (a1, a2), in fact, the program will perform one step: T1 = a1, T2 = a2. For the reason of value-passing address, VBScript can only return one value. Let’s first see what is called “return”. When a procedure calls another procedure (such as the main program calls a function), control goes to the called procedure, and when the process is finished, it goes back to the place where it was called and continues to execute. This is called “return”, which can be returned with a value called “return value” (which is a “popular” understanding). In vbs, it inherits the tradition of basic and returns with the method of “function name = return value”. This “return value” refers to an expression (in programming, everything is an expression, such as variable a, constant 0, Hello). C = 1 + 2 and so on. These are expressions. For example

One function is ht, and the way to return it is: HT = the value you want to return. Note: After returning, the following statement will not be executed.

I don’t need to talk about calling a function: variable = function name (parameter)

Sometimes we don’t need to return any value. At this point we can use a structure called “subroutine”. The difference between a subroutine or a process and a function.

It is: 1) no return value, 2) using subkeyword definition, 3) calling through Call. For example:

dim yname 
  Yname = InputBox ("Please enter your name:") 
  call who(yname) 
  sub who(cname) 
  Msgbox "hello" & CNAME 
  Msgbox "Thank you for reading my course" 
  Msgbox "This is the last lesson in the basics" 
  end sub

You must see, it’s very simple. To exit a process is the same as to exit a function: exit sub.

It should be noted that subroutines (processes) are a relatively special structure. Languages such as C do not have this concept. Everything in C is a function with no return value.

  In C language, just use the void modifier definition.

There’s nothing to talk about today, so that’s the end of the basics. Now you have the basic programming concepts (process-oriented structured programming), you can choose to learn another language (such as C or Pascal). Now the basics will be helpful. If you want to continue learning VBS or understand it in more detail. Programming in transition can continue to learn with me, but because my vacation is over, the update time may be slow, please forgive me. The preliminary plan is as follows:

Advanced articles:

Deep discussion of variables
_Variable Type
Effective Range of_Variable
Deep discussion of_array
_dynamic array
Deep discussion of_function
_array as function parameter
_Multiple return values
_string operation
_Other
_Basic Knowledge of Object-Oriented Programming (OOP)
_File Operation
_FSO object
_Other relevant parts
_VBS and Web Page
Embedding VBS in_HTML
VBS and Forms (Design Your Program Interface Wow!)

Practical Chapter

_virus programming
Socket Programming (TCP/UDP)
This is just a rough idea, I think there should be some changes, and you will read it in time. Today’s content, please practice more, homework is to review the previous courses. To friends who want to leave this course for further study: I wish you a smooth journey in learning programming again.
Editing common operators and functions of vbScript in this section
Basic operations
+ Digital Addition and String Connection
– Digital subtraction
* Digital multiplication
/ Digital Division
Mod Remainder
\ Quotient
& String connection
^ Secondary
= Equality
Unequal
>= greater than or equal to
> greater than
<= less than or equal to
Less than
Notfei
Ander and
Or or
Xor XOR
Circulation and decision-making
If… Then…
If… then… else if… then… else if… then… no
Otherwise, if not otherwise
Select case… group selection condition
  end select
Next counting cycle
While… wend conditional cycle (1)
Do while… loop conditional loop (2)
Do… loop while conditional loop (3)
Do until… loop conditional loop (4)
Do… loop until conditional loop (V)

Mathematical function

Absolute value of ABS
Sgn sign
Hex converts to hexadecimal
Oct to octal
Sqr square root
Int takes integers
Fix takes integers
Round takes integers
Log E-based logarithm
Sin sinusoidal function
Cos cosine function
Tan tangent function
String Processing Function
IsNull judges whether the object is empty
Len string length
Mid takes part of the string
Left takes part of the string from the beginning of the string
Right takes part of the string from the end of the string
Lcase to lowercase
Ucase conversion to uppercase
Trim clears spaces at the beginning and end of a string
Ltrim Clears the Starting Space Character of a String
Rtrim clears trailing spaces
Replace replaces part of the string character
Instr determines whether it is contained in another string (search from the beginning)
InstrRev determines whether or not it is contained in another string (searched from the end)
Space space character with arbitrary number of characters
String Any Character of Arbitrary Character Number
StrReverse inversion string
Split splits a string with a character

Data type conversion function

Cint Conversion to Plastic
Cstr Converts to Strings
Clng converts to long integers
Converting Cbool into Boolean Function
Cdate conversion to date function
CSng Conversion to Single Precision
CDbl Conversion to Double Precision

Date-time function

Date’s current date
Time Now
NOW Present Date and Time
DateAdd Increase Date
DateDiff two-day difference
DateSerial Date Setting
Datevalue Date Setting
Year in the current year
Month is in the current month
Day is today
Hour Now
Minute Now Minute
Second Now seconds
Timer’s midnight seconds from now
TimeSerial time setting
Timevalue time part
WeekDay Weekday Name
MonthName month name

Other functions

Array generates arrays
Asc character ASCII code
Chr ASCII Code Characters
Filter filter array
InputBox input window
Join merges elements in an array
MsgBox Information Window
Lower bound of Lbound array
Last Ubound Array

Instruction

Const setting constant
Dim defines variables or arrays
Erase Clear Array
ReDim redeclares arrays
Randomize starting random number
Rnd Gets Random Numbers
ASP objects
Session object
IsEmpty tests whether the Session variable exists
TimeOut Sets Session Variable Life Cycle
Abandon Forces Clearance of Session Variables
Application object
IsEmpty tests whether the Application variable exists
Lock Locks Application variables
Unlock unlocks Lock instructions
Cookies object
Expires Sets the Life Cycle of Cookies Variables
Connection object
Open opens the connection to the database
Execute opens the Recordset object
Close closes the Connection object
Recordset object
Move the record pointer to the first entry
Moelast moves the record pointer to the last one
Moenext moves the record pointer to the next entry
Move revious to move the record pointer to the previous one
Bof test is the beginning of Recordset
EOF test is the end of Recordset
Open opens the Recoreset object
Close closes the Recordset object
Subobjects of field read data
Number of fileds. count fields
Number of PageSize entries per page
Abslutepage is set to a page
Total pages of pagecount
Absoluteposition jumps directly to a record