VBS Programming Tutorial (2nd article) _vbs

Source: Internet
Author: User
Tags arithmetic constant
Second article:

I had no idea that someone would not do the first homework.

It looks like it's going to be very, very meticulous, well, today we're talking about "quantity" and basic operations.

It's simpler to say constants first.

What is a constant, a constant is the amount of the value that cannot be changed.

Constants are divided into two types: the first, the natural constants. This is called because they are constants, how do you change the value of 21? He's always 21, and he's never going to turn 46.

If you use "21=46" in your program, such statements will raise an error. Similarly, a string is also a constant (remember the string?), "Hello World" is an example, if you use a statement such as "Hello world" = "Bye", it can also cause an error. Can you cite more examples of natural constants?

The second is our own defined constants, which also use the code number, they are also assigned, but the difference with the variables is that they are assigned at the time of the definition, can not be changed, if the attempt to change will cause an error. To define a variable, we use the keyword "Const" (the keyword means that the system defines a word that has special functions, cannot be used as a variable name or a constant name), and the format is: const constant NAME = constant value.

For example:

Const PI=3.1415926

Const name= "Memory Fragment"

So we've defined two constants, pi, and name, and in general, the constant name is all capitalized, but it doesn't have to be the same as you like. It is a good practice to define a number of values that do not need to be changed in the program as constants, which prevents unnecessary surprises. In addition, using custom constants can also reduce your workload. Like what:

MsgBox ("Hello World")
MsgBox ("Hello World")
MsgBox ("Hello World")
MsgBox ("Hello World")
MsgBox ("Hello World")

This program output five times Hello world, if you want to change the output for bye-bye, you have to modify all the programs, of course, you can manually modify 5 times, but if you want to output 1000 times? Constants can solve this problem for us:

Const hw= "Hello World"
MsgBox (HW)
MsgBox (HW)
MsgBox (HW)
MsgBox (HW)
MsgBox (HW)

So when you want to modify the output, just change the value of the HW.

OK, now let's take a look at the first important "cornerstone" of programming: Variables. The best way to explain a variable I think is "box", a variable like a box, which can only contain a thing, when you want to load something else must take out the original things. This "box" has a name, and when you use variables in a program, the system opens the box to take out the contents and let these things work, not the box. Some languages are very dependent on the "box" to contain something, so as to find the right "box" (such as C language), but the VBS to provide me with the automatic telescopic "magic box", we do not care about what is loaded in the thing,

VBS automatically adjusts the size of the box. For example:

Dim A1,A2,A3
A1=14
a2=12.23
a3= "Hello"

Rather than the C language as trouble: or VB formal statement (VB can declare also can not do):

int A1; Dim A1 As Integer
FLOAT A2; Dim A2 as Double
char* A3; Dim A3 as Strnig
a1=14; A1=14
a2=12.23; a2=12.23
A3= "Hello"; a3= "Hello"

Well...... Pull away ...

What's the use of variables? Wow, that would be a big use. The simplest, you are not able to determine the value of the program Run-time variables, such as the previous lesson we have to enter the name of the program, you are not sure what InputBox return (remember InputBox return value?) is what you enter), so you have no way to deal with all kinds of situations, but we use the name of this "Box" to put the user's name, to use when we just know the name of the box to the right, the system will open his own and the contents of the inside out. For example, we write a program that calculates the area of a rectangle, such as the program to be used by a pupil:

Dim a,b,s
A=15
B=12
S=a*b
MsgBox (s)

So you can find a length of 15, width of 12 rectangular area, is not very simple? Of course, this program can also be written like this:

Dim s
S=15*12
MsgBox (s)

This seems to be a lot shorter and less memory, but not an encouraging approach, why? Please look below.

Now, our program to become like a point of the line, whose program is written out to others to modify the source code to use AH?

So, we're going to accept the user input, remember? InputBox function.

The revised procedure is as follows:

Dim a,b,s
A=inputbox ("Please enter the length of the rectangle:")
B=inputbox ("Please enter the width of the rectangle:")
S=a*b
MsgBox (s)

OK, so the modification, no matter what data the user input, we can calculate the size of the rectangle. What if you can change it with s=15*12? Of course not.

I think you've found that the mathematical calculations in VBS are no different from true arithmetic, +,-, *,/, (), [],{} are all the same usage, such as:

Dim ans
ans=12+32/4+[(23-10) *2]
MsgBox (ans)

The law of arithmetic is also effective in programming, and you can get the fun of primary school in programming (Do you hate math? Then don't study computer).

One interesting operator in programming is "mod", which is called the "rest operator", which is the remainder of a division, such as

Dim a
A=16 MoD 5

Do you know a equals a few? Bingo! Yes, that's 1. Because of the 16/5 =3....1, the result of the MoD calculation is 1.

Another operator is "^" (the small arrow on the keyboard "6"), and he says "power" (or "square") for example:

Dim a,b,c
a=2
B=a^2
C=a^3
MsgBox (A)
MsgBox (c)

Then b=a*a=4, c=a*a*a=8

Let's not talk too much at once, this time it's here, now summarize.

Points:

1 constants are divided into natural constants and custom constants, and the values of constants cannot be modified

2 variable is like a box, we don't care what is in the box, but we must know the name of the box.

3 arithmetic no difference in programming

4 MoD is the remainder of the operation


Homework:

1) To compile a program to calculate the area of the circle, the radius by the user given (using InputBox) Pi value of 3.14159

2) A program to obtain the remainder of 20/3

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.