How to simplify this long if else statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goodamr
    New Member
    • Jun 2007
    • 24

    How to simplify this long if else statement?

    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:
    Code:
    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.
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    Code:
    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

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Code:
      total_price = price - tax1 * (discount1 \ 100) - tax2 * (discount2 \ 100) - tax3 * (discount3 \ 100)

      Comment

      • goodamr
        New Member
        • Jun 2007
        • 24

        #4
        @Jhardman: The problem is that the 3 condition can happen at the same time

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          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.

          Comment

          • goodamr
            New Member
            • Jun 2007
            • 24

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

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              The results will be the same. Why do you have to use a loop?

              Comment

              • goodamr
                New Member
                • Jun 2007
                • 24

                #8
                The issue is that: We subtract the (tax) variable from the (price) only when the (discount) equals 100

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  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.

                  Comment

                  • goodamr
                    New Member
                    • Jun 2007
                    • 24

                    #10
                    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.

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      I know. It accounts for that.

                      Comment

                      • goodamr
                        New Member
                        • Jun 2007
                        • 24

                        #12
                        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.

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          It sounds like you're having trouble understanding the formula. Let's break it down.

                          Given:
                          Code:
                          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

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            It sounds like you have the data in a database. What is the table structure?

                            Comment

                            • goodamr
                              New Member
                              • Jun 2007
                              • 24

                              #15
                              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.

                              Comment

                              Working...