Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Flexible and practical method of VBS

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "flexible and practical methods of VBS". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the "flexible and practical VBS method"!

Select structure

Select the structure, that is, judge the conditions, and then choose to execute the structure of different branches according to the result of the judgment. The common ones are IF conditional statement and Select Case statement.

● IF conditional statement

IF conditional statements have a variety of specific forms, such as If. Then, If... Then... Else, If... Then... ElseIf . The following are illustrated by routines. Write a VBS script in your notebook with the following:

Dim a,b

A=inputbox ("enter the first number", "enter")

B=inputbox ("enter second number", "enter")

If a > b then

MsgBox ("the first number is greater than the second number")

End if

After running, the user is asked to enter two numbers, the value of the first number is assigned to a, the value of the second number is assigned to b, and then the program automatically judges an and b. If the value of an is greater than the value of b, a pop-up message box shows that "the first number is greater than the second number". Note: when the statement after then is not on the same line as then, add a line of End if to the end of the judgment statement, otherwise the program will end. If you are on the same line, you don't need to add End if. For example, the above statement can be changed to:

Dim a,b

A=inputbox ("enter the first number", "enter")

B=inputbox ("enter second number", "enter")

If a > b then MsgBox ("the first number is greater than the second number")

You can use multiple If statements in another program. We can refine the above program as follows:

Dim a,b

A=inputbox ("enter the first number", "enter")

B=inputbox ("enter second number", "enter")

If a > b then

MsgBox ("the first number is greater than the second number")

End if

If axib then

MsgBox ("first number equals second number")

End if

If ab then

MsgBox ("the first number is greater than the second number")

Else

IF axib then

MsgBox ("first number equals second number")

Else

MsgBox ("first number is less than second number")

End if

End if

--

Dim a,b

A=inputbox ("enter the first number", "enter")

B=inputbox ("enter second number", "enter")

If a > b then

MsgBox ("the first number is greater than the second number")

Elseif axib then

MsgBox ("first number equals second number")

Else

MsgBox ("first number is less than second number")

End if

● Select Case statement

Select Case statement is another representation of multi-branch structure. It has the characteristics of intuitive representation, simple structure, and does not easily lead to confusion. Its syntax form is as follows:

Select Case variable or expression

Case expression 1

Statement 1

Case expression 2

Statement 2

End Select

Now let's use the Select Case sentence to write a program that converts numbers into English and experience the advantages of Select Case, Let's goggles!

Dim a

A=inputbox ("enter a number", "enter")

Select Case a

Case 1

MsgBox ("the number you entered is One")

Case 2

MsgBox ("the number you entered is Two")

Case 3

MsgBox ("the number you entered is Tree")

Case 4

MsgBox ("the number you entered is Four")

End Select

From the above, do you think the Select Case sentence is very intuitive? if you use the If sentence, it will be very confusing, and even make people dizzy. Also note that no matter how many branches there are in a conditional statement, as long as one branch is executed, the rest are no longer executed.

Cyclic structure

● For Loop statement

The For statement is used to control the loop structure for which the number of loops is known, as follows:

For loop variable = initial value To final value [step]

Statement

[Exit for] 'exit loop statement

Next

Now let's write a program that calculates the sum of integers from 1 to 100 to learn more about the use of For. The program is as follows:

Dim s

Sausage 0

For iTunes 1 to 100

S=s+i

Next

Msgbox (s)

This program is also one of the classic programs, it first declares a variable S, and then assigns an initial value of 0 to to S so that as I adds 1 each time, the statement s=s+i in the loop also accumulates with the increase of I until the end of the cycle. The Next statement moves the body of the loop into the next loop.

● Do... Loop loop statement

The For loop statement is used to control a loop with a known number of loops, while Do... Loop is used to control the number of unknown cycles, according to the set conditions to control the cycle. It has two forms, one is to judge before execution, and the other is to execute first and then judge.

Judge and then execute the grammatical form:

Do [while/until condition]

Statement

[Exit Do] 'exit the loop

Loop

Execute first and then judge grammatical form:

Do

Statement

[Exit Do]

Loop [while/until condition]

Let's use Do... Loop statement to rewrite the above program that calculates the sum of integers from 1 to 100, as follows:

Execute first and judge later

Dim s

Sausage 0

ITunes 0

Do

I=i+1

S=s+i

Loop until i > = 100

Msgbox (s)

Judge first and then execute:

Dim s

Sausage 0

ITunes 0

Do while s

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report