438,868 Members | 1,974 Online Need help? Post your question and get tips & solutions from a community of 438,868 IT Pros & Developers. It's quick & easy.

# How to simplify this long if else statement?

 P: 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 if discount1 = 100 and discount2 = 100 and discount3 = 100 then  total_price = price - tax1 - tax2 - tax3   elseif discount2 = 100 and discount3 = 100 then total_price = price - tax2 - tax3   elseif discount1 = 100 and discount3 = 100 then total_price = price - tax1 - tax3   elseif discount1 = 100 and discount2 = 100 then total_price = price - tax1 - tax2   elseif discount1 = 100 then total_price = price - tax1   elseif discount2 = 100 then total_price = price - tax2   elseif discount3 = 100 then total_price = price - tax3   else total_price = price   end if   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 Replies

 Expert 2.5K+ P: 3,405 Expand|Select|Wrap|Line Numbers total_price = price If discount1 = 100 then    total_price = total_price - tax1 End if If discount2 = 100 then    total_price = total_price - tax2 End if If discount3 = 100 then    total_price = total_price - tax3 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

 Expert Mod 10K+ P: 12,370 Expand|Select|Wrap|Line Numbers total_price = price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100) Feb 5 '11 #3

 P: 24 @Jhardman: The problem is that the 3 condition can happen at the same time Feb 6 '11 #4

 Expert Mod 10K+ P: 12,370 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

 P: 24 @Rabbit: Your statement is not what I need. If you can simplify it using arrays or for loop. Feb 6 '11 #6

 Expert Mod 10K+ P: 12,370 The results will be the same. Why do you have to use a loop? Feb 6 '11 #7

 P: 24 The issue is that: We subtract the (tax) variable from the (price) only when the (discount) equals 100 Feb 6 '11 #8

 Expert Mod 10K+ P: 12,370 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

 P: 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

 Expert Mod 10K+ P: 12,370 I know. It accounts for that. Feb 6 '11 #11

 P: 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

 Expert Mod 10K+ P: 12,370 It sounds like you're having trouble understanding the formula. Let's break it down. Given: Expand|Select|Wrap|Line Numbers price = 1234 tax1 = 200 discount1 = 50 tax2 = 30 discount2 = 100 tax3 = 4 discount3 = 4   discount1 \ 100 = 50 \ 100 = 0 discount2 \ 100 = 100 \ 100 = 1 discount3 \ 100 = 100 \ 100 = 1   tax1 * (discount1 \ 100) = 200 * 0 = 0 tax2 * (discount2 \ 100) = 30 * 1 = 30 tax3 * (discount3 \ 100) = 4 * 1 = 4   price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100) price - 0 - 30 - 4 = 1234 - 0 - 30 - 4 = 1200 Feb 6 '11 #13

 Expert Mod 10K+ P: 12,370 It sounds like you have the data in a database. What is the table structure? Feb 6 '11 #14

 P: 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

 Expert Mod 10K+ P: 12,370 Can you give me some sample data? Feb 6 '11 #16

 P: 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

 Expert Mod 10K+ P: 12,370 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 SELECT (SUM(price) -    SUM(CASE discount WHEN 100 THEN tax ELSE 0 END)   ) AS total_price FROM Table1 Feb 6 '11 #18

 P: 24 I'm using Oracle 9 Feb 6 '11 #19

 Expert 2.5K+ P: 3,405 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 