assignment statement: assigns a value to a variable variable must appear on the left side, value on...
TRANSCRIPT
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
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
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
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)
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
Assignment, Math, Operators
var3 + var5 * 3 / {var12 + var13 / (3 ^ var6 * (var9))} + 2
12
3
4
5
6
7
98
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
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
IF condition1 = condition2 THEN true pathELSE false pathENDIF
IF condition1 > condition2 THEN true pathENDIF
IF condition1 >= condition2 THEN true pathELSE false pathENDIF
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.
Logical Operators
• Logical operators are used to connect simple conditions into a more complex condition called a compound condition.
• AND OR NOT
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