Tutorial Addendum On C# - Allotment A - Analytic Expressions and Codicillary Statements

 31 December 18:00   

    



    



    

Let s see how we can use "if" statements to break the afterward tax adding problem.

    

The federal government anesthetized the afterward tax law for year 2002:

    



        

  • For anniversary of your taxable assets dollar amid $1 and $30000, you pay $0.20 tax

        



  •     

  • For anniversary of your taxable assets dollar amid $30001 and $50000, you pay $0.25 tax.

        



  •     

  • For anniversary of your taxable assets dollar amid $50001 and $70000, you pay $0.30 tax.

        



  •     

  • For anniversary of your taxable assets dollar aloft $50000, you pay $0.35 tax.

        



  •     



    



    

Now assuming, you are authoritative $85000 in 2002, how abundant federal tax will you pay? See the

    

output of the afterward program:

    

 

    

class Taxes {

    

accessible changeless abandoned Main() {

    

bifold assets = 85000.0;

    

bifold tax = 0.0;

    

if (income>30000.0) {

    

tax = tax + 30000.0*0.20;

    

} abroad if (income>0.0) {

    

tax = tax + income*0.20;

    

}

    

if (income>50000.0) {

    

tax = tax + (50000.0-30000.0)*0.25;

    

} abroad if (income>30000.0) {

    

tax = tax + (income-30000.0)*0.25;

    

}

    

if (income>70000.0) {

    

tax = tax + (70000.0-50000.0)*0.30;

    

} abroad 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

    



    



    



    

"switch" Statements

    



    

A "switch" account switches the breeze of beheading to one of the pre-defined sections of

    

statement depending on the consistent amount of the controling expression. Actuality is the

    

syntax of a "switch" statement:

    



    

switch statement:

    

about-face (expression) {

    

case literal_1:

    

embeded_statements

    

break;

    

case literal_2:

    

embeded_statements

    

break;

    

......

    

default:

    

embeded_statements

    

break;

    

}

    



    



    

When a "switch" account is encountered in the beheading flow, the announcement will

    

be evaluated first. If consistent amount equals to one of the literals listed in

    

the "case" clauses, the beheading breeze will be switched to the first embeded account of

    

that "case" clause. The "break" account will breach the beheading breeze from continuing

    

to the next statement, and alteration it to the end of the "switch" statement.

    



    

If the consistent amount of the announcement does no according to any of the literals listed

    

in the "case" clauses, the beheading breeze will be switched to the first embeded account of

    

the "default" clause.

    



    

The afterward program shows how a "switch" account is acclimated to book a specific

    

message depending on which day of anniversary it is today:

    

 

    

class Switches {

    

accessible changeless abandoned Main() {

    

int today;

    

cord message;

    

today = (int) System.DateTime.Today.DayOfWeek;

    

about-face (today) {

    

case 1:

    

bulletin = "Today is Monday. We are accessible until 5 p.m.";

    

break;

    

case 2:

    

bulletin = "Today is Tuesday. We are accessible until 5 p.m.";

    

break;

    

case 3:

    

bulletin = "Today is Wendesday. We are accessible until 5 p.m.";

    

break;

    

case 4:

    

bulletin = "Today is Thursday. We are accessible until 7 p.m.";

    

break;

    

case 5:

    

bulletin = "Today is Friday. We are accessible until 7 p.m.";

    

break;

    

default:

    

bulletin = "Sorry, we are bankrupt on weekends.";

    

break;

    

}

    

System.Console.WriteLine(message);

    

}

    

}

    



    



    

Output:

    

 

    

Sorry, we are bankrupt on weekends.

    



    



    

Note that we are using the DateTime anatomy provided by the C# compiler to

    

get the day of anniversary of today, casted to the "int" type. By blockage the advertence manual,

    

we can see that the day of anniversary has a amount of 0 to 6, apery Sunday to Saturday,

    

respectively. Of course, the aloft achievement was generated on a Sunday.

    



    

 


Tags: system, income, dollar, statements, value, console, message, statement, public, taxable

 income, statement, switch, message, statements, embeded, execution, value, expression, following, taxable, dollar, system, default, console, resulting, writeline, , tax tax, switch statement, message today, open until, taxable income, execution flow, income dollar, resulting value, embeded statements, statements break, system console, income 30000, dollar between, income 50000, tax income, income 70000, console writeline, taxable income dollar, embeded statements break, income dollar between, system console writeline, execution flow will, static void main, public static void, switch statement switch,

Share Tutorial Addendum On C# - Allotment A - Analytic Expressions and Codicillary Statements:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  c-sharp