ASP Getting Started Tutorial-single condition selection structure

Source: Internet
Author: User

Single condition selection structure is the most commonly used two-branch selection structure, which is characterized by: if the value of the given condition (condition expression) is true, the X1 block is executed, otherwise the X2 block is executed.

1, Line If statement

The syntax format is:

if<条件> then [ <语句1> ][Else <语句2> ]

Simple example:

<title>Msgbox()函数的应用</title>
<body>
<script language="VBScript" for="button1" event="onClick">
dim myWeek,a
myWeek=WeekDay(Now)-1
if myWeek=0 then a="要好好休息哟。" else a="努力工作呀!"
msgbox "今天是星期" & myWeek & "," & a
</script>
<center><input type="button" name="button1" value="请查看今天是星期几"></center>

Description

When the program runs to the IF statement, first test the variable myweek is zero, if zero, then execute the Then after the statement (a= "to have a good rest yo." "), otherwise execute the clause after else (a=" To work hard! " ")。 After executing the statement after Then or Else, execution continues from the statement after end If. The ELSE clause is optional. You can combine multiple commands into a single statement by using a colon (:) in the < statement 1> or < statement 2>.

2. Block IF statement

Although the line if statement is easy to use, it can meet the needs of many choice structure programming, but when the Then part and the Else part contain more content, it is difficult to hold all the commands on a single line. To do this, VBScript provides a block IF statement that implements a selection structure with multiple statement lines. The block if statement also antisymmetric the line if statement, whose syntax structure is:

    if <条件> Then
      [语句列1]
    [Else
      [语句列2]]
    End if

Simple example:

<title>块If语句的应用</title>
<body>
<script language="VBScript" for="button1" event="onClick">
dim a,b
a=Text1.value
if Not IsNumeric(a) then
Msgbox "您输入的不是数值数据"
Else
b="您输入的数据是:" & a &",它的平方根是:" & sqr(a)
Msgbox b,,"求一个数的平方根"
End if
</script>
<center>请输入数值数据:<input type="text" name="text1" size="10">
<input type="button" name="button1" value="求一个数的平方根"></center>

3. Nesting of If statements

An If statement can be nested using an if statement in the action block (Statement column 1 or statement 2) of an if statement.

Simple example: railway baggage, from A to B, the provisions of each customer standard shipping fee calculation method is: Baggage weight of not more than 50 kg, 0.5 yuan per kilogram, more than 50 kg more than 100 kg, its more than a portion of 1.5 yuan per kilogram, more than 100 kilograms, its more than a portion of 2 yuan per kilogram. Please calculate and export the shipping charges.

Analysis: The luggage weight is w kg, payable freight is x yuan, then the freight formula is:

When W <=50, the X=0.5*w

When < W <=100, then x=05*50+1.5* (W-50)

When W > 100, then x=0.5*50+1.5*50+ (W-100)

<body>
请输入行李重量:<input name="text2" TYPE="TEXT" size="10">&nbsp;kg&nbsp;&nbsp;
<input type="button" name="button3" value="计算运费">
<script language="VBScript" for="button3" event="onClick">
w=text2.value
if not isNumeric(w) then
msgbox "您输入的不是数值数据"
else
if w <=50 then
x=0.5 * w
else
if w<=100 then
x=0.5*50+1.5*(w-50)
else
x=0.5*50+1.5*50+2*(w-100)
end if
end if
Msgbox "行李的托运费是:"& x & "元",,"计算行李费"
end if
</script>
</body>

See the full set of ASP Getting started tutorials

Related Article

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.