330 likes | 507 Views
Operators, Expressions and Assignment. Calculating Things. Expressions. Expressions are used to calculate values: 8 * 9 22 - 13 These values are often loaded into variables: X = 8 * 9 Y = 22 - 13 Variables can be used in Expressions: X = 8 * Y Z = X - 13. One Value Per Variable!.
E N D
Operators, Expressions and Assignment Calculating Things...
Expressions • Expressions are used to calculate values: 8 * 9 22 - 13 • These values are often loaded into variables: X = 8 * 9 Y = 22 - 13 • Variables can be used in Expressions: X = 8 * Y Z = X - 13
One Value Per Variable! • Variables hold a single value. • A variable can occur on both sides of an equation: X = X + 1 Y = Y * X / (7 - Y) • This means: • take the value out • Use it to calculate the new value. • Put the new value in the variable.
Expressions and Operators • Expressions are built up using operators and operands. • Depending on the operators used and the purpose of the expression, they fall into three general groups: • Mathematical • Comparison • Logical • These types may be mixed.
Kinds of Expressions • Mathematical Expressions • Used to calculate values. • Comparison Expressions • Compare two values and evaluate a relationship between them. • Logical Expressions • Combines comparison expressions.
Math Operators in Visual Basic ^ Power (Exponentiation) * Multiplication / Division (that rounds) \ Division (that truncates) Mod Modulo (Remainder) + Addition - Subtraction
Why two operators for division? • Most languages have one, and it truncates the value given it. • Truncation means removing any fractional part from a number. 5.5 -> 5 3.423 -> 3 • VB has the truncating division (backslash) to be consistent with other languages. This is known as integer division.
The VB Division • VB has a rounding division (forward slash) to be consistent with your expectations. • In almost every circumstance you’ll want to use the forward slash operator. 7 / 8 -> 1 8 / 5 -> 2
Question: • What is the operator for Exponentiation? • A. * • B. ^ • C. ** • D. ~ • E. None of the above.
What is Modulo? • It gives the remainder of a division operation. • You can calculate it for yourself like this: X mod Y => (X / Y - Floor(X / Y)) * Y • Or, you can remember long division.
Long Division 1 5 9 5 4 This is the Remainder
Order of Precedence of Operators ( ) Parenthesis ^ Exponentiation - Negation * / Multiplication, Division \ Integer Division Mod Modulo + - Addition, Subtraction
Examples of Math Expression • X = 1 + 1 • The value 2 is loaded into variable X • Y = X * 2 • The value in X is multiplied by 2 and the result is loaded into Y, so Y will hold 4. • Z = X * 3 + Y ^ 2 • The value in Y is raised to the power 2. The value in X is multiplied by 3. The results are added and loaded into Z.
Something a little more complicated M=3/Y+2*Z^X\2-4Mod7*-3^(5*.2) HUNH??????????
Step by Step Given: X = 2, Y = 4, and Z = 10 M = 3 / Y + 2 * Z ^ X \ 2 - 4 Mod 7 * -3 ^ (5 * .2) Parenthesis: (5 * .2) => 1.0 M = 3 / Y + 2 * Z ^ X \ 2 - 4 Mod 7 * -3 ^ 1 Exponentiation: Z ^ X => 100, -3 ^ 1 => -3 M = 3 / Y + 2 * 100 \ 2 - 4 Mod 7 * -3
Step by Step by Step M = 3 / Y + 2 * 100 \ 2 - 4 Mod 7 * -3 Now do the division and multiplication, left to right: 3 / Y => .75, 2 * 100 => 200, 7 * -3 => -21 M = .75 + 200 \ 2 - 4 Mod -21
Step by Step by Step... M = .75 + 200 \ 2 - 4 Mod -21 Then the Integer Division: 200 \ 2 => 100 M = .75 + 100 - 4 Mod -21 Then the modulus operator: 4 Mod -21 => 4 M = .75 + 100 - 4
Step by Step by Step... Finally, do the addition and subtraction left to right: .75 + 100 => 100.75, 100.75 - 4 => 96.75 M = 96.75 So the answer is 96.75, which is loaded into variable M.
Question: • What value will be loaded into Z from the following expression. • Z = 6 + 8 / 2 ^ 2 • A. 49 • B. 8 • C. 32 • D. 3.5 • E. None of the above.
Comparison Operators • There are six basic comparison operators < Less Than > Greater Than <= Less Than or Equal To >= Greater Than or Equal To = Equal To < > Not Equal To
Something you don’t have to know! • There are two esoteric comparison operators: Like Compares Strings Is Compares Objects • You’re not responsible for knowing these and we’ll not be covering them.
Use of Comparison Operators Comparison Operators ask questions: Is X bigger than Y?: X > Y Is Y at least as large Z?: Y >= Z Is X the same as Z?: X = Z
Values Returned • Comparison operators all return one of two values: True or False. • Either the relationship holds or it doesn’t • Either a > b, so the result is True • or a isn’t greater than b and the result is False
Representing True & False • True is represented in VB as -1 • Which is 11111111 in binary • False is represented in VB as 0 • Which is 00000000 in binary
Question: • If I want to know if X is greater than 5, I would use: • A. X = 4.9 • B. X>= 5 • C. X < 5 • D. X <> 5 • E. None of the above
Logical Operators • There are three logical operators: And Or Not • Used to combine comparison and logical expressions for more complex situations.
Use of Logical Operators • To enroll at WSU you must have a high school diploma and have lots of money: HighSchool = “Yes” And Money > $1000
More Logical Operators • To graduate from WSU you must have 135 credits, an acceptable GPA and a be enrolled in a college: Credits >= 135 And GPA > 2.0 And Not College = “None” Credits >= 135 And GPA > 2.0 And College <> “None”
Still More Logical Operators • To pay for your Ultra Deluxe Slice-o-Matic in three easy payments you need either $120 in cash or a credit card. CreditCard = “Yes” Or Cash >= 120
Question: • If I wanted to know if A is greater than B and that C is not greater than D I would use: • A. A > B and C > D • B. A > B and not C > D • C. A > B and C <= D • D. Answer B and Answer C • E. None of the above
Values of Logical Expressions • Logical expressions return either True or False, no matter how complex they become. True False
Checking for correctness? • Perform operations by hand • Compare Answers • Repeat
Example Programs • Evaluating an Equation The quadratic formula: