View Full Version : if and else statemenst (2Q.s)
scroots
10-25-2002, 07:49 PM
i`m only asking as i havent seen any good examples and i would like to know as this sort of thing is very handy to also i need it for a project.
what is the correct way of having an if or an else statement tht referes to a record field in a database e.g.
if suchfield = 1
Response.Write ("1111")
else
Response.Write ("")
2) also what about if or or. like a field contains a single number, how can i check to see if the number is a 1 or a 2 and write a statement based upon it.
if suchfield = 1 or 2
Response.Write ("1111")
else
Response.Write ("")
thanks in advance.
scroots
BigDaddy
10-25-2002, 09:09 PM
Originally posted by scroots
i`m only asking as i havent seen any good examples and i would like to know as this sort of thing is very handy to also i need it for a project.
what is the correct way of having an if or an else statement tht referes to a record field in a database e.g.
if suchfield = 1
Response.Write ("1111")
else
Response.Write ("")
2) also what about if or or. like a field contains a single number, how can i check to see if the number is a 1 or a 2 and write a statement based upon it.
if suchfield = 1 or 2
Response.Write ("1111")
else
Response.Write ("")
thanks in advance.
scroots
1.
IF rs("suchfield") = 1 Then
response.write("1111")
Else
response.write("blah"
End IF
--- Not sure what you're looking for on this. Instead of response.write (""), meaning not writing anything, you can just end it without an "else" clause.
Example:
IF rs("suchfield") = 1 THEN
response.write("1111")
END IF
2.
IF rs("suchfield") = 1 or rs("suchfield") = 2 THEN
Response.Write ("1111")
else
Response.Write ("")
End If
Again, if you're not writing anything for the 2nd one, just don't have an "else" clause on the if statement.
Is that what you're looking for?
scroots
10-26-2002, 07:39 PM
thank you, i will now know how to do this as it will be a common thing that i will need in some of my projects.
thank you
scroots
whammy
10-26-2002, 09:53 PM
Dim mystring
mystring = "Yay!"
If mystring = "Yay!" Then
'Do something
ElseIf mystring = "Boo!" Then
'Do something else
Else
'Do something else
End If
'OR
If mystring = "Yay!" Then Response.Write("Yay!") End If
Also, you may want to look at the Select Case syntax, since in some situations it's easier to use and runs faster (it's like switch() in javascript and other languages):
Dim mystring
mystring = "Yay!"
Select Case mystring
Case "Yay!"
'Do something here
Case "Boo!"
'Do something else here
Case Else
'Do yet something else
End Select
glenngv
10-28-2002, 07:05 AM
Originally posted by whammy
If mystring = "Yay!" Then Response.Write("Yay!") End If
if you only have one statement in the If ... then statement and put it in one line, you don't need End If :)
If mystring = "Yay!" Then Response.Write("Yay!")
whammy
10-28-2002, 11:47 PM
I know, actually I rarely use it in one liners ...actually, I've used one-liners with an else in them too, and they work fine without an End If. i.e.:
If mystring = "Yay!" Then Response.Write("Yay!") Else Response.Write("Boo!")
;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.