ISYS 350 Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators Operator Name of the operator Description Addition Adds two numbers ID: 473028
Download Presentation The PPT/PDF document "Expression" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Expression
ISYS 350Slide2
Performing Calculations
Basic calculations such as arithmetic calculation can be performed by math operators
Operator
Name of the operator
Description+AdditionAdds two numbers-SubtractionSubtracts one number from another*MultiplicationMultiplies one number by another/DivisionDivides one number by another and gives the quotient%ModulusDivides 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
);Slide3
Order of Evaluation
Operator Multiples
1 ( ) Inner to outer, left to right
2. Power left to right
3. *, / left to right4. +, - left to rightSlide4
Formula to ExpressionSlide5
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= 11double a = 8.5;double b = 3.4;double result15 = --a; // result15 = 7.5double result16 = ++b; // result16 = 4.4Slide6
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.Slide7Slide8
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(); }Slide9
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(); }Slide10
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.Slide11
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?Slide12
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:Slide13
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.