"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) 2015, 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);


Income: $85000
Tax: $22250

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

Logical Expressions and Conditional Statements

 Logical Expressions

 "if" Statements

"if" Statements - Example

 "switch" Statements

 "switch" Statements - Example

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials


 PDF Printing Version