Thread: if else if nor working properlly?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    if else if nor working properlly?

    Hello

    I have written a fairly large program, and everything else works fine, but there is one function that has problems. I'll try to show it with the code.

    Code:
    double ft(double inputGrossPay)
    {
    double finalFt, firstFt = 0, secondFt = 0, thirdFt = 0;
    
      if (inputGrossPay <= 20000)
      {
       inputGrossPay = inputGrossPay * 52;
       finalFt = ((inputGrossPay * 0.16) / 52 * 1.47);
      }
    
      else if (inputGrossPay >= 20000 && inputGrossPay <= 40000)
      {
       inputGrossPay = inputGrossPay * 52;
       secondFt =  inputGrossPay - 20000;
       firstFt = inputGrossPay - secondFt;
       finalFt = ((firstFt * 0.16 + secondFt * 0.23) / 52 * 1.47);
      }
    
    
      else if (inputGrossPay > 40000)
      {
       inputGrossPay = inputGrossPay * 52;
       thirdFt = inputGrossPay - 40000;
       secondFt =  inputGrossPay - thirdFt;
       firstFt =  inputGrossPay - secondFt;
       finalFt = ((firstFt * 0.16 + secondFt * 0.23 + thirdFt * 0.29) / 52 * 1.47);
      }
    
    return finalFt;
    }


    notice how

    Code:
      if (inputGrossPay <= 20000)
      {
       inputGrossPay = inputGrossPay * 52;
       finalFt = ((inputGrossPay * 0.16) / 52 * 1.47);
      }
    is on top of

    Code:
    else if (inputGrossPay >= 20000 && inputGrossPay <= 40000)
      {
       inputGrossPay = inputGrossPay * 52;
       secondFt =  inputGrossPay - 20000;
       firstFt = inputGrossPay - secondFt;
       finalFt = ((firstFt * 0.16 + secondFt * 0.23) / 52 * 1.47);
      }
    in this case, when I run the program, some of the answers are correct and some are wrong,
    but when I change there order for example if

    Code:
    if (inputGrossPay >= 20000 && inputGrossPay <= 40000)
      {
       inputGrossPay = inputGrossPay * 52;
       secondFt =  inputGrossPay - 20000;
       firstFt = inputGrossPay - secondFt;
       finalFt = ((firstFt * 0.16 + secondFt * 0.23) / 52 * 1.47);
      }
    goes on top of

    Code:
     
    else if (inputGrossPay <= 20000)
      {
       inputGrossPay = inputGrossPay * 52;
       finalFt = ((inputGrossPay * 0.16) / 52 * 1.47);
      }
    than the answers that where right get wrong and the wrong ones get right, it seems that only the first if statement runs.

    Why is this happening?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What was the expected behavior and what was the resulting behavior?

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by arya6000 View Post
    Hello

    I have written a fairly large program, and everything else works fine, but there is one function that has problems. I'll try to show it with the code.
    Code:
      if (inputGrossPay <= 20000)
      {
         ...
      }
    
      else if (inputGrossPay >= 20000 && inputGrossPay <= 40000)
      {
         ...
      }
    Notice that if inputGrossPay is exactly 20000, either of these blocks could execute. Because if x = 1, then x <= 1 is true AND x >= 1 is true. Whichever is first is the one which executes. Perhaps you want this instead:

    Code:
      if (inputGrossPay <= 20000)
      {
         ...
      }
    
      else if (inputGrossPay > 20000 && inputGrossPay <= 40000)
      {
         ...
      }
    And the same adjustment for the other case as well.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, notice these two things (not related to your issue)

    You have
    Code:
    if (...) {...} 
    else if (...) {...} 
    else if (...) {...}
    The last "else if" should merely be an "else".

    Also, from the department of redundancy department, each branch of the if/else ladder is doing the exact same calculation as the first expression:

    Code:
    inputGrossPay = inputGrossPay * 52;
    You could remove it from the "if/else" ladder and place it at the top. One line of code to maintain and write versus 3.

    Todd

Popular pages Recent additions subscribe to a feed