Operators, Expressions and Assignment

33
Operators, Expressions and Assignment Calculating Things...

description

Operators, Expressions and Assignment. Calculating Things. Expressions. Expressions are used to calculate values: 8 * 922 - 13 These values are often loaded into variables: X = 8 * 9Y = 22 - 13 Variables can be used in Expressions: X = 8 * YZ = X - 13. One Value Per Variable!. - PowerPoint PPT Presentation

Transcript of Operators, Expressions and Assignment

Page 1: Operators, Expressions and Assignment

Operators, Expressions and Assignment

Calculating Things...

Page 2: Operators, Expressions and Assignment

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

Page 3: Operators, Expressions and Assignment

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.

Page 4: Operators, Expressions and Assignment

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.

Page 5: Operators, Expressions and Assignment

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.

Page 6: Operators, Expressions and Assignment

Math Operators in Visual Basic

^ Power (Exponentiation)* Multiplication

/ Division (that rounds)

\ Division (that truncates)

Mod Modulo (Remainder)

+ Addition

- Subtraction

Page 7: Operators, Expressions and Assignment

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

VB has the truncating division (backslash) to be consistent with other languages. This is known as integer division.

Page 8: Operators, Expressions and Assignment

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 -> 18 / 5 -> 2

Page 9: Operators, Expressions and Assignment

Question:

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

Page 10: Operators, Expressions and Assignment

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)) * YOr, you can remember long division.

Page 11: Operators, Expressions and Assignment

Long Division

15 9 5 4

This is the Remainder

Page 12: Operators, Expressions and Assignment

Order of Precedence of Operators

( ) Parenthesis^ Exponentiation- Negation* / Multiplication, Division\ Integer DivisionMod Modulo+ - Addition, Subtraction

Page 13: Operators, Expressions and Assignment

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.

Page 14: Operators, Expressions and Assignment

Something a little more complicated

M=3/Y+2*Z^X\2-4Mod7*-3^(5*.2)

HUNH??????????

Page 15: Operators, Expressions and Assignment

Step by Step

Given: X = 2, Y = 4, and Z = 10M = 3 / Y + 2 * Z ^ X \ 2 - 4 Mod 7 * -3 ^ (5 * .2)

Parenthesis: (5 * .2) => 1.0M = 3 / Y + 2 * Z ^ X \ 2 - 4 Mod 7 * -3 ^ 1

Exponentiation: Z ^ X => 100, -3 ^ 1 => -3M = 3 / Y + 2 * 100 \ 2 - 4 Mod 7 * -3

Page 16: Operators, Expressions and Assignment

Step by Step by Step

M = 3 / Y + 2 * 100 \ 2 - 4 Mod 7 * -3Now do the division and multiplication, left to

right: 3 / Y => .75, 2 * 100 => 200, 7 * -3 => -21

M = .75 + 200 \ 2 - 4 Mod -21

Page 17: Operators, Expressions and Assignment

Step by Step by Step...

M = .75 + 200 \ 2 - 4 Mod -21 Then the Integer Division: 200 \ 2 => 100M = .75 + 100 - 4 Mod -21Then the modulus operator: 4 Mod -21 => 4M = .75 + 100 - 4

Page 18: Operators, Expressions and Assignment

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.75So the answer is 96.75, which is loaded

into variable M.

Page 19: Operators, Expressions and Assignment

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.

Page 20: Operators, Expressions and Assignment

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

Page 21: Operators, Expressions and Assignment

Something you don’t have to know!

There are two esoteric comparison operators:Like Compares StringsIs Compares Objects You’re not responsible for knowing these

and we’ll not be covering them.

Page 22: Operators, Expressions and Assignment

Use of Comparison Operators

Comparison Operators ask questions:Is X bigger than Y?: X > YIs Y at least as large Z?: Y >= ZIs X the same as Z?: X = Z

Page 23: Operators, Expressions and Assignment

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

Page 24: Operators, Expressions and Assignment

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

Page 25: Operators, Expressions and Assignment

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

Page 26: Operators, Expressions and Assignment

Logical Operators

There are three logical operators:And Or Not

Used to combine comparison and logical expressions for more complex situations.

Page 27: Operators, Expressions and Assignment

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

Page 28: Operators, Expressions and Assignment

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”

Page 29: Operators, Expressions and Assignment

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

Page 30: Operators, Expressions and Assignment

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

Page 31: Operators, Expressions and Assignment

Values of Logical Expressions

Logical expressions return either True or False, no matter how complex they become.

TrueFalse

Page 32: Operators, Expressions and Assignment

Checking for correctness?

Perform operations by handCompare AnswersRepeat

Page 33: Operators, Expressions and Assignment

Example Programs

Evaluating an EquationThe quadratic formula:

y b b aca 2 42