1 / 33

Operators, Expressions and Assignment

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

wilson
Télécharger la présentation

Operators, Expressions and Assignment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operators, Expressions and Assignment Calculating Things...

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

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

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

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

  6. Math Operators in Visual Basic ^ Power (Exponentiation) * Multiplication / Division (that rounds) \ Division (that truncates) Mod Modulo (Remainder) + Addition - Subtraction

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

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

  9. Question: • What is the operator for Exponentiation? • A. * • B. ^ • C. ** • D. ~ • E. None of the above.

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

  11. Long Division 1 5 9 5 4 This is the Remainder

  12. Order of Precedence of Operators ( ) Parenthesis ^ Exponentiation - Negation * / Multiplication, Division \ Integer Division Mod Modulo + - Addition, Subtraction

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

  14. Something a little more complicated M=3/Y+2*Z^X\2-4Mod7*-3^(5*.2) HUNH??????????

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

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

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

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

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

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

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

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

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

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

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

  26. Logical Operators • There are three logical operators: And Or Not • Used to combine comparison and logical expressions for more complex situations.

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

  28. 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”

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

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

  31. Values of Logical Expressions • Logical expressions return either True or False, no matter how complex they become. True False

  32. Checking for correctness? • Perform operations by hand • Compare Answers • Repeat

  33. Example Programs • Evaluating an Equation The quadratic formula:

More Related