...

View Full Version : trouble with AND and OR



Crash1hd
10-20-2003, 08:53 PM
The following code does not work!

If Bgpic <> "Custom" or Bgpic <> "10" or Bgpic <> "" Then Response.Write "blah"

but if I where to do this

If Bgpic <> "Custom" Then Response.Write "blah"

or

If Bgpic <> "10" Then Response.Write "blah"

or

If Bgpic <> "" Then Response.Write "blah"

it would work fine so how do I save on the typing what am I doing wrong with the first code?

raf
10-20-2003, 09:11 PM
I don't realy see anything wrong with it.
Maybe try

If (Bgpic <> "Custom") or (Bgpic <> "10") or (Bgpic <> "") Then
Response.Write "blah"
end if

or

If (Bgpic <> "Custom") or (Bgpic <> "10") or (Len(Bgpic) <> 0) Then
Response.Write "blah"
end if

or

If (Bgpic <> "Custom") or (Bgpic <> "10") or (Bgpic <> Nothing) Then
Response.Write "blah"
end if


What do you try to do with (Bgpic <> "") ?

Roy Sinclair
10-20-2003, 10:21 PM
It's a simple logic error. You're using OR when you should be using AND.

If Bgpic <> "Custom" and Bgpic <> "10" and Bgpic <> "" Then Response.Write "blah"

raf
10-20-2003, 10:30 PM
<edit> misread </edit>

Crash1hd
10-21-2003, 09:09 AM
Geeze :) lol I never can remember when it should be AND or OR thanks all

P.s. it was so that when the 3 features 10 Custom and blank or "" where not choosen!

whammy
10-22-2003, 05:37 AM
Not to mention, raf DID have the right idea... whenever possible GROUP your logic using parentheses - first of all, it prevents programming logic errors.

Also, it makes more sense if another developer has to (or you have to) update your code later on. For instance:

If a = 1 and c=1 or b=1 Then
'do something
End If

doesn't make much sense, although it appears to, at first... can you see how that's ambiguous? I could have probably coded a better example, but anyway:

If (a = 1 and c=1) or b=1 Then
'do something
End If

See how the logic differs there? There are a LOT of cases in Classic ASP (not to mention other languages!) where you will err, if you don't group your logic. Also, languages may differ in their "parsing order" when it comes to "and" or "or" logic (to explain it simply). If you enclose your logic in parentheses that you want calculated AS A GROUP (which is what I mean by grouping), then it should solve the problem.

If you don't see the difference, compare this:

If a = 1 and (c=1 or b=1) Then
'do something
End If

Then it will become clear to you... not to mention this kind of logic "parsing" may be different depending upon which language you're using. Grouping pretty much solves that problem in all of the languages I use... and will save you hours of time, trying to figure out what's wrong - because that type of error is NOT immediately obvious!

In addition, it makes your intentions much clearer. For you or whoever else may be updating your code in the future.

Crash1hd
10-22-2003, 08:52 AM
Excelent :) I will be rewriting my code to match is there a difference between the following?

If (a = 1 and c = 1) or b = 1 Then
'do something
End If

and

If (a = "1" and c = "1") or b = "1" Then
'do something
End If

What do the "" quotes do anyhow?

raf
10-22-2003, 09:00 AM
values of stringvalues need to be enclosed in "
values of numerical values should not be enclosed.

so


dim a,b
a=1
b=1
response.write(a+b) ' prints 2 --> + as a operator for numerical vaules, sums the values
a="1"
b="1"
response.write(a+b) ' prints 11 --> + as a stringvariables operator, concatenates the values


It's important to realise that all values from the form and querystring-collection, are stringvariable !


response.write(request.form("a") + request.form("b")) 'prints 11 if you entered 1 in both fields !!

Crash1hd
10-22-2003, 09:48 AM
Ok I am still having trouble with the following

If (Mthoughts = "no" AND (MthoughtsPass = "" OR MUthoughtsPass = "")) AND (Mphoto = "no" AND (MphotoPass = "" OR MuploadPass = "") ) Then

do whatever

else

do this

end if

so what I want is if Mthoughts = no then the 2 pass values have to be blank but if Mthoughts = yes then ignore as if its yes then the 2 pass values are allowed? same with Mphoto

raf
10-22-2003, 11:59 AM
i don't quite understand the explanation, but i tend to use
if-then-else constructs in such case. Might be a bit more typing, but it sure is easier to read/understand later on.

I suppose you need


If (Mthoughts = "no" AND (MthoughtsPass = "" AND MUthoughtsPass = "")) OR (Mphoto = "no" AND (MphotoPass = "" AND MuploadPass = "") ) Then

do whatever

else

do this

end if

or


dim situation
situation = 2
If (Mthoughts = "no" then
if (MthoughtsPass = "") AND (MUthoughtsPass = "") then
situation=1
end if
end if

if Mphoto = "no" then
if (MphotoPass = "") AND (MuploadPass = "") Then
situation=1
end if
end if
if situation=1 then
do whatever
else
do this
end if



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum