arithmatic calculation w8

20
ARITHMETIC CALCULATION ARITHMETIC CALCULATION & & USER INPUT STATEMENT USER INPUT STATEMENT Lecturer: MISS SITI FARHANA

Upload: fikri-rashid

Post on 28-Sep-2015

257 views

Category:

Documents


0 download

DESCRIPTION

calculation

TRANSCRIPT

  • ARITHMETIC CALCULATION&USER INPUT STATEMENTLecturer: MISS SITI FARHANA

  • Learning ObjectivesTo use C# operators to write expressionsTo program with assignment expression Apply and know how to calculate increment and decrement operators in simple programming

  • Operators Arithmetic operators:Addition+ Subtraction-Multiplication*Division/

  • Cont

    OperatorNameExampleEquivalent+=Addition assignmenti += 8i = i + 8-=Subtraction assignmentf -= 8.0f = f 8.0*=Multiplication assignmenti *= 2i = i * 2/=Division assignmenti /= 2i = i / 2%=Remainder assignmenti %= 8i = i % 8

  • Example: using System;namespace arithmetic{ class operator {public static void Main(){ double radius=20; double area; area = radius * radius * 3.142; System.Console.WriteLine(area); } }}

  • Expression & Precedence of OperatorTable below show Operator Precedence Chart

    PRECEDENCE OPERATORHighest Order

    Lowest Ordervar++ and var--.+, - (unary plus and minus), ++var and --var(type)(Casting)!(Not)*, / , % (multiplication, division and modulus)+ , - (addition and subtraction)< , , >= (Comparison)== , !=& (unconditional AND)^ (exclusive OR)| (unconditional OR)&& (conditional AND)|| (conditional OR)= , += , -= , *=, /= , %= (assignment operator)

  • Example: 3 + 4 * 4 > 5 * ( 4 + 3 ) 1

    Expression & Precedence of Operator(1) Inside parentheses first3 + 4 * 4 > 5 * 7 1(2) Multiplication3 + 16 > 5 * 7 1(3) Multiplication3 + 16 > 35 1(4) addition19 > 35 1(5) Subtraction(6) Greater than

  • Exercises:Find the value of a: a = 46 / 9; a = 46 % 9 + 4 * 4 - 2; a = 45 + 43 % 5 * (23 * 3 % 2);

  • Increment and Decrement OperatorsOperator Name Description++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment.

    var++ postincrement The expression (var++) evaluates to the original value in var and increments var by 1.

    --var predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement.

    var-- postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.

  • Increment and Decrement Operators(a ++) and (++a) same as (a+1) Example: differentiate between a++ and ++a.

    a ++++ a 0 0 1 1 2 2 3 3 0 1 1 2 2 3 3 4

  • If n is equal to 2, then 2*(++n) evaluates to 6If n is equal to 2, then 2*(n++) evaluates to 4

    Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i.

    Increment and Decrement Operators

  • int num = 6;int result = ++num; System.Console.WriteLine(result); // result is 7 System.Console.WriteLine(num);// num is 7

    // in this case the mathematical operation takes place before the expression is evaluated.Increment and Decrement Operators

  • int num = 6;int result = num++; System.Console.WriteLine(result); // result is 6 System.Console.WriteLine(num);// num is 7

    // in this case the mathematical operation takes place after the expression is evaluated.Increment and Decrement Operators

  • Exercises:Assume that int a = 1 and double d = 1.0, and that each expression is independent. What are the results of the following expressions? a % = 3 / a + 3; d = 4 + d * d + 4; d + = 1.5 * 3 + (++a); d - = 1.5 * 3 + a++;

  • Assignment Expression After variable declared, you can assign a value to it by using an assignment statement. Assignment statement is used to store the result of the computation or expression into a variableEqual sign (=) is used as the assignment operator. The syntax for assignment statements is as follow:Variable = expressionAn expression represents a computation involving values, variables and operators that evaluates to a value.

  • Assignment ExpressionExample:char ch =a;int a = 2;double total = 20.00;string subject = C# programming;area = radius * radius * 3.142;age = 20;amount = item_price * quantity;

  • Exercises:How to assign x and y if the value of x is 5.23 and value of y is true?Do the exercises from book page 3-24 and 3-25 question number 3.3, 3.4, 3.5 and 3.6

  • USER INPUT STATEMENT(ENTER INPUT IN STRING DATA TYPE)class Program { static void Main(string[] args) { string name; System.Console.Write("Enter Your Name : "); name = Console.ReadLine(); } }

  • USER INPUT STATEMENT(ENTER INPUT IN INTEGER DATA TYPE)class Program { static void Main(string[] args) { int num; System.Console.Write("Enter Your Number : "); num = int.Parse(Console.ReadLine()); } }

  • USER INPUT STATEMENT(ENTER INPUT IN DOUBLE DATA TYPE)class Program { static void Main(string[] args) { double num; System.Console.Write("Enter Your Number : "); num = double.Parse(Console.ReadLine()); } }