Tutorial on VBS programming (article 3rd)

Source: Internet
Author: User

Article 3 (six in total ):

First, let me solve some questions about the last course.

First, the remainder problem, 16/5 = 3... 1, is because I changed the previous part and forgot to change it later. Sorry.

Second, take a look at the program list:

1)
Dim a, B, c
A = inputbox ("a is:", "input radius ")
B = Inputbox ("B is:", "input radius ")
C = a * 2 + B * 2
Msgbox (c)
The input value 1 and 2 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 is 24 at 1 and 2.

Why is it different? In mathematics, c = (a + B) * 2 and c = a * 2 + B * 2 are equivalent, as are VBS. the problem lies in "+". In VBS, + not only indicates the plus sign, but also connects two strings, for example, "Hello" + "World" = "HelloWorld" have you understood? Do you still remember the return value of the InoutBox function? Yes string! This shows the problem. In programming, "1" is not equal to (<>) 1, "1" is a character, and 1 is a number, so, B is a string variable, "1" + "2" = "12", it's like we joke with our partners and ask them 1 + 1 =? Similarly, we always say with a smile "wrong, it should be 11". But why can a * 2 but no error? At this time, VBS is intelligent. If the content of this string is a number and mathematical operations are performed on it, the string is forcibly converted to a number for calculation. If the string represents a number, but not involved in mathematical operations, but involved in string operations (merging) is treated as strings, so you can see that a + B = 12. At this time, the result of a + B (12) is a string, when it is multiplied by 2, it is forcibly converted to the number 12, so that I get the result 24.

How can I 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. We modify it as follows:

C = (int (a) + int (B) * 2

This means to pass a as a parameter to the int function, and the int function will return the integer (your input value), and then let the return value participate in the operation, so that the correct answer is obtained. therefore, if you are using the inputbox function, you 'd better use the int statement to process it. For example, c = int (c) 'C is your own variable.

Do you think this course is a bit boring? Well, the variables and operators are indeed like this, but a lot of exercises are fine. This time, let's write something really fun: Process Control statements. this part begins with true programming.

First, we will introduce the judgment structure.

Before that, we first introduce a simple variable type: Boolean. This variable has only two possible values: True, Flase, True, or false. this type of variable is useful in some situations (such as "Switch "). we define a bool variable in the same way as other variables and assign values, for example:

Dim a, B
A = true
B = false

Note: true and "true" are different. true is a string, true is a Boolean value, and cannot be confused.

Back to the if statement, let's take a look at the simplified version of the if statement: if then statement body. Let's look at an example:

Dim a, B
A = 12
B = 13
If B> a then msgbox ("B is greater than ")

Let's just look at the last row. The expression a> B has a return value, which is of the bool type. because there are only two possibilities for this formula: B is greater than a and B is not greater than a, there are only two possibilities for this formula, that is, true or false. the if statement determines whether the return value of this expression is true or false. if it is true, the statement after then is executed. if it is false, the statement is not executed, change the value of a to 14 to see if a dialog box is displayed?

What should we do if we want to execute multi-line statements after judgment? We need to use statement blocks to solve this problem. Here we can call a block if

Dim a, B
A = 12
B = 13
If a <B then
Msgbox ("A less than B ")
Msgbox ("B greater than ")
End if

The two msgbox functions are sandwiched between the if and end if functions. This part is the statement block. Before each statement in the block, please leave a 4-8 (one <Tab> key) space, this is not necessary, but it is a good habit to understand the program structure. in this way, we can run more than one statement. Please note that if... then... the three key parts of end if should not be dropped. OK. I have a question and enter a number. If it is smaller than 100, an "error" will be output. If it is greater than 100, "correct" will be output. I have two program versions here:

Dim
A = inputbox ("enter a number greater than 100 ")
A = int (a) 'inputbox returns a string, which is converted into an integer :)
If a> 100 then msgbox ("correct ")
If a <100 then msgbox ("error ")

There is also a simpler

Dim
A = inputbox ("enter a number greater than 100 ")
A = int (a) 'inputbox returns a string, which is an integer.
If a> 100 then
Msgbox ("correct ")
Else
Msgbox ("error ")
End if

If one more else is displayed, else is executed when the expression to be judged is false. in this way, the program can handle two different situations. don't forget to end with end if

Hey hey, I'm a metamorphosis. Now I want you to handle three situations: <100, = 100,> 100, and write it in an if structure. What do you do? I will give you the answer:

Dim
A = inputbox ("enter a number greater than 100 ")
A = int (a) 'inputbox returns a string, which is an integer.
If a> 100 then
Msgbox ("correct ")
Elseifa = 100 then
Msgbox ("boss, are you kidding me? ")
Else
Msgbox ("error ")
End if

Enter 100 this time. What is it? The else if statement can appear multiple times in the if structure to flexibly determine different situations (if you want to judge too much, use "select structure". I will talk about it later ), when all elseif operations are completed and no matching conditions are met, execute the statements in 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 is no problem here, it will be automatically converted
If d = c then
Msgbox ("You are smart ")
Else
Msgbox ("You are not answering your questions! ")
End if

Let's take a look at this. No matter how correct your answers are, you are a pig. Haha, I am not playing with you, or the inputbox return type at the beginning of the article is playing with you. d is the inputbox return value, it is a string, while c is the result of an integer calculation. It is an integer. A string is not equal to an integer in any way, although they are literally the same: "8" <> (not equal to number) 8, so the value of the if argument is always false, always execute the else statement. we can modify it like this.

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 out the value of d and turn it into an integer, and put it in the box "d ".
C = a * 2 + B * 2
If d = c then
Msgbox ("You are smart ")
Else
Msgbox ("You are not answering your questions! ")
End if

This will be successful. This is also an annoying part of the Inputbox function. vbs has no other good input methods.

Speaking of if, we have to talk about logical operators. Today we will introduce two types of operators: "and" and "or". After learning the if statement, I will give you an example.

Dim a, B
A = inputbox ("enter a number> 10 ")
B = inputbox ("input another number> 10 ")
A = int ()
B = int (B)
If a> 10 and B> 10 then
Msgbox ("correct ")
Else
Msgbox ("error ")
End if

This program requires you to enter two values, both of which must be greater than 10. If one value is not greater than 10, an error is output.

Dim a, B
A = inputbox ("enter a number> 10 ")
B = inputbox ("input another number> 10 ")
A = int ()
B = int (B)
If a> 10 or B> 10 then
Msgbox ("correct ")
Else
Msgbox ("error ")
End if

This program requires you to enter two values. If one value is greater than 10, success is returned. in fact, and or are well understood. I read the sentence "if a> 10 or B> 10 then" in Chinese: "if a is greater than 10 or B is greater than 10, so... ". is that easy to understand.

Okay. Let's look at a new structure. Today's class is over. It's midnight and I'm exhausted.

Elseif .. then will make the program look messy, so there is a select case structure specifically to deal with this situation, the syntax structure of select case is very simple:

Select case variable name
Case Value
Statement
Case Value
Statement
Case else
Statement
End select

The following is an example:

Dim
A = inputbox ("enter a value ranging from 1 to 3 ")
A = int (a) 'processing the string returned by inputbox
Select case
Case 1
Msgbox ("1 ")
Case 2
Msgbox ("II ")
Case 3
Msgbox ("3 ")
Case else
Msgbox ("input error ")
End select

In this example, the numbers 1, 2, and 3 are converted into uppercase Chinese numbers. The program is written as if... elseif in the following format:

Dim
A = inputbox ("Enter the value 1-3 ")
A = int ()
If a = 1 then
Msgbox ("1 ")
Elseif a = 2 then
Msgbox ("II ")
Elseif a = 3 then
Msgbox ("3 ")
Else
Msgbox ("input error ")
End if

How are you doing? Please select.

OK. The end of today is summarized as follows:

Key points:

1) inputbox returns a string instead of a number, which must be converted to a number in the form of a = int ().

2) The bool variable has only two values: true and false.

2.5) if both and expressions are true, true is returned. or if either of the expressions is true, true is returned.

3) if statement format

4) select... case format

Job:

1) Use 3 bool values to store whether your 3 siblings are male (Tip: sister1male = false)

2) The number of given numbers is greater than 10 and less than 20. The output is "correct". Otherwise, the output is "Incorrect"

3) Input 12 or 15. The output is "correct". Otherwise, the output is "Incorrect"

4) convert positive integers within 5 into Chinese big numbers.

5) design a program by yourself and 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.