Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    trouble with AND and OR

    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?

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 <> "") ?

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <edit> misread </edit>

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by whammy; 10-22-2003 at 06:01 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    values of stringvalues need to be enclosed in "
    values of numerical values should not be enclosed.

    so
    Code:
    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 !
    Code:
    response.write(request.form("a") + request.form("b"))    'prints 11 if you entered 1 in both fields !!

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by Crash1hd; 10-22-2003 at 09:54 AM.

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    If (Mthoughts = "no" AND (MthoughtsPass = "" AND MUthoughtsPass = "")) OR (Mphoto = "no" AND (MphotoPass = "" AND MuploadPass = "") ) Then
    
    do whatever
    
    else
    
    do this
    
    end if
    or
    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •