Download presentation
Presentation is loading. Please wait.
1
Operators, Expressions and Assignment Calculating Things...
2
Expressions l Expressions are used to calculate values: 8 * 922 - 13 l These values are often loaded into variables: X = 8 * 9Y = 22 - 13 l Variables can be used in Expressions: X = 8 * YZ = X - 13
3
One Value Per Variable! l Variables hold a single value. l A variable can occur on both sides of an equation: X = X + 1 Y = Y * X / (7 - Y) l This means: l take the value out l Use it to calculate the new value. l Put the new value in the variable.
4
Expressions and Operators l Expressions are built up using operators and operands. l Depending on the operators used and the purpose of the expression, they fall into three general groups: Mathematical Comparison Logical l These types may be mixed.
5
Kinds of Expressions l Mathematical Expressions Used to calculate values. l Comparison Expressions Compare two values and evaluate a relationship between them. l 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? l 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 l VB has a rounding division (forward slash) to be consistent with your expectations. l In almost every circumstance you’ll want to use the forward slash operator. 7 / 8->1 8 / 5->2
9
Question: l 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 59 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 l X = 1 + 1 The value 2 is loaded into variable X l Y = X * 2 The value in X is multiplied by 2 and the result is loaded into Y, so Y will hold 4. l 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: l What value will be loaded into Z from the following expression. l Z = 6 + 8 / 2 ^ 2 A.49 B.8 C.32 D.3.5 E.None of the above.
20
Comparison Operators l 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! l There are two esoteric comparison operators: LikeCompares Strings IsCompares 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 l Comparison operators all return one of two values: True or False. l 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 l True is represented in VB as -1 l Which is 11111111 in binary l False is represented in VB as 0 l Which is 00000000 in binary
25
Question: l 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 l There are three logical operators: AndOrNot l Used to combine comparison and logical expressions for more complex situations.
27
Use of Logical Operators l 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 l 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 l 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: l 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 l Logical expressions return either True or False, no matter how complex they become. True False
32
Checking for correctness? l Perform operations by hand l Compare Answers l Repeat
33
Example Programs l Evaluating an Equation The quadratic formula:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.