Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Arithmetic Operations +Addition2 + 3=5 -Subtraction7 – 3=4 *Multiplication5 * 4=20 /Division12 / 3=4 ^Exponentiation2 ^ 3=8 %Modulus14 % 3= 2

3 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

4 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)

5 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

6 Assignment, Math, Operators var3 + var5 * 3 / {var12 + var13 / (3 ^ var6 * (var9))} + 2 12 3 4 5 6 7 9 8

7 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

8 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

9 IF condition1 = condition2 THEN true path ELSE false path ENDIF IF condition1 > condition2 THEN true path ENDIF IF condition1 >= condition2 THEN true path ELSE false path ENDIF

10 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.

11 Logical Operators Logical operators are used to connect simple conditions into a more complex condition called a compound condition. AND OR NOT

12 Hierarchy of Operations TypeOperatorOrder Performed Arithmetic operations are performed first, in order shown ( ) ^ * / % + - 1 st parentheses 2 nd exponentiation 3 rd : multiplication, division, modulus 4 th : 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 1 st : NOT 2 nd : AND 3 rd : OR


Download ppt "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."

Similar presentations


Ads by Google