Today, I will introduce VBS in general, hoping to give you an intuitive impression. In addition, the explanation is combined with the shortcut wizard tutorial (made by Li Yue) on the button official website to help you understand this stuff.
I. Overview
To allow computers to do things, they must be given instructions. The original commands were very complicated. They were all things such as 0011001. Later, the commands were simplified and closer to human languages. VBS (Microsoft (R) Visual Basic (R) Scripting) is also a high-level language. Compared with other advanced languages, VBS is easy to learn, it is a good entry for beginners of computer programming.
If the evolution from a low-level language to a high-level language is a strategic problem for humans to control computers, the specific setting variables and loops are the specific tactical problems for controlling computers. What we are learning now is the tactics to use VBS to make computers work for us.
2. Start with the simplest things.
Network selling script:
Vbs I
Vbs I = 10
While I> 0
SayString sells XXX, and the price is negotiable.
Vbs I = I-1
Delay10000
Endfor
Many people sneer at this script, and there are many ways to sell it continuously, some of which are several times simpler than this script. However, with the introduction, we will find that VBS provides us with a powerful tool. What are the meanings of these lines.
Vbs I // tell the computer that there is a variable named I.
Vbs I = 10 // This variable is 10
While I> 0 // when the variable is greater than 0, execute the following command until you see the first endfor.
SayString sells XXX, and the price is negotiable. // Key statement: output text
Vbs I = I-1 // This variable minus 1, and then the number after minus 1 is used as the value of I.
Delay 10000 // key statement: Pause for 10 seconds (10000 ms)
Endfor // return to the while
Effect: the product is sold for 10 times.
Now I need to make some effort to explain why so many troublesome things need to be done.
Use and importance of Variables
In fact, this program can be fully written
SayString sells XXX, and the price is negotiable.
Delay10000
SayString sells XXX, and the price is negotiable.
Delay10000
...... Repeated N times
SayString sells XXX, and the price is negotiable.
Delay10000
In fact, complex program segments can be restored to sequential programs. Using a lot of complicated stuff is not to show programming capabilities, but to make it easier for you.
We don't feel anything when we keep selling XXX, but when we sell YYY? Have you changed all of them? Or we have to repeat 1000 times, and then only repeat 500 times. Do we have to seriously count 500 times and then delete the subsequent ones? Of course, it's cool to compile this mini program. However, to improve development, we must develop large programs and rely on our variable friends.
A variable is actually a character code, just like your name, in order to differentiate the variables and let them work in different headers. You can use numbers, characters, and underscores to name a variable. However, you cannot use Chinese characters, spaces, or other strange symbols. In addition, numbers cannot be used as the first character.
Before using the variable, you must first write a statement like vbs I, which means that now I assign a variable named I and participate in the program running. In fact, you do not need to perform such registration procedures and use I directly somewhere in the program. However, practice has proved this is a bad habit, making it difficult for you to write large programs.
Then vbs I = 10 tells the program that the current value of this variable I is 10, and where I is used in the future, it is equivalent to 10. For example, vbs j = I + 1 is equivalent to j = 10 + 1, so the j value is 11.
The charm of a variable is that it can not only replace numbers, but also replace characters. Compared with a number, a character has its own particularity.
For example:
Vbs I = "character"
Vbs j = "concatenation"
Vbs a = I & j
In this case, a is a string. Note the two new symbols: "And. Someone may have guessed their role. . The computer has a completely different understanding of vbs I = 10 and vbs I = "10. & Is the string Sn. If a = j & I, a is a string.
After knowing the usage of variables, we can make the selling program more interesting. For example, if we want to make the program speak differently, we should first sell XXX for 10 times and then sell YYY for 10 times. Then we can write it as follows:
Vbs I
Vbs I = 10
While I> 0
Ifexpression I <= 5
SayString sells XXX, and the price is negotiable.
Endif
Ifexpression I> 5
SayString sells YYY, and the price is negotiable.
Endif
Vbs I = I-1
Delay10000
Endfor
In fact, we use a small programming technique, that is, to control the program trend with variable I size. It can be seen that YYY is sold for the first five times, and XXX is sold for the last five times.
Or call XXX once, then YYY again, and then repeat the previous action:
Vbs I
Vbs I = 10
While I> 0
Ifexpression I mod 2 = 0
SayString sells XXX, and the price is negotiable.
Endif
Ifexpression I mod 2 = 1
SayString sells YYY, and the price is negotiable.
Endif
Vbs I = I-1
Delay10000
Endfor
Here we also use an I-controlled method to run the program. Mod is the same operator number as +,-, and * To take the remainder. For example, 10 mod 2 = 0 means 10 divided by 2, and the remaining number is 0. Similarly 9 mod 2 = mod 2 = mod 2 = mod 2 = mod 2 = mod 2 = 1 ......
Oh, write it here for now.
In fact, this script is a script under the press-key wizard and is not clearly written. Sorry, if you want to see the vbs tutorial, we suggest you download a vbscript manual, then, read the previous article in The vbs section of the script house. At the beginning, there were more basic posts.
Http://www.jb51.net/books/210.html
Microsoft Vbscript Reference Manual