Four articles (total six articles):
Hello everyone, yesterday I took a day off, so today I write the 4th chapter: Circulation structure
We first look at a problem: shopping malls for daily settlement, the requirement to add up today's turnover, each input a number, this problem is actually very simple, but we now learn
The knowledge to finish this problem is quite troublesome, let's analyze it. First of all, we need to know the number of transactions, so as to control the number of input, but this set
The meter is very inefficient, and every day you redesign the program. Assuming that 5 transactions were made today, the following are the source programs:
Dim sum
Sum=0 ' initialization variable
sum=sum + int (InputBox ("Please enter transaction volume")
' Sum=sum+x This form is to take out the value of its own, to do an operation, and then put back to itself, this method is very useful
' Here the function nesting is used, and the return value of the InputBox is passed directly to the INT function, which is converted to an integer
sum=sum + int (InputBox ("Please enter transaction volume")
sum=sum + int (InputBox ("Please enter transaction volume")
sum=sum + int (InputBox ("Please enter transaction volume")
sum=sum + int (InputBox ("Please enter transaction volume")
MsgBox (Sum)
See, I designed the program by copying the process 5 times, which can be used in places where there are fewer transactions such as the car exchange, if
Do you want to copy it and paste it thousands of times in the supermarket? What we're talking about today can overcome this flaw, first of all, let's talk about the following do ... The Loop statement.
The structure of the Do...loop looks very simple: Do...loop, that's all, this structure keeps executing the statement between do and loop (name: Loop body),
Never stop. For example:
Todo
MsgBox ("This information will appear repeatedly, to stop the program, use Task Manager (Ctrl+alt+del) to abort the WScript process")
Loop
Run this program, when you point out a dialog box will come out another, you will never finish, there is always the next one. Who would run such a program? Unless it's for
People messing around (I've done this), so in do. There is also a statement in the loop structure: Exit Do, which terminates the loop, and jumps to the statement following the loop to continue
Perform. For example:
Dim a ' Note: Constants do not need to be declared in dim, or they can cause errors
Const pass= "123456" "This is a string, please use" "Wrapped up. Set password is constant and cannot be changed
Todo
A=inputbox ("Please enter password")
If A=pass Then
MsgBox ("Password checksum succeeded")
Exit Do
End If
Loop
This program will keep asking you for the password, until you have entered the correct password. (if can be nested within another if, or nested within the loop body)
, so be sure to use indents to separate parts of the program. This program is very classic, the early procedures are doing so. But we are hacker, so
We understand the security of the system, this unlimited authentication program is easy to be exhaustive cracked, we have to limit the number of certification. Modify the program as follows
Dim a,ctr
Ctr=0 ' Set counter
The const pass= "pas123_" above is weak password, this change stronger a bit
Todo
If Ctr=3 Then
MsgBox ("has reached the certification limit, the certification process is closed")
Exit Do
Else
A=inputbox ("Please enter password")
If A=pass Then
MsgBox ("Certified successful")
MsgBox (You can add a successful message here)
Exit Do
Else
Ctr=ctr+1 ' If the password goes wrong, add one more error authentication count
MsgBox ("Authentication error, please check password")
End If
End If
Loop
Run this program to try, when you have 3 of this error, you will stop asking the password again, close the program. Telnet authentication is used to limit the number of programs with this large
with small differences. Note that the nested if statement, read the program carefully, may be more difficult to understand, but also please try to design a similar program yourself.
In fact, to add validation to the Do...loop function, does not necessarily need to use if, we can directly use do. Let me introduce the while keyword, while you can put
After do or loop, and then another expression, the loop body runs when the expression is true (the expression is set). Let's take a look at the revised
Program
Dim a,ctr
Ctr=0
Const pass= "pas123_"
Do While ctr<3
A=inputbox ("Please enter password")
If A=pass Then
MsgBox ("Certified successful")
MsgBox (You can add a successful message here)
Exit Do
Else
Ctr=ctr+1 ' If the password goes wrong, add one more error authentication count
MsgBox ("Authentication error, please check password")
End If
Loop
The functionality to do this is exactly the same as the previous example, so let's take a look at putting a while behind the loop:
Dim a,ctr
Ctr=0
Const pass= "pas123_"
Todo
A=inputbox ("Please enter password")
If A=pass Then
MsgBox ("Certified successful")
MsgBox (You can add a successful message here)
Exit Do
Else
Ctr=ctr+1 ' If the password goes wrong, add one more error authentication count
MsgBox ("Authentication error, please check password")
End If
Loop while Ctr<3
function is the same, why should you put it behind the loop? You'll know by changing the value of CTR to 3, while the program behind do will exit directly, and behind the loop
It will also allow a certification, and the loop does not end. And while the opposite is until, the usage is the same as while, but he only has when the value of the following expression is false (table
When the form is not set up, the loop body is executed, please test it yourself
OK, let's look at another kind of cyclic structure, For....next, which is based on counting and is also the most common loop structure in programming.
Dim i
For I=0 to 5
MsgBox (i)
Next
See that? I was incremented for each output, but we didn't explicitly point out that I was going to increment, and when I reached 5, the loop ended because it started with 0, so
The loop body executes 6 times, and it's important that most things start at 0 instead of 1. This program can also be written as
Do in the form:
Dim i
I=0
Do While i<5
MsgBox (i)
I=i+1 ' because do cannot be counted automatically, you must manually add
Loop
How to, or for more useful. For it's useful in programming, let's take another example and talk about nesting loops.
Dim i,j
For I=1 to 9
For I=1 to 9
Str=str & I * J & "" ' & is and the symbol of the string
Next ' Each next corresponds to a for
Next
Look at the results of the operation, whether you will think of the primary school math teacher (ugly countenance). Note that there is a "big" for, and a small for, when
After a small for execution period, the large for is executed once (in other words, the big for execution once, the small to execute 9 times), so the total execution of 9,980
A. In a large for, it can be more than just a small for, or it can be added to another statement. Let's revise 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 for
Str=str & VbCrlf ' VbCrlf is equivalent to the return key on the keyboard because you can't type on the keyboard, so the system defines a default constant
Next
After this operation completes, the output results are segmented according to the multiplier, each small for run once, change one line (through vbCrLf).
This time the content of the rookie may be more difficult to understand, there is only one way to master: more practice. In addition, I saw a lot of people in the Forum and asked, "What's the work for VBScript?
A knitting? " I was very angry, I was in the first article has been explained: Edit the source code with Notepad, and then save as a. vbs extension of the program is OK, please do not
Ask again. In addition, a homemade garbage software "Super x PA" preempted the extension of VBS, please unload that garbage.
Let's summarize:
Points:
1 do. Usage of loop and exit Do
2 while executing the loop body when the expression is true, until vice versa
3 For...next is the counting loop, each execution counter accumulator
4 function and writing of nested loops
4.5) & for connection strings
5) vbCrLf Equivalent to the key on the keyboard enter
Homework:
1 in China's mathematical classic "Nine Chapters of arithmetic" has such a problem: hundred money to buy hundred chickens, Rooster 5 Money A, hen 3 money A, chick 1 money 2 Only (this data I am ginseng
Test a programming book, but I remember is male 3, mother 1, small 1 money 3 only? Anyway, just follow the book to find out how many ways to buy these chickens. If you can't read it,
Words I use vernacular to say: "Someone wants to buy a chicken, with 100 dollars just bought 100 chickens, the price is as follows:": 5$, Mother: 3$, Small: 1$ for 2, let you ask how many kinds of sell
Method (male and female small how to match). Please use the loop to solve the problem.
PS: To go to the Immigration Bureau today, so the article is written in haste. Most of the code is not experimental, please help me find the wrong. In addition, the last homework everyone in the forum should
See, I will not repeat the answer, do not know whether you like to do this problem, or the previous kind of relatively simple?
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.