Tutorial on VBS basic programming (article 4th)

Source: Internet
Author: User

Four (six in total ):
Hello everyone, I took a day off yesterday, so I wrote chapter 2 today: circular structure.

Let's take a look at the question: the mall performs daily settlement and requires that the current turnover be accumulated. Each time you enter a number, this question is actually very simple, but now we are learning

It is quite troublesome to finish the problem with previous knowledge. First, we need to know the number of purchases and purchases to control the number of inputs.

It is very inefficient, and every day the program is re-designed. Assuming that five transactions are made today, the following is the source program:

Dim sum
Sum = 0' initialization variable
Sum = sum + int (inputbox ("Enter transaction amount "))
'Sum = sum + x is a useful method that extracts its own values, performs an operation, and puts them back into itself.
'The nested function is used here to directly pass the inputbox return value to the int function and convert it into an integer, the same below
Sum = sum + int (inputbox ("Enter transaction amount "))
Sum = sum + int (inputbox ("Enter transaction amount "))
Sum = sum + int (inputbox ("Enter transaction amount "))
Sum = sum + int (inputbox ("Enter transaction amount "))
Msgbox (sum)

Have you seen it? I copied the computing process five times to design the program. This kind of program can be used together in places with a small number of transactions such as the automobile exchange. If

Isn't it necessary to copy and paste them in the supermarket thousands of times? The content we are talking about today can overcome this defect. First, let's talk about the following Do... Loop statements.

Do... the loop structure looks very simple: do... loop, that's all. This structure continues to execute the statement (called loop body) between do and loop ),
Never stop. For example:

Do
Msgbox ("This information will appear repeatedly. To stop the program, use the Task Manager (Ctrl + Alt + Del) to stop the wscript Process ")
Loop

Run this program. When you click to pin a dialog box, the other one will come out immediately. You will never finish it. There will always be one. Who will run this program? Unless

Other people make trouble (I have done this), so there is another statement in the do... loop structure: exit do, which will terminate the loop and jump to the statement next to the loop to continue

Execution. For example:

Dim a' Note: constants do not need to be declared in dim; otherwise, an error is thrown.
Const pass = "123456" '. This is a string. enclose it with "". Set the password to a constant and cannot be changed.
Do
A = inputbox ("enter the password ")
If a = pass then
Msgbox ("password verification successful ")
Exit do
End if
Loop

This program keeps asking you the password until you have entered the correct password. (if can be nested in another if, or nested in the loop body.

So we must use indentation to clearly distinguish the various parts of the Program). This program is very classic, and early programs do this. But we are Hacker, so

We understand the security of the system. This unlimited authentication program can be easily cracked. We need to limit the number of authentication times. modify the program as follows:

Dim a, ctr
Ctr = 0' set counter
Const pass = "pas123 _" 'the one above is a weak password. This change is more powerful.
Do
If ctr = 3 then
Msgbox ("the authentication threshold has been reached and the authentication program is closed ")
Exit do
Else
A = inputbox ("enter the password ")
If a = pass then
Msgbox ("authentication successful ")
Msgbox ("(you can add a piece of information after success )")
Exit do
Else
Ctr = ctr + 1 'add an error authentication count if the password fails
Msgbox ("authentication error, check password ")
End if
End if
Loop

Run this program and try again. When 3 errors occur, you will stop asking for the password again and close the program. telnet AUTHENTICATION is used to limit the number of times.

It may be difficult to understand the nested if statement. Please design a similar program by yourself.

In fact, to add the verification function in do... loop, you don't have to use if. We can use do. I will introduce the while keyword directly, while can put
After do or loop, and then an expression, when the expression value is true (the expression is true), the loop body is run. Let's take a look at the modified
Program"

Dim a, ctr
Ctr = 0
Const pass = "pas123 _"
Do while ctr <3
A = inputbox ("enter the password ")
If a = pass then
Msgbox ("authentication successful ")
Msgbox ("(you can add a piece of information after success )")
Exit do
Else
Ctr = ctr + 1 'add an error authentication count if the password fails
Msgbox ("authentication error, check password ")
End if
Loop

The functions are exactly the same as those in the previous example. Let's take a look at putting while behind the loop:

Dim a, ctr
Ctr = 0
Const pass = "pas123 _"
Do
A = inputbox ("enter the password ")
If a = pass then
Msgbox ("authentication successful ")
Msgbox ("(you can add a piece of information after success )")
Exit do
Else
Ctr = ctr + 1 'add an error authentication count if the password fails
Msgbox ("authentication error, check password ")
End if
Loop while ctr <3

The functions are the same. Why should they be placed behind the loop? You can change the ctr value to 3, and the program behind do will exit directly, while the program behind loop

It also allows one authentication and ends in the loop. The opposite of while is until. The usage is the same as while. However, only when the value of the following expression is false (Table

The loop body is executed only when the form is not valid. Please test it by yourself.

Okay. Let's look at another loop structure, for... next, which is based on count and the most common loop structure in programming.

Dim I
For I = 0 to 5
Msgbox (I)
Next

Have you seen it? The I output increases progressively every time, but we do not explicitly point out that I will increase progressively. When I reaches 5, the loop ends, because it starts from 0, so

The loop body is executed six times, which is very important. Most of the tasks start from 0 instead of 1. This program can also be written

Do format:

Dim I
I = 0
Do while I <5
Msgbox (I)
I = I + 1' because do cannot be automatically counted, you must manually add
Loop

How about it? It's better to use for. for is useful in programming. Let's take another example to explain nested loops by the way.

Dim I, j
For I = 1 to 9
For I = 1 to 9
Str = str & I * j & "'& is the symbol of the sum string
Next 'Each next corresponds to a
Next

Check whether the running results remind you of the mathematics teacher (ugly face) in the elementary age. Note that there is a "big" for, and a small.

After a small for execution cycle, a large for execution is performed once (in other words, a large for execution is performed once, and a small for execution is performed 9 times ).

Once. In the big for, you can add more than a small for statement. Let's modify the source program:

Dim I, j
For I = 1 to 9
For I = 1 to 9
Str = str & I * j &""
Next 'Each next corresponds to a
Str = str & vbCrlf 'vbcrlf is equivalent to the Enter key on the keyboard. Because you cannot input it on the keyboard, the system defines a default constant.
Next

After this operation, the output results are divided by the multiplier. After each small for operation is completed, a line is changed (through vbcrlf ).

This article may be hard to understand for cainiao. There is only one method to master: Multiple practices. In addition, many people on the Forum still asked: "What job does VBScript need?

Editing? "I am very angry. I have explained in the first article: use NotePad to edit the source code and save it as a program with the. vbs extension. Please do not

Ask again. In addition, a domestic spam software "Super X overlord" has preemptible the vbs extension. please uninstall the spam.

Let's summarize:

Key points:

1) do .. loop and exit do usage

2) while when the expression is true, execute the loop body, until and vice versa.

3) for... next is a counting loop, and each execution of the counter increments

4) Functions and writing of nested loops

4.5) & used to connect strings

5) vbCrLf is equivalent to the Enter key on the keyboard.

Job:

1) In our classical mathematical book "nine chapters of arithmetic", there is a question: buy a chicken for a hundred dollars, a rooster for five dollars, and a hen for three dollars, chicken: 1 RMB and 2 RMB (I am a parameter for this data)

I got a programming book, but I remember it was Gong 3, MA 1, Xiao 1, and Qian 3? No matter how many ways you can buy the chicken.

Let me talk about it in a vernacular: Someone is going to buy a chicken. I bought 100 chicken for 100 yuan. The price is as follows: public: 5 $, Mother: 3 $, small: 1 $ for 2. How many types of products do you want to sell?

.

Ps: I have to go to the Immigration Service today, so my article is very hasty. Most of the Code is not tested. Please help me find the error. In addition, the previous assignment should be performed on the forum.

Now, I will not repeat the answer. I don't know if you like to answer this question, or is it simple in the past?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.