The
for loop on line 22 causes all of the lines between 22 and 34 to execute 5 times. Of those five loop iterations, the tax calculation on line 28 will only execute for those cases where
gross_pay > income_amt[i]; for your example of gross pay of 50001, that would be for
i=0-3, but not 4.
@weaknessforcats has pointed out that The-Taxes-Are print statement is inside the
for-loop but outside of the
if-block so it is executed all 5 times through the loop. Do you want to move it inside the
if-block (so it executes every time the tax is calculated) or perhaps outside of the
for-loop (so it executes exactly once)?
Some other comments:
- Lines 13 and 14: floating-point variables are imprecise. That may be acceptable for scientific calculations, but most people insist on precision for calculations involving money.
- Line 24: the if-condition is of the form if(A && A). The "&& A" portion is useless and redundant. That can't be what you want.
- I assume that your intention is to use
- tax_rates[0] if the gross pay is between income_amt[0] and income_amt[1];
- tax_rates[1] if the gross pay is between income_amt[1] and income_amt[2];
- tax_rates[2] if the gross pay is between income_amt[2] and income_amt[3];
- tax_rates[3] if the gross pay is between income_amt[3] and income_amt[4]; and
- tax-rates[4] if the gross pay is above income_amt[4].
Your current logic does not do this.
Also, how do you want to handle cases when the gross pay is equal to the low or high income threshold?
xx