VBS Programming Tutorial (3rd article) _vbs

Source: Internet
Author: User
Tags logical operators
Third article (total six articles):

First of all, let me address some of the questions from the last course.

First, that remainder problem, 16/5 = 3...1, is because I changed the front part, the back of the forgotten changed, sorry.

Second, take a look at the list of programs:

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 1, 2 o'clock is 6

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 1, 2 o'clock is 24

Why is it different? In mathematics c= (a+b) *2 and c=a*2+b*2 are equivalent, as are the same in VBS. The problem is "+", in the VBS, + not only the meaning of the plus sign, but also the two strings, such as "Hello" + "world" = "HelloWorld" Have you understood? Do you remember the return value of the Inoutbox function? It's a string! That's the problem, right? In programming "1" is not equal to (<>) 1, "1" is a character, and 1 is a number, so a,b are all string variables, "1" + "2" = "12", it's as if we were joking with our buddies and asking them 1+1=?, we always laugh and say, "wrong, should be 11 ". But why, a can *2 without mistakes? This is a performance of the VBS more intelligent, if the content of this string is a number and is mathematically performed on him, the string is cast to participate in the operation, if the string represents a number, but does not participate in the mathematical operation, but participates in the string operation (merging) as a string processing, so you see A+B=12, The result of this a+b (12) is a string, and when it is multiplied by 2 it is forced to convert to the number 12, so that I get the result 24.

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

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

The idea is to pass a as an argument to the INT function, the INT function returns that integer (your input value) and then lets the return value participate in the operation, and the correct answer is obtained. So, if you use the InputBox function later, it's better to use the INT statement processing: such as C=int (c ) ' C is your own variable

Do you think that this course is a bit boring, oh, the variables and operators part of it is true, but a lot of practice is good, this time, we write something really fun: Process Control statements. This section begins with real programming.

Firstly, the structure of judgment is introduced.

Before we begin, we introduce a simple variable type: Boolean (Boolean), which has only two possible values: True,flase, True or false. This variable is useful in some cases (such as "switch"). We define a bool variable in the same way as other variables, and the assignment is the same, for example:

Dim a,b
A=true
B=false

Note that true is not the same as "true", "true" is a string, and true is a Boolean value and must not be confused.

To go back to the IF statement, let's take a look at the simplified version of the IF statement: the IF-then statement body we'll look at an example:

Dim a,b
A=12
B=13
If B>a Then MsgBox ("B greater than a")

We only look at the last line, a>b this expression has a return value, is a bool type. Because there are only two possibilities for this equation: B is greater than A, B is not greater than a, so there are only two probabilities of this equation, that is true or false. If statement to determine whether the return value of this expression is true or false, if True (true) then execute the statement after then, if it is false, then do not execute, you change the value of a to 14 to see if there will be pop-up dialog box?

What do we do when we have to execute a multiline statement after the judgment, and we need to solve it with a block of statements, where we can call block if

Dim a,b
A=12
B=13
If A<b Then
MsgBox ("A less than B")
MsgBox ("B greater 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 empty out 4--8 (a <Tab> key) lattice, this is not necessary, but is a good habit, in order to see the structure of the CHU program. So that we can run more than one statement, please note that if...then...end if these three key parts are not dropped. OK, I have a problem, enter a number, if less than 100 output "error", if more than 100 on the output "correct", I have two program version:

Dim a
A=inputbox ("Please enter a number greater than 100")
A=int (a) ' InputBox returns a string and we turn him 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 and we turn him into an integer
If A>100 Then
MsgBox ("correct")
Else
MsgBox ("error")
End If

See more than one else, the role of else is when the expression to be judged is false. This allows the program to handle two different situations. Don't forget to use End if ending

Hey, I'm a pervert, now I want you to deal with three kinds of situations, <100,=100,>100, but also write in an if structure, what do you do, I give you the answer:

Dim a
A=inputbox ("Please enter a number greater than 100")
A=int (a) ' InputBox returns a string and we turn him into an integer
If A>100 Then
MsgBox ("correct")
ElseIf A=100 Then
MsgBox ("Boss, you tricked me?")
Else
MsgBox ("error")
End If

This time enter 100 to see, what is it? The else if statement can appear multiple times in the if structure, with flexibility to judge different situations (if you're going to judge too much, use "Select Structure", later), and then execute the statements in else when all the ElseIf are finished, and no match is made. Another example:

Dim a,b,c,d
A=inputbox ("A is:", "input radius")
B=inputbox ("B is:", "input radius")
D=inputbox ("Answer:", "input Answer")

C=a*2+b*2 ' There's no problem here, it will be automatically converted
If D=c Then
Msgbox ("You're Smart")
Else
Msgbox ("Hello Pig's own problem is not!")
End If

Look at this again. No matter how correctly you answer, you are a pig, haha, not I play you, or the beginning of the article InputBox return type is playing you, D is the return value of InputBox, he is a string, and C is the result of an integer calculation, he is an integer. A string, however, is not equal to an integer, although they are literally the same: "8" <> (not equal to number 8) so if the value of the judgment is always false, the statement of the else part is always executed. We can change that.

Dim a,b,c,d
A=inputbox ("A is:", "input radius")
B=inputbox ("B is:", "input radius")
D=inputbox ("Answer:", "input Answer")
D=int (d)
' Here we take the value of D and turn it into an integer, and put it back in the "D" box.
C=a*2+b*2
If D=c Then
Msgbox ("You're Smart")
Else
Msgbox ("Hello Pig's own problem is not!")
End If

That's the way it works. This is also a nuisance of the InputBox function, there is no way, VBS no other good input mode.

When it comes to if, we have to talk about the logical operators, today we introduce two kinds of, "and" and "or" learned the IF statement, I give an example, you see it.

Dim a,b
A=inputbox ("Enter a number >10")
B=inputbox ("Enter another number >10")
A=int (a)
B=int (b)
If a>10 and b>10 then
MsgBox ("correct")
Else
MsgBox ("error")
End If

This program allows you to enter two values, must be greater than 10, as long as there is not more than the output error

Dim a,b
A=inputbox ("Enter a number >10")
B=inputbox ("Enter another number >10")
A=int (a)
B=int (b)
If a>10 or b>10 then
MsgBox ("correct")
Else
MsgBox ("error")
End If


This program allows you to enter two values, as long as there is a greater than 10, return success. In fact, and and or well understood, I read the phrase "if a>10 or b>10 then" in Mandarin: "If A is greater than 10 or B is greater than 10, then ...". This is not a good understanding of it.

OK, let's look at a new structure, today's class is over, it's midnight, and I'm dead tired.

ElseIf when your program has to deal with many different kinds of judgments. Then makes the program look cluttered, so there's a Select Case structure that specifically copes with this situation, and the syntax of select cases is simple:

Select Case Variable Name
Case value
Statement
Case value
Statement
Case Else
Statement
End Select


We can give you an example of a simple explanation:

Dim a
A=inputbox ("Enter a 1--3 value")
A=int (a) ' handles InputBox return string problem
Select Case A
Case 1
MsgBox ("one")
Case 2
MsgBox ("II")
Case 3
MsgBox ("three")
Case Else
MsgBox ("Input error")
End Select

This example converts the 1,2,3 three Arabic numerals into Chinese capital numbers, which are written in the form of If...elseif as follows

Dim a
A=inputbox ("Please enter the value of 1--3")
A=int (a)
If A=1 Then
MsgBox ("one")
ElseIf a=2 Then
MsgBox ("II")
ElseIf A=3 Then
MsgBox ("three")
Else
MsgBox ("Input error")
End If

Well, trouble, or select it.

OK, this concludes today, to sum up:


Points:

1 InputBox returns a string rather than a number, which must be converted to the ratio in the form of A=int (a)

2 the value of bool variable has only two kinds: true,false

2.5) and both sides of the expression are true, returns True. Returns true if one of the expressions on either side is true

3 The format of the IF statement

4) select...case format


Homework:

1 Use 3 bool values to store your 3 siblings whether they are males (hint: sister1male=false)

2 given a number, greater than 10 and less than 20 output "correct", otherwise output "error"

3 Input 12, or 15, output "correct", otherwise output "error"

4 converts a positive integer within 5 to a larger number in China

5 to design a program to apply today's knowledge

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.