assignment statement: assigns a value to a variable variable must appear on the left side, value on...

12
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side may be an expression that will be evaluated before storing the value in the variable Assignment operator: the equal sign (=) in most languages Variable: Memory location: has an address and a value Value (contents) is used for various operations

Upload: gervais-wright

Post on 17-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Assignment statement: Assigns a value to a variableVariable must appear on the left side, value on the right side of the assignment operatorRight side may be an expression that will be evaluated before storing the value in the variable

Assignment operator: the equal sign (=) in most languagesVariable:

Memory location: has an address and a valueValue (contents) is used for various operations

Page 2: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Arithmetic Operations

• + Addition 2 + 3 = 5

• - Subtraction 7 – 3 = 4

• * Multiplication 5 * 4 =20

• / Division 12 / 3 = 4

• ^ Exponentiation 2 ^ 3 = 8

• % Modulus 14 % 3 = 2

Page 3: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Assignment, Math, Operators

Add: + variable = variable + 15

Subtract: - variable1 = variable2 – variable3

Multiply: * varible5 = varible8 * variable3

Divide: / variable19 / 5

Parenthesis: ( ) (variable33 * 12)

Bracket: { } or [ ] {variable65 – variable10}

Exponent: ^ (caret) variable12 ^ 3

Page 4: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Hierarchy of Operations

• 1st: perform operations inside parentheses (from inside out if more than one)

• 2nd: perform exponentiation

• 3rd: do multiplications, divisions, and modulus from left to right (if there are more than one)

• 4th: do additions and subtractions from left to right (if there are more than one)

Page 5: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Assignment, Math, Operators

Bracket: { } or [ ]

Parenthesis: ( )

Exponent: ^ (caret)

Multiply: * Divide: /

Add: + Subtract: -

Order of Operations

Equations resolved in order below (from top to bottom)Left to Right

Page 6: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Assignment, Math, Operators

var3 + var5 * 3 / {var12 + var13 / (3 ^ var6 * (var9))} + 2

12

3

4

5

6

7

98

Page 7: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Example of Hierarchy of Operations

• 3 * (6 + 2) / 12 – (7 – 5) ^ 2 * 3 = ?

• ( ) first: = 3 * 8 / 12 – 2 ^ 2 * 3

• ^ next: = 3 * 8 / 12 – 4 * 3

• Leftmost * next: = 24 / 12 – 4 * 3

• Division next: = 2 – 4 * 3

• Multiply next: = 2 – 12

• Subtract last: = -10

Page 8: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Relational Operators• Relational operators are the symbols used

in the condition to be evaluated in If statements:

• = equal to• <> not equal to• < less than• > greater than• <= less than or equal to• >= greater than or equal to

Page 9: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

IF condition1 = condition2 THEN true pathELSE false pathENDIF

IF condition1 > condition2 THEN true pathENDIF

IF condition1 >= condition2 THEN true pathELSE false pathENDIF

Page 10: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Comparison vs. Assignment• The equals sign (=) in this text may have two different meanings.

The difference is very significant. • As an assignment operator, the equals sign sets the value of an

expression on the right side to the variable on the left side.• As a comparison operator, the equals sign asks the question, “Is

the value of the variable on the left side the same as the value of the expression, number, or variable on the right side?”

• Many programming languages distinguish between these two operators as follows:

• a single equals sign (=) signifies the assignment operator• a double equals sign (==) signifies the comparison operator• This is demonstrated in the examples that follow in the next slides.

Page 11: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Logical Operators

• Logical operators are used to connect simple conditions into a more complex condition called a compound condition.

• AND OR NOT

Page 12: Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side

Hierarchy of OperationsType Operator Order Performed

Arithmetic operations

are performed first, in order shown

( )

^

* / %

+ -

1st parentheses

2nd exponentiation

3rd: multiplication, division, modulus

4th: addition, subtraction

Relational operations

are performed second

= <> < <= > >=

All relational operators have equal precedence

Logical operations

are performed last, in the order shown

NOT

AND

OR

1st: NOT

2nd: AND

3rd: OR