SoFunction
Updated on 2025-04-06

VBS Programming Tutorial (Part )

Article 2:
I really didn’t expect that someone wouldn’t know how to do the first homework.

It seems that we need to be very detailed. Well, today we are talking about various "quantity" and basic operations.

Let’s talk about constants first, this is relatively simple.

What is a constant? A constant is a quantity whose value cannot be changed.

Constants are divided into two types: the first type, natural constants. This is called because they are constants themselves. How do you change the value of 21? It is always 21, and it cannot become 46.

If you use "21=46" in your program, such a statement will raise an error. Similarly, a string is also a constant (remember a string of characters? It is a string of characters wrapped between ""), "Hello World" is an example, and if you use a statement like "Hello World"="Bye" will also raise an error. Can you give more examples of natural constants?

The second type is a constant that we define ourselves. This kind of quantity also uses code names, and they are also assigned values, but the difference with variables is that they are assigned values ​​when they are defined and cannot be changed later. If they attempt to change, an error will be triggered. To define a variable, we use the keyword "const" (the keyword means that the system defines a word with special functions and cannot be used as variable names or constant names). The format is: const Const name = constant value.

For example:

const PI=3.1415926 

const NAME="Memory Fragments"

In this way, we define two constants, PI and NAME. Generally speaking, the constant names are all capitalized, but they can be used as you like. It is a good habit to define some values ​​that do not need to be changed in the program as constants, which can prevent unnecessary accidents. In addition, using custom constants can also reduce your workload. For example:

msgbox("Hello World") 
msgbox("Hello World") 
msgbox("Hello World") 
msgbox("Hello World") 
msgbox("Hello World") 

This program outputs Hello World five times. If you want to change the output to Bye-Bye, you must modify all programs. Of course, you can modify it manually 5 times, but what 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) 

In this way, when you want to modify the output, just modify the value of hw.

Okay, now let’s take a look at the first important "cornerstone" of programming: variables. I think the best way to interpret variables is "box". A variable is like a box, and can only hold one thing inside. When you want to put something in something, you must take out the original things. This "box" has a name. When you use variables in the program, the system will open the box to take out the things inside, and let these things participate in the processing, rather than the box. Some languages ​​rely heavily on what is in the "box" so that you can find a suitable "box" (such as C language). But VBS provides me with a "magic box" that can automatically retract, and we don’t have to care about what is in it.

VBS will automatically adjust the size of the box. For example:

Dim a1,a2,a3 
a1=14 
a2=12.23 
a3="Hello" 

Instead of having to worry about C: or a regular statement of VB (VB can be declared or not) like:

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... it's too far...

What are the uses of variables? Wow, that's very useful. The simplest thing is, you can't determine the value of the variable when the program runs. For example, the program we wrote to enter the name in the previous class. You can't determine what InputBox returns (remember the return value of Inputbox? It's the content you entered), so you can't deal with various situations, but we use the name "box" to pack the user's name. When we use it, we just need to know the name of the name box. The system will open it by itself and take out the contents inside. For example, we write a program to calculate the area of ​​a rectangle, for example, this program needs to be sent to primary school students for use:

dim a,b,s 
a=15 
b=12 
s=a*b 
msgbox(s) 

In this way, you can find the area of ​​a rectangle with a length of 15 and a width of 12. Isn't it very simple? Of course, this program can also be written like this:

dim s 
s=15*12 
msgbox(s) 

This seems that the program is much shorter and saves memory, but it is not an encouragement method. Why? Please see below.

Now, our program needs to become like a little bit. Whose program needs to be written and the source code needs to be modified to use it?

So, we have to accept user input, do you still remember? InputBox function.

The modified program 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, with such a modification, no matter what data the user inputs, we can calculate the area of ​​the rectangle. If you use s=15*12, can you change it? Of course not.

I think you have discovered that mathematical calculations in vbs are no different from real arithmetic. +,-,*,/,(),[],{} are all used the same way, for example:

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

The rules of the four operations also take effect in programming. You can regain the fun of elementary school during programming (do you hate mathematics? Then don’t learn computers).

There is an interesting operator in programming that is "mod", which is called the "remain operator", which is to obtain the remainder of a division, for example

dim a 
a=16 mod 5 

Do you know how many a is equal to? Bingo! Yes, it is 1. Because 16/ 5 =3....1, the result of mod calculation is 1.

There is another operator that is "^" (that is the small arrow on the keyboard "6"), which means "multiple 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, let’s stop here this time, let’s summarize it now.

Key points:

1) Constants are divided into natural constants and custom constants, and the value of the constant cannot be modified.

2) Variables are like boxes. We don’t care what is in the box, but we must know the name of the box.

3) The four operations are no different in programming.

4) MOD is the residual operation


Operation:

1) Write a program to calculate the area of ​​the circle, and the radius is given by the user (using Inputbox) PI value 3.14159

2) Make a program to obtain a remainder of 20/3