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

Post on 28-Dec-2015

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Expression

ISYS 350

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);

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

Formula to Expression

CD

BA

DC

ABCD

BA

ABAAB

22

DC

AB

CD

BA

B

A

X

X

Y

X

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

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.

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

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(); }

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(); }

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.

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?

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:

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.

top related