Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

help with IF, ELSE IF and ELSE. ASP/VB

LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

Hi,

I have a numerical value (currency) taken from a database field which I
display on an ASP page. I want to use IF, ELSE IF and ELSE, assuming that's
the best way to go about it, to add mark up charges to the value in the
database.

For example, if the value in the database field is equal to, or less than,
45, then 5 is added:

So far I've got:

<%IF (rsRecordset.Fields.Item("price7day").Value) <= 45 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+5), -1, -2, -2,
-2) %>
<%End IF%>

This works a treat! What I'm stuck with is how to then apply a different
charge if the number is BETWEEN two values. I tried this:

<%IF (rsRecordset.Fields.Item("price7day").Value) <= 45 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+5), -1, -2, -2,
-2) %>
<%Else IF (rsRecordset.Fields.Item("price7day").Value) >= 45.01 AND <= 99.99
Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+10), -1, -2, -2,
-2) %>
<%End IF%>

...but this isn't working, producing an error at the word "AND".
Can someone show me how this should be laid out? Much appreciated.
Regards
nathon.


TOPICS
Server side applications

Views

578
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

On 03 Jul 2006 in macromedia.dreamweaver.appdev, Nathon Jones wrote:

> <%IF (rsRecordset.Fields.Item("price7day").Value) <= 45 Then%>
> <%=FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+5), -1,
> -2, -2, -2) %>
> <%Else IF (rsRecordset.Fields.Item("price7day").Value) >= 45.01 AND
> <= 99.99 Then%>

At the point of the ELSEIF, you already know that the value is > 45,
because it got past the first condition. So the ELSEIF should be:

<% ElseIF (rsRecordset.Fields.Item("price7day").Value) <= 99.99 Then %>

If you want to make it work with the AND conditional (you don't have to),
you'd need to include the value to compare with, thus:

<% ElseIF (rsRecordset.Fields.Item("price7day").Value) >= 45.01
AND (rsRecordset.Fields.Item("price7day").Value) <= 99.99 Then %>

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

You need to repeat the statement. So instead of having

if a > 1 and <2

have

if a>1 and a<2

--
Jules
http://www.charon.co.uk/charoncart
Charon Cart 3
Shopping Cart Extension for Dreamweaver MX/MX 2004





Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

Terrific Joe, thank you.

If there was another variable, for example:

1) If the value <= 45 then add 5
2) If the value is >= 45.01 AND <= 99.9 then add 10
3) If the value is >= 100 then add 15

...would I then have to use the AND conditional? Or because it, in effect,
fails the first two conditions it will automatically work?
I'll play about with it and see what happens. Really appreciate your help
Joe. Thank you.

Regards
Nathon.

"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns97F5536D5B483makowiecatnycapdotrE@216.104.212.96...
> On 03 Jul 2006 in macromedia.dreamweaver.appdev, Nathon Jones wrote:
>
>> <%IF (rsRecordset.Fields.Item("price7day").Value) <= 45 Then%>
>> <%=FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+5), -1,
>> -2, -2, -2) %>
>> <%Else IF (rsRecordset.Fields.Item("price7day").Value) >= 45.01 AND
>> <= 99.99 Then%>
>
> At the point of the ELSEIF, you already know that the value is > 45,
> because it got past the first condition. So the ELSEIF should be:
>
> <% ElseIF (rsRecordset.Fields.Item("price7day").Value) <= 99.99 Then %>
>
> If you want to make it work with the AND conditional (you don't have to),
> you'd need to include the value to compare with, thus:
>
> <% ElseIF (rsRecordset.Fields.Item("price7day").Value) >= 45.01
> AND (rsRecordset.Fields.Item("price7day").Value) <= 99.99 Then %>
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

Cheers Jules.

Regards
Nath.

"Julian Roberts" <nospam@charon.co.uk> wrote in message
news:e8b1pv$5cf$1@forums.macromedia.com...
> You need to repeat the statement. So instead of having
>
> if a > 1 and <2
>
> have
>
> if a>1 and a<2
>
> --
> Jules
> http://www.charon.co.uk/charoncart
> Charon Cart 3
> Shopping Cart Extension for Dreamweaver MX/MX 2004
>
>
>
>
>


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

On 03 Jul 2006 in macromedia.dreamweaver.appdev, Nathon Jones wrote:

> Terrific Joe, thank you.
>
> If there was another variable, for example:
>
> 1) If the value <= 45 then add 5
> 2) If the value is >= 45.01 AND <= 99.9 then add 10
> 3) If the value is >= 100 then add 15
>
> ...would I then have to use the AND conditional?

No.

> Or because it, in effect, fails the first two conditions it will
> automatically work?

Every time you go up through another level, you've eliminated lower
levels, so you don't have to make that comparison again. Walk through
the tests with values of 40, 60 and 110

40:
Value <= 45? T
Value <= 100? Doesn't get here
Value > 100? Doesn't get here

60:
Value <= 45? F
Value <= 100? T
Value > 100? Doesn't get here

110:
Value <= 45? F
Value <= 100? F
Value > 100? T

By the way, watch the edge conditions. Between 2 and 3 above, you have
x <= 99.9 and x >= 100. What happens when the value is 99.93?

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

Hi Joe,

Thanks again for helping me with this. Just to clarify what you mean here:
"By the way, watch the edge conditions. Between 2 and 3 above, you have x
<= 99.9 and x >= 100. What happens when the value is 99.93?"

Item 2, in my last message, is "if VALUE is greater than or equal to 45.01
and VALUE is less than or equal to 99.9" then add "10".

If the value was 99.93, then 10 would be added because it is less than, or
equal to, 99.9? Isn't it?
Can you explain what edge conditions are and what I've done wrong there? Do
you mean that I should simply make sure to write "99.99"? Whoops, sorry
about that! That's what I meant, but I appreciate you are taking it as
read - thank you for pointing that out.

Really appreciate it. Again, thanks for sorting me out!
Regards
Nathon.

"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns97F55A527BA95makowiecatnycapdotrE@216.104.212.96...
> On 03 Jul 2006 in macromedia.dreamweaver.appdev, Nathon Jones wrote:
>
>> Terrific Joe, thank you.
>>
>> If there was another variable, for example:
>>
>> 1) If the value <= 45 then add 5
>> 2) If the value is >= 45.01 AND <= 99.9 then add 10
>> 3) If the value is >= 100 then add 15
>>
>> ...would I then have to use the AND conditional?
>
> No.
>
>> Or because it, in effect, fails the first two conditions it will
>> automatically work?
>
> Every time you go up through another level, you've eliminated lower
> levels, so you don't have to make that comparison again. Walk through
> the tests with values of 40, 60 and 110
>
> 40:
> Value <= 45? T
> Value <= 100? Doesn't get here
> Value > 100? Doesn't get here
>
> 60:
> Value <= 45? F
> Value <= 100? T
> Value > 100? Doesn't get here
>
> 110:
> Value <= 45? F
> Value <= 100? F
> Value > 100? T
>
> By the way, watch the edge conditions. Between 2 and 3 above, you have
> x <= 99.9 and x >= 100. What happens when the value is 99.93?
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

On 03 Jul 2006 in macromedia.dreamweaver.appdev, Nathon Jones wrote:

> Thanks again for helping me with this. Just to clarify what you
> mean here: "By the way, watch the edge conditions. Between 2 and 3
> above, you have x <= 99.9 and x >= 100. What happens when the value
> is 99.93?"
>
> Item 2, in my last message, is "if VALUE is greater than or equal to
> 45.01 and VALUE is less than or equal to 99.9" then add "10".
>
> If the value was 99.93, then 10 would be added because it is less
> than, or equal to, 99.9? Isn't it?

Is 99.93 < 99.9?

It's more conventional to use something like:

Condition 1: x < 100
Condition 2: x >= 100

That will catch everything (range is continuous); if you use <= for the
first, and >= for the second, you have problems at 100:

Condition 1: x <= 100
Condition 2: x >= 100

Ranges overlap - which one will get picked? Generally, the first, but
it could be ambiguous. And if you use something like:

Condition 1: x <= 99.9
Condition 2: x >= 100

Range is discontinuous - you're missing 99.9 < x < 100. You run the
risk of getting 99.93 - I don't know where your numbers are coming
from, but if they're calculated, you run the risk of getting a number
in between due to roundoff. If you can be positive that your numbers
will never have a value in that range, you'll be OK; however, given how
computers think, I never make that assumption. Always make sure your
numeric ranges are continuous without overlapping.

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

Hi Joe,

Thanks again for bearing with me. So is this purely because I wrote 99.9
rather than, let's say, 99.90? Would specifying the 2nd decimal prevent any
potential problem there? Here is what I now have:

<%IF (rsRecordset.Fields.Item("price7day").Value) <= 45.00 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+5), -1, -2, -2,
-2) %>

<%ElseIF (rsRecordset.Fields.Item("price7day").Value) <= 99.99 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+8), -1, -2, -2,
-2) %>

<%ElseIF (rsRecordset.Fields.Item("price7day").Value) <= 150.00 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+12), -1, -2, -2,
-2) %>

<%ElseIF (rsRecordset.Fields.Item("price7day").Value) <= 200.00 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+20), -1, -2, -2,
-2) %>

<%ElseIF (rsRecordset.Fields.Item("price7day").Value) > 200.01 Then%>
<%=
FormatCurrency(((rsRecordset.Fields.Item("price7day").Value)+30), -1, -2, -2,
-2) %>
<%End IF%>

And these are the mark-ups I need to apply to the currency field
("price7day") in the database:
0-�45 - add �5
�45.01-�99.99 - add �8
�100.00-�150.00 - add �12
�150.01-�200.00 - add �20
�200.01+ - add �30

Anything in there that could cause me a problem?
The numbers are purely currency values so they may well be entered into the
database as 99.9 rather than 99.90.

What I'm not understanding in your reply is Condition 1 and Condition 2? I
only have one condition for each, which is what I thought you were
recommending, rather than using AND?

Hope you can stick with me on this - although I appreciate you may begin to
tire of my novice status!

Regards
Nath.

"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns97F56EE0CF45AmakowiecatnycapdotrE@216.104.212.96...
> On 03 Jul 2006 in macromedia.dreamweaver.appdev, Nathon Jones wrote:
>
>> Thanks again for helping me with this. Just to clarify what you
>> mean here: "By the way, watch the edge conditions. Between 2 and 3
>> above, you have x <= 99.9 and x >= 100. What happens when the value
>> is 99.93?"
>>
>> Item 2, in my last message, is "if VALUE is greater than or equal to
>> 45.01 and VALUE is less than or equal to 99.9" then add "10".
>>
>> If the value was 99.93, then 10 would be added because it is less
>> than, or equal to, 99.9? Isn't it?
>
> Is 99.93 < 99.9?
>
> It's more conventional to use something like:
>
> Condition 1: x < 100
> Condition 2: x >= 100
>
> That will catch everything (range is continuous); if you use <= for the
> first, and >= for the second, you have problems at 100:
>
> Condition 1: x <= 100
> Condition 2: x >= 100
>
> Ranges overlap - which one will get picked? Generally, the first, but
> it could be ambiguous. And if you use something like:
>
> Condition 1: x <= 99.9
> Condition 2: x >= 100
>
> Range is discontinuous - you're missing 99.9 < x < 100. You run the
> risk of getting 99.93 - I don't know where your numbers are coming
> from, but if they're calculated, you run the risk of getting a number
> in between due to roundoff. If you can be positive that your numbers
> will never have a value in that range, you'll be OK; however, given how
> computers think, I never make that assumption. Always make sure your
> numeric ranges are continuous without overlapping.
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2006 Jul 03, 2006

Copy link to clipboard

Copied

LATEST

> Anything in there that could cause me a problem?
> The numbers are purely currency values so they may well be entered into
> the database as 99.9 rather than 99.90.
>
> What I'm not understanding in your reply is Condition 1 and Condition 2?
> I only have one condition for each, which is what I thought you were
> recommending, rather than using AND?

99.93 > 99.90 and will fail your test, so you still have a gap in your range
from 99.90 to 99.9999....etc....
Currency values are (generally) stored with greater precision, often out to
four decimal places.
When setting a range by using ELSEIF, the first and only the first positive
wins, so you need only test one side of the range. Only use = when you mean
it.

Example:

If X < 50 Then
Response.Write("What a small purchase!")
ElseIf X < 100 Then
Response.Write("Thanks for buying some stuff.")
ElseIf X < 150 Then
Response.Write("Thanks for being a good customer.")
Else
Response.Write("You're a great customer!")
End If

Purchases up to but not including 50 pounds will see "What a small
purchase!"
Purchases 50 pounds up to but not including 100 pounds will see "Thanks for
buying some stuff."
And so on and so forth. Real numbers are continuous; to avoid problems, you
must treat them as such.


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines