468,505 Members | 1,905 Online

# How to simplify this long if else statement?

24
I have 3 products. All the products have the same price, I named it : price

Each product has a discount. I defined its variables as follows:
discount1, discount2, discount3

But there is a different tax for each product when the discount equals 100% (for example) as follows:
tax1, tax2, tax3

I wrote this code and it works well:
Expand|Select|Wrap|Line Numbers
1. if discount1 = 100 and discount2 = 100 and discount3 = 100 then
2. total_price = price - tax1 - tax2 - tax3
3.
4. elseif discount2 = 100 and discount3 = 100 then
5. total_price = price - tax2 - tax3
6.
7. elseif discount1 = 100 and discount3 = 100 then
8. total_price = price - tax1 - tax3
9.
10. elseif discount1 = 100 and discount2 = 100 then
11. total_price = price - tax1 - tax2
12.
13. elseif discount1 = 100 then
14. total_price = price - tax1
15.
16. elseif discount2 = 100 then
17. total_price = price - tax2
18.
19. elseif discount3 = 100 then
20. total_price = price - tax3
21.
22. else
23. total_price = price
24.
25. end if
26.
But I need a simple and short code that do the same functionality because when new products are added, the code will be large and difficult to follow.
Feb 5 '11 #1

So then you don't actually have fields named tax1, discount1, etc.?

Then everything can be handled with a SQL statement. It depends on what SQL server you're using but for Microsoft SQL Server, I would do something like this
Expand|Select|Wrap|Line Numbers
1. SELECT (SUM(price) -
2.   SUM(CASE discount WHEN 100 THEN tax ELSE 0 END)
3.   ) AS total_price
4. FROM Table1

19 2716
jhardman
3,405 Expert 2GB
Expand|Select|Wrap|Line Numbers
1. total_price = price
2. If discount1 = 100 then
3.    total_price = total_price - tax1
4. End if
5. If discount2 = 100 then
6.    total_price = total_price - tax2
7. End if
8. If discount3 = 100 then
9.    total_price = total_price - tax3
10. End if
but it would be even better if it was arrays or something similar that you could loop through.

Jared
Feb 5 '11 #2
Rabbit
12,513 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
1. total_price = price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100)
Feb 5 '11 #3
goodamr
24
@Jhardman: The problem is that the 3 condition can happen at the same time
Feb 6 '11 #4
Rabbit
12,513 Expert Mod 8TB
His code accounts for that. So does mine. Although that depends on if the language you're using has an integer division operator. If not, you can use an integer conversion function or floor function.
Feb 6 '11 #5
goodamr
24
@Rabbit: Your statement is not what I need. If you can simplify it using arrays or for loop.
Feb 6 '11 #6
Rabbit
12,513 Expert Mod 8TB
The results will be the same. Why do you have to use a loop?
Feb 6 '11 #7
goodamr
24
The issue is that: We subtract the (tax) variable from the (price) only when the (discount) equals 100
Feb 6 '11 #8
Rabbit
12,513 Expert Mod 8TB
Exactly. Both jhardman's code and my code do what you want. Well, for mine you may need to use a different function if asp doesn't have an integer division operator but the concept is the same.
Feb 6 '11 #9
goodamr
24
Oh sorry, Jhardman's code is correct. But your code is not correct. suppose the discount is 50 so we don't have to subtract the tax from the price.
Feb 6 '11 #10
Rabbit
12,513 Expert Mod 8TB
I know. It accounts for that.
Feb 6 '11 #11
goodamr
24
Also, suppose we have more than three variables for example
discount1, discount2,......, discount100 and tax1, tax2,...., tax100

I think it will be hard to maintain the code. I think we should have an array or a for loop, but how can I accomplish that. so any solution?

Thanks.
Feb 6 '11 #12
Rabbit
12,513 Expert Mod 8TB
It sounds like you're having trouble understanding the formula. Let's break it down.

Given:
Expand|Select|Wrap|Line Numbers
1. price = 1234
2. tax1 = 200
3. discount1 = 50
4. tax2 = 30
5. discount2 = 100
6. tax3 = 4
7. discount3 = 4
8.
9. discount1 \ 100 = 50 \ 100 = 0
10. discount2 \ 100 = 100 \ 100 = 1
11. discount3 \ 100 = 100 \ 100 = 1
12.
13. tax1 * (discount1 \ 100) = 200 * 0 = 0
14. tax2 * (discount2 \ 100) = 30 * 1 = 30
15. tax3 * (discount3 \ 100) = 4 * 1 = 4
16.
17. price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100)
18. price - 0 - 30 - 4 = 1234 - 0 - 30 - 4 = 1200
Feb 6 '11 #13
Rabbit
12,513 Expert Mod 8TB
It sounds like you have the data in a database. What is the table structure?
Feb 6 '11 #14
goodamr
24
Yes, I have the data in a database. Here is the table structure:

student_id
Year
Price
Discount
Tax

I need to calculate the total price in all years and when the discoutn = 100 we subtrat the tax from the related price.

Thanks alot for helping me.
Feb 6 '11 #15
Rabbit
12,513 Expert Mod 8TB
Can you give me some sample data?
Feb 6 '11 #16
goodamr
24
2100694445,2007,3200,100,150
2100694445,2008,3600,100,250
2100694445,2009,2200,100,50
2100694445,2010,4500,100,250

Thanks.
Feb 6 '11 #17
Rabbit
12,513 Expert Mod 8TB
So then you don't actually have fields named tax1, discount1, etc.?

Then everything can be handled with a SQL statement. It depends on what SQL server you're using but for Microsoft SQL Server, I would do something like this
Expand|Select|Wrap|Line Numbers
1. SELECT (SUM(price) -
2.   SUM(CASE discount WHEN 100 THEN tax ELSE 0 END)
3.   ) AS total_price
4. FROM Table1
Feb 6 '11 #18
goodamr
24
I'm using Oracle 9
Feb 6 '11 #19
jhardman
3,405 Expert 2GB
Yeah, the query rabbit gave last is the best solution, you will need to make sure that syntax works in oracle, but otherwise, nice solution.

Jared
Feb 6 '11 #20