Title: Assignment statement:
1Assignment 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
2Arithmetic 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
3Assignment, 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
4Hierarchy 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)
5Assignment, Math, Operators
Order of Operations Equations resolved in order
below (from top to bottom)Left to Right
Bracket or Parenthesis (
) Exponent (caret) Multiply Divide
/ Add Subtract -
6Assignment, Math, Operators
var3 var5 3 / var12 var13 / (3 var6
(var9)) 2
1
2
3
4
5
6
7
9
8
7Example 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
8Relational Operators
- Relational operators are the symbols used in the
condition to be evaluated in If statements - equal to
- ltgt not equal to
- lt less than
- gt greater than
- lt less than or equal to
- gt greater than or equal to
9IF condition1 condition2 THEN true
path ELSE false path ENDIF IF condition1 gt
condition2 THEN true path ENDIF IF
condition1 gt condition2 THEN true
path ELSE false path ENDIF
10Comparison 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.
11Logical Operators
- Logical operators are used to connect simple
conditions into a more complex condition called a
compound condition. - AND OR NOT
12Hierarchy of Operations
Type 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 ltgt lt lt gt gt All relational operators have equal precedence
Logical operations are performed last, in the order shown NOT AND OR 1st NOT 2nd AND 3rd OR