If statement 预期语句结束,如果

If statement 预期语句结束,如果,if-statement,asp-classic,vbscript,If Statement,Asp Classic,Vbscript,我在表单中设置了以下代码,得到了预期的语句错误。我第一次这么做的时候,我觉得我的语法是正确的,我遗漏了什么 <% If Trim(oQuote("shipmeth"))="FREIGHT" Then Response.Write "Freight" ElseIf Trim(oQuote("shipmeth"))="THRSHLD" Then Resp

我在表单中设置了以下代码,得到了预期的语句错误。我第一次这么做的时候,我觉得我的语法是正确的,我遗漏了什么

            <%
            If Trim(oQuote("shipmeth"))="FREIGHT" Then 
                Response.Write "Freight" 
            ElseIf Trim(oQuote("shipmeth"))="THRSHLD" Then 
                Response.Write "Threshold"
            End If
            %>

我认为elseif应该是一个词,像elseif一样

我认为elseif应该是一个词,像elseif一样

if后面的第一个语句必须在新行上

If Trim(oQuote("shipmeth"))="FREIGHT" Then 
  Response.Write "Freight" 
以下条件可以在同一行上,但必须使用ElseIf

ElseIf Trim(oQuote("shipmeth"))="THRSHLD" Then Response.Write "Threshold"
ElseIf ...
我会建议一个更具可读性的案例

select case Trim(oQuote("shipmeth"))
    Case "THRSHLD"
        Response.Write "Threshold"
    Case "PREMTHRHLD"
        Response.Write "Premium Threshold"
    ...
end select

它的另一个优点是只执行TrimoQuoteshipmeth一次。

If后面的第一条语句必须位于新行

If Trim(oQuote("shipmeth"))="FREIGHT" Then 
  Response.Write "Freight" 
以下条件可以在同一行上,但必须使用ElseIf

ElseIf Trim(oQuote("shipmeth"))="THRSHLD" Then Response.Write "Threshold"
ElseIf ...
我会建议一个更具可读性的案例

select case Trim(oQuote("shipmeth"))
    Case "THRSHLD"
        Response.Write "Threshold"
    Case "PREMTHRHLD"
        Response.Write "Premium Threshold"
    ...
end select

它的另一个优点是只执行TrimoQuoteshipmeth一次。

当使用嵌套的双向条件时,每个条件必须在其自己的一端关闭,如果:

只有n向条件可以用单端关闭,因为它只是一个条件:

If condition_A Then
  action_A
ElseIf condition_B Then
  action_B
ElseIf condition_C Then
  action_C
Else
  action_D
End If
然而,这种n路条件只有在检查不同的条件时才有意义,例如

If IsEmpty(a) Then
  ...
ElseIf b > 23 Then
  ...
检查同一变量的不同值时,最好使用建议的Select语句:

Select Case foo
  Case "a"
    'handle foo="a"
  Case "b", "d"
    'handle foo="b" as well as foo="d"
  Case Else
    'handle non-matches
End Select

当使用嵌套的双向条件时,在以下情况下,每个条件都必须以其自身的结尾关闭:

只有n向条件可以用单端关闭,因为它只是一个条件:

If condition_A Then
  action_A
ElseIf condition_B Then
  action_B
ElseIf condition_C Then
  action_C
Else
  action_D
End If
然而,这种n路条件只有在检查不同的条件时才有意义,例如

If IsEmpty(a) Then
  ...
ElseIf b > 23 Then
  ...
检查同一变量的不同值时,最好使用建议的Select语句:

Select Case foo
  Case "a"
    'handle foo="a"
  Case "b", "d"
    'handle foo="b" as well as foo="d"
  Case Else
    'handle non-matches
End Select

你在这里有很好的答案,我将附加一个稍微优化的替代方案

strTest   = Trim(oQuote("shipmeth"))
strResult = "Unexpected"
If strTest ="FREIGHT" Then strResult = "Freight"
If strTest ="THRSHLD" Then strResult = "Threshold"
Response.Write strResult
编辑

为了澄清我的想法,我讨厌嵌套If..Then,不幸的是VBScript中没有GoTo标签,因此可以使用函数优化上述双重比较

strResult = "Unexpected"
MyResponse Trim(...), strResult
Response.Write strResult

Sub MyResponse(strIn, strOut)
    If strIn = "FREIGHT" Then
        strOut = "Freight" : Exit Sub
    End If
    If strIn = "THRSHLD" Then strOut = "Threshold"
End Sub

你在这里有很好的答案,我将附加一个稍微优化的替代方案

strTest   = Trim(oQuote("shipmeth"))
strResult = "Unexpected"
If strTest ="FREIGHT" Then strResult = "Freight"
If strTest ="THRSHLD" Then strResult = "Threshold"
Response.Write strResult
编辑

为了澄清我的想法,我讨厌嵌套If..Then,不幸的是VBScript中没有GoTo标签,因此可以使用函数优化上述双重比较

strResult = "Unexpected"
MyResponse Trim(...), strResult
Response.Write strResult

Sub MyResponse(strIn, strOut)
    If strIn = "FREIGHT" Then
        strOut = "Freight" : Exit Sub
    End If
    If strIn = "THRSHLD" Then strOut = "Threshold"
End Sub

这是什么语言?请相应地标记。它看起来像vba、vb或vbscript经典版查看响应。写我认为它是ASP经典版,但我可能错了。这是什么语言?请相应地标记。它看起来像vba,vb或vbscript经典版查看响应。Write我认为这是ASP经典版,但我可能错了。我已将其更改为orig post中的代码,但它仅为第一个语句生成输出我已将其更改为orig post中的代码,但它仅为第一个语句生成输出-0.49,用于在该代码上下文中使用optimize在刚刚看到strTest等于运费之后,很高兴地将strTest与THRSHLD和42个其他值进行了比较。@Ekkehard.Horner-将修剪结果存储在变量中主要是我所说的轻度优化注意:粗体轻度。当然,我同意将strTest与其他42个值进行比较。如果是这种情况,我将使用Select,但对于2个值,这几乎就足够了。-0.49对于在代码上下文中使用optimize,在看到strTest与THRSHLD以及其他42个值相等后,很高兴地将strTest与THRSHLD和其他42个值进行比较。@Ekkehard.Horner-将修剪结果存储在变量中主要是我所说的轻度优化注意:粗体轻度。当然,我同意将strTest与其他42个值进行比较。如果是这种情况,我将使用Select,但对于2个值,这几乎足够了。