Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expression ISYS 350.

Similar presentations


Presentation on theme: "Expression ISYS 350."— Presentation transcript:

1 Expression ISYS 350

2 Performing Calculations
Basic calculations such as arithmetic calculation can be performed by math operators Operator 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);

3 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

4 Formula to Expression

5 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 = 7 int result8 = ++x; // result8 = 15, x = 15 ++z; // z= 11 double a = 8.5; double b = 3.4; double result15 = --a; // result15 = 7.5 double result16 = ++b; // result16 = 4.4

6 Compound (Shortcut) Operators
+= 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.

7

8 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) //myInt += 1; //myInt = ++myInt; ++myInt; textBox1.Text = myInt.ToString(); }

9 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) myInt += stepValue; // myInt = myInt + stepValue;

10 Prefix/Postfix Increment/Decrement Operators
int a = 5; int b = 5; int y = ++a; // a = 6, y = 6 int 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.

11 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?

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

13 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.


Download ppt "Expression ISYS 350."

Similar presentations


Ads by Google