vbs basic tutorial (2)

Introduction

Earlier we talked about variables initially, now let’s talk about constants

The so-called constant is the amount that cannot be changed. There are two types of constants: natural constants and custom constants (what, can you define constants yourself?) Let's focus on the following.

For example, 32 is a natural constant. Constants are not like variables. You can install whatever you give them. Constants cannot be changed. If you have to force them to change, for example, 32=67, an error will occur and the compilation will fail. Do you remember "Hello world"? He is a string constant. Like 32=67, if you have to force "Hello world"="Good morning", an error will occur.

The second is the constants that we define ourselves. This kind of quantities also use code names, and they are also assigned, but the difference from variables is that they are assigned when they are defined, and they cannot be changed later. If you try to change it, it will cause An error. To define a constant, we use the keyword "const" (keyword means that the system defines a word with special functions, which cannot be used as a variable name or constant name)
  Custom constant format: const constant name constant value
reading At this point, some students may ask: Isn’t a constant a quantity that cannot be changed? What can he do? It seems that nothing can be done. Of course not, the use of constants can make us a lot more convenient.

Procedure 1

The following is a program made by Xiaoming, let's take a look.

 msgbox"Hello world"
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world" 
 msgbox"Hello world"

He output a total of ten hello world, but he found that he had written a mistake. What he wanted to output was "Good morning", what should I do?
Some students might say: Teacher, just change the string inside. What if it is 1,000 or 10,000? At this time, we can use constants.

const JA="Good morning"
 msgbox JA
 msgbox JA 
 msgbox JA 

If you use this form, it is much simpler, just change the JA.

Parsing

Variables are an extremely important part of vbs. It is like a box. Constants are also partly equivalent to the "things" in the "box". The two are indispensable and complement each other. When processing the "things" in the "box", the computer will automatically select the appropriate box based on this item. We don't have to worry about what box to choose, the computer will automatically select it for us (this has been discussed before).

Variables are unknowable things, because you want to put things in them, and we don’t know what things are. For example, when we talked about a=inputbox last time, we never know what is in a. He is asking the user to put something in the box a. (Also called return value, return the value of inputbox to a). If we want to use this value, we only need to call a to re-output the content. (Of course, you cannot input something and output it for use. This thing can also be cut, supplemented, etc., we will talk about this later)

When the value you get through user input is stored in a variable, we can use it to calculate, + means addition,-means subtraction, * means multiplication (yes, not ×.)/ means division (not ÷) , Through these operators to achieve what we want, for example, you can wash and fry the vegetables inside to get delicious dishes.

Procedure 2 & Knowledge Point 2

Let's look at a program that calculates 2×9:

dim a,b,c
a=15 
b=12 
c=a*b 
msgbox s 

Does this calculate 2x9? (Of course you can calculate any number)

We can also simplify this procedure:

 dim a 
  a=15*12 
  msgbox a

Is there two fewer variables defined? And it's shorter and more streamlined.

Did we learn the input function inputbox in the last lesson? Some students may think, if we let others input the things in a and b, wouldn't all multiplications be calculated? This idea is very good and can make our program more flexible and easier to use. The following code:

dim a,b,c
a=inputbox("请输入一个数字,下面我们要来进行乘法运算:") 
b=inputbox("请输入另一个数字:") 
c=a*b 
msgbox c 

In vbs, + (plus sign or connector),-(minus sign), * (multiplication sign), / (division sign), () parentheses, [] brackets, {} curly brackets ^ (power Or Fang) is the same usage,

Procedure 3

In addition, there is the remainder operator mod, please see its usage:

dim a 
  a=16 mod 5
  msgbox a

a=16÷5=3……1
The value of a is 1,
right?

Point

1. Custom constant format: const constant name constant value
2. Four arithmetic operations can also be done in programming
3. In vbs, the operator
+ (plus sign or connector),
-(minus sign),
* (multiplication sign)
/ (Division sign)
, () parentheses, () square
brackets,
() braces
^ (power or square)
mod (take remainder)

operation

1. Write a flexible remainder program
2. Write a calculation of circumference, and then write a calculation of circle area
3. Learn to use custom constants
4. Understand the concepts of constants and variables

Guess you like

Origin blog.csdn.net/CSDN_C2/article/details/105656031