473,463 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

✓ answered by Rabbit

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 3001
jhardman
3,406 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,516 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,516 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,516 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,516 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,516 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,516 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,516 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,516 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,516 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,406 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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Curious | last post by:
Hi List, Obviously my google-fu is very weak and I can't find a simple sample code for C++ that allows me to simply download the source of a specified web page. I'm looking at downloading the...
1
by: Dave | last post by:
I am looking for a Javascript code to preload 4 images, and display a different image in their place during a mouseover. In other words, 8 images total. I'm not quite finding anything appropriate....
22
by: Tommy | last post by:
Hi all. I am studying computer security, and I got this short and simple (?) c-code. Something is logical wrong in this code, and if used in the wrong hands of someone, it could be taken advantage...
1
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have written an application in studio 2005, when designing all the buttons and so on are displayed just fine. When i execute the application i get the boring buttons like there were in...
7
by: Trickynick1001 | last post by:
Hi, a newbie here. I don't have a real firm grasp on the idea of Javascript, as I'm used to programming in Qbasic and C. I'm not used to OOP. Anyway, I really don't have any idea what the...
6
by: Howard | last post by:
Just curious What is the largest program you have written in terms of source code length and complexity? Howard
14
by: mistral | last post by:
Need compile python code, source is in html and starts with parameters: #!/bin/sh - "exec" "python" "-O" "$0" "$@" I have installed ActivePython for windows.
6
by: santoshsri | last post by:
Hello, Please let me know.. How to check whether the VB code is written in VB3 or VB6 ? Thanks, San
2
by: amuven | last post by:
Hi All, I'm new to python I have written very basic and small code and I request to help for the following senario.For my project i want to write a python code which should open an already...
2
by: papayrus | last post by:
I been trying to simply make a directory in C++ but I dont get what all these people are talking about and they never put a complete code in there I can use that includes the includes ect. Can you...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.