expression isys 350. performing calculations basic calculations such as arithmetic calculation can...

13
Expression ISYS 350

Upload: cynthia-shaw

Post on 28-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Expression

ISYS 350

Page 2: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Performing Calculations• Basic calculations such as arithmetic calculation can be

performed by math operatorsOperator Name of the operator Description

+ Addition Adds two numbers

- Subtraction Subtracts one number from another

* Multiplication Multiplies one number by another

/ Division Divides one number by another and gives the quotient

% Modulus Divides one number by another and gives the remainder

Other calculations: Use Math class’s methods.Example: Math.Pow(x, y)

double x = 3.0, y = 2.0, z;z=Math.Pow(x,y);

Page 3: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Order of Evaluation

• Operator Multiples• 1 ( ) Inner to outer, left to right• 2. Power left to right• 3. *, / left to right• 4. +, - left to right

Page 4: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Formula to Expression

CD

BA

DC

ABCD

BA

ABAAB

22

DC

AB

CD

BA

B

A

X

X

Y

X

Page 5: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Increment/Decrement Operators

++ Increment Adds 1 to the operand (x = x + 1).

-- Decrement Subtracts 1 from the operand (x = x - 1).

int x = 14;int y = 8;Int z = 10;int result7 = --y; // result7 = 7int result8 = ++x; // result8 = 15, x = 15++z; // z= 11

double a = 8.5;double b = 3.4;double result15 = --a; // result15 = 7.5double result16 = ++b; // result16 = 4.4

Page 6: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Compound (Shortcut) Operators

Operator

+= Adding the operand to the starting value of the variable.-= Subtracting the operand from the starting value of the variable.

*= Multiplying the operand by the starting value of the variable./= Dividing the operand by the starting value of the variable.%= Remainder after dividing the right operand by the value in the variable.

Page 7: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Statements that use the same variable on both sides of the equals sign

count = count + 1; // count is increased by 1 count = count – 1; // count is decreased by 1 total = total + 100.0; // total is increased by 100.0 total = total – 100.0; // total is decreased by 100 price = price * .8; // price is multiplied by 8 sum = sum + nextNumber; // sum is increased by value of nextNumber

Statements that use the shortcut operators to get the same results

count += 1; // count is increased by 1 count -= 1; // count is decreased by 1 total += 100.0; // total is increased by 100.0 total -= 100.0; // total is decreased by 100.0 price *= .8; // price is multipled by 8 sum += nextNumber; // sum is increased by the value of nextNumber

Page 8: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Decrement/Increment by 1

private void button1_Click(object sender, EventArgs e) { int myInt; myInt = int.Parse(textBox1.Text); // myInt-=1; // myInt = --myInt; --myInt; textBox1.Text = myInt.ToString(); } private void button2_Click(object sender, EventArgs e) { int myInt; myInt = int.Parse(textBox1.Text); //myInt += 1; //myInt = ++myInt; ++myInt; textBox1.Text = myInt.ToString(); }

Page 9: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Decrement/Increment by any Step Value

private void button1_Click(object sender, EventArgs e) { int stepValue; stepValue = int.Parse(textBox2.Text); int myInt; myInt = int.Parse(textBox1.Text); myInt -= stepValue; // myInt = myInt - stepValue; textBox1.Text = myInt.ToString(); } private void button2_Click(object sender, EventArgs e) { int stepValue; stepValue = int.Parse(textBox2.Text); int myInt; myInt = int.Parse(textBox1.Text); myInt += stepValue; // myInt = myInt + stepValue; textBox1.Text = myInt.ToString(); }

Page 10: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Prefix/Postfix Increment/Decrement Operators

int a = 5;int b = 5;int y = ++a; // a = 6, y = 6int z = b++; // b = 6, z = 5

•When you use an increment or decrement operator as a prefix to a variable, the variable is incremented or decremented and then the result is assigned. •When you use an increment or decrement operator as a postfix to a variable, the result is assigned and then the variable is incremented or decremented.

Page 11: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Counter• Example: Keep track the number of times a

user clicks a button• Need to declare a variable:

int Counter=0;

• Need to increase the counter by 1 when the button is clicked:

Counter = Counter + 1; // Or

Counter+=1; // Or

++Counter;

Question: Where to declare this variable?

Page 12: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Example

int Counter = 0;private void button1_Click(object sender, EventArgs e) { Counter = Counter + 1; textBox1.Text = Counter.ToString(); }

private void button1_Click(object sender, EventArgs e) { int Counter = 0; Counter = Counter + 1; textBox1.Text = Counter.ToString(); }

Incorrect:

Correct:

Page 13: Expression ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators OperatorName of the

Variable Scope• The scope of a variable determines its

visibility to the rest of a program.– Procedural-level scope: declared in a procedure

and visible only in the procedure.– Class-level scope: declared in a class but outside

any procedure; visible to all procedures in the class.