C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
"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:
Now assuming, you are making $85000 in 2002, how much federal tax will you pay? See the output of the following program:
// Taxes.cs // Copyright (c) 2006 HerongYang.com. All Rights Reserved. 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
Table of Contents
►Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation