Tutorial Addendum On C# - Allotment A - Data, Variables and Expressions

android appcolt wargamesios appcolt
 31 December 18:00   




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 afterwards the


operator will be ignored.



When a account with an addition accurate is executed, the announcement will be


evaluated by assuming the addition operation, and the consistent amount will be placed


into the account area the announcement is located. For example, the afterward


statements will abundance the amount 3 to a, and 15 to b.




continued a, b;


a = 1 + 2;


b = 5*a;




Multiple addition operations can aswell be entered into C# statements as an addition


expression with a added circuitous syntax:





accurate arithmetic_operator accurate arithmetic_operator accurate ...




For example: 5*1+2, pi*r*r, 199.99*qty+shipping_fee.



Rule: If assorted operators are acclimated in an expression, data operation haveto be


started with the abettor that has the accomplished akin of precedence. If two operators accept


the aforementioned akin of precedence, starts with the larboard abettor first. If an abettor with a


lower akin of antecedence needs to be candy first, enclose this abettor and the related


data in a brace of parentheses.



Levels of antecedence of addition operators:



  • Multiplicative akin - high: * / %


  • Additive akin - low: + -




Examples of expressions:



  • "1 + 2":


    This announcement is aswell simple. It has alone one operator. The consistent amount is 3.



  • "d*3.14159":


    This is addition one-operator expression. The consistent amount is the amount of


    variable d accumulate by 3.14159.



  • "price + price*tax_rate":


    This is a two-operator expression. The * abettor has a college


    precedence, so the computer will account price*tax_rate first, and takes the aftereffect and


    add it to price.



  • "price*(1+tax_rate)":


    This is aswell a two-operator expression. The parentheses armament the


    computer to account 1+tax_rate first, and takes the aftereffect and accumulate it to price.





So now we understand that an addition announcement can accept one or some addition operations.


It can aswell accept parentheses to change adapt the adjustment appraisal process. At the end of


the appraisal process, an addition accurate will consistently aftermath an accumulation or absolute value.


Base on this observation, we can aswell alarm a individual accurate or capricious of integeral or


real data blazon as an addition expression, because it is aswell bearing an accumulation or


real value. So we can expend our addition announcement analogue with the afterward syntax:










With the compassionate that an addition announcement consistently produces an accumulation or real


value, we can now generize our analogue of appointment account to yield an arithmetic


expression at the appropriate duke ancillary of the appointment assurance "=", with afterward syntax:



assignment statement:


integer_variable = arithmetic_expression


real_variable = arithmetic_expression




Now let s convenance what we accept abstruse so far with a simple program to simulate the banknote


register in a grocery store:




class Accountant {


accessible changeless abandoned Main() {


continued qty_milk, qty_bread, qty_coke;


bifold price_milk, price_bread, price_coke;


bifold total;



qty_milk = 2;


qty_bread = 1;


qty_coke = 6;


price_milk = 1.99;


price_bread = 1.49;


price_coke = 0.99;



absolute = qty_milk * price_milk;


absolute = absolute + qty_bread * price_bread;


absolute = absolute + qty_coke * price_coke;




System.Console.WriteLine(" ");


System.Console.WriteLine("Product Amount Abundance Sub-total");


System.Console.WriteLine("Mike 2% 2L {0} {1} {2}",


price_milk, qty_milk, qty_milk*price_milk);


System.Console.WriteLine("Bread 1lb {0} {1} {2}",


price_bread, qty_bread, qty_bread*price_bread);


System.Console.WriteLine("Coke 2L {0} {1} {2}",


price_coke, qty_coke, qty_coke*price_coke);


System.Console.WriteLine(" ");


System.Console.WriteLine("Total: {0}",










Output of the program will be:






Product Amount Abundance Sub-total


Mike 2% 2L 1.99 2 3.98


Bread 1lb 1.49 1 1.49


Coke 2L 0.99 6 5.94


Total: 11.41




Notic that how the amplitude characters are acclimated in the WriteLine adjustment alarm to


line the columns in the output.



Exercise: Adapt the Accountant chic to add tax into the calculation, bold


the tax amount is 5%. Tax haveto be activated on the absolute price. The tax bulk haveto be


printed out on a separate line.




Tags: system, example, assignment, bread, price, level, value, console, operators, statement

 price, arithmetic, expression, operator, bread, total, writeline, system, console, value, precedence, level, literal, variable, integer, statement, variables, assignment, operators, example, expressions, resulting, following, syntax, parentheses, , arithmetic expression, system console, console writeline, price milk, price bread, total qty, price coke, qty coke, qty milk, qty bread, {1} {2}, {0} {1}, {2} price, resulting value, expression the, milk qty, tax rate, bread qty, operator expression, system console writeline, console writeline system, writeline system console, coke system console, arithmetic operator literal, syntax arithmetic expression, arithmetic expression literal, literal arithmetic operator, real data type,

Share Tutorial Addendum On C# - Allotment A - Data, Variables and Expressions:
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 ...

Article In : Computers & Technology  -  c-sharp