"if" Statements - Example

This section provides a tutorial example on how to use 'if' statements to calculate income taxes.

Let's see how we can use "if" statements to solve the following tax calculation problem. The federal government passed the following tax law for year 2002:

• For each of your taxable income dollar between \$1 and \$30000, you pay \$0.20 tax
• For each of your taxable income dollar between \$30001 and \$50000, you pay \$0.25 tax.
• For each of your taxable income dollar between \$50001 and \$70000, you pay \$0.30 tax.
• For each of your taxable income dollar above \$50000, you pay \$0.35 tax.

Now assuming, you are making \$85000 in 2002, how much federal tax will you pay? See the output of the following program:

```// Taxes.cs

class Taxes {
public static void Main() {
double income = 85000.0;
double tax = 0.0;
if (income>30000.0) {
tax = tax + 30000.0*0.20;
} else if (income>0.0) {
tax = tax + income*0.20;
}
if (income>50000.0) {
tax = tax + (50000.0-30000.0)*0.25;
} else if (income>30000.0) {
tax = tax + (income-30000.0)*0.25;
}
if (income>70000.0) {
tax = tax + (70000.0-50000.0)*0.30;
} else if (income>50000.0) {
tax = tax + (income-50000.0)*0.30;
}
if (income>70000.0) {
tax = tax + (income-70000.0)*0.35;
}
System.Console.WriteLine("Income: \${0}",income);
System.Console.WriteLine("Tax: \${0}",tax);
}
}
```

Output:

```Income: \$85000
Tax: \$22250
```

Last update: 2015.