Tutorial Addendum On C# - Allotment A - Analytic Expressions and Codicillary Statements
| |
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 Interface' onMouseOver="tip('infobox2')" onMouseOut="untip()" target='_parent'> 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.
|
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, |
Also see ...
Variables, Declarations, and Appointment StatementsLiterals advice us to access data into C programs area it is bare for processing.If the aforementioned data is bare in two places,
Now let s analysis what accept abstruse in this area with a sample program: class Variables { accessible changeless abandoned Main() { bool is_ok; int n; int num_of_sec;
We can aswell put variables instead of literals into addition expression, as continued as the variables are of basic or absolute data type. For example:3.14159*diameter, sqrt_s*sqrt_s. Agenda that amplitude charaters afore or a
"const" and "readonly"C has two key words to anticipate ethics of variables to be afflicted unwantedly: const and readonly. Actuality is a allegory of the two:
Output: Print(int i): 1.Print(long i): 12345678901234.Print(object o): 2.Print(int i, int j): 3, 4.Print(params int[] i): 5, 6.Two absorbing notes:br
In adjustment to acknowledgment some of the questions aloft in the antecedent sections, let s yield a abutting attending at how absolute numbers are represented in a computer system.In C, the IEEE 754 single precision and doub
Rule 7: If the backer basic abundance all 1s, the atom componentis aloof for addition operation uses.For abrogating values, aggregate is identical with the absolute numbers,except the assuranc
accessible abandoned outputDebugText() { int i; Console.WriteLine("======"); Console.WriteLine("Converting a absolute amount to IEEE Stardard 754"); Console.WriteLine("Binary Amphibia