Upcoming SlideShare
×

# 03 1 Operators And Expressions

1,750

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,750
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
99
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 03 1 Operators And Expressions

1. 1. Operators and Expressions Lecture 3 by Jumail Bin Taliba Faculty of Computer Science & Information System
2. 2. Elements of a program <ul><li>Literals  fixed data written into a program </li></ul><ul><li>Variables & constants  placeholders (in memory) for pieces of data </li></ul><ul><li>Types  sets of possible values for data </li></ul><ul><li>Expressions  combinations of operands (such as variables or even &quot;smaller&quot; expressions) and operators. They compute new values from old ones. </li></ul><ul><li>Assignments  used to store values into variables </li></ul><ul><li>Statements  &quot;instructions&quot;. In C, any expression followed by a semicolon is a statement </li></ul>
3. 3. Elements of a program <ul><li>Control-flow constructs  constructs that allow statements or groups of statements to be executed only when certain conditions hold or to be executed more than once. </li></ul><ul><li>Functions  named blocks of statements that perform a well-defined operation. </li></ul><ul><li>Libraries  collections of functions. </li></ul>
4. 4. Statement <ul><li>Statements are elements in a program which (usually) ended up with semi-colon (;) </li></ul><ul><ul><li>e.g. below is a variables declaration statement </li></ul></ul><ul><ul><ul><li>int a, b, c ; </li></ul></ul></ul><ul><li>Preprocessor directives (i.e. #include and define ) are not statements. They don’t use semi-colon </li></ul>
5. 5. Some types of statement in C++
6. 6. An expression statement is a statement that results a value Some examples of expression Value <ul><li>Literal expression </li></ul><ul><li>e.g. 2, “A+”, ‘B’ </li></ul>The literal itself <ul><li>Variable expression </li></ul><ul><li>e.g. Variable1 </li></ul><ul><li>arithmetic expression </li></ul><ul><li>e.g. 2 + 3 -1 </li></ul>The content of the variable The result of the operation
7. 7. Operators <ul><li>Operators can be classified according to </li></ul><ul><ul><li>the type of their operands and of their output </li></ul></ul><ul><ul><ul><li>Arithmetic </li></ul></ul></ul><ul><ul><ul><li>Relational </li></ul></ul></ul><ul><ul><ul><li>Logical </li></ul></ul></ul><ul><ul><ul><li>Bitwise </li></ul></ul></ul><ul><ul><li>the number of their operands </li></ul></ul><ul><ul><ul><li>Unary (one operand) </li></ul></ul></ul><ul><ul><ul><li>Binary (two operands) </li></ul></ul></ul>
8. 8. Binary expression
9. 9. Unary Expression
10. 10. Ternary Expression (a>2) ? 1: 0 Operator First operand is a condition Second operand is a value Third operand is another value
11. 11. Arithmetic operators <ul><li>They operate on numbers and the result is a number. </li></ul><ul><li>The type of the result depends on the types of the operands. </li></ul><ul><li>If the types of the operands differ (e.g. an integer added to a floating point number), one is &quot;promoted&quot; to other. </li></ul><ul><ul><li>The &quot;smaller&quot; type is promoted to the &quot;larger&quot; one. </li></ul></ul><ul><ul><ul><li>char  int  float  double </li></ul></ul></ul>
12. 12. <ul><li>Example of promotion: </li></ul><ul><ul><li>The result of the following “double division” is 2.5 </li></ul></ul><ul><ul><li>5 / 2.0 </li></ul></ul><ul><ul><li>Before the division process, 5 is promoted from integer 5 to float 5.0 </li></ul></ul><ul><li>The result of the following “integer division” is 2 </li></ul><ul><ul><li>5 / 2 </li></ul></ul><ul><ul><li>There is no promotion occurred. Both operands are the same type. </li></ul></ul>
13. 13. Arithmetic operators: +, * <ul><li>+ is the addition operator </li></ul><ul><li>* is the multiplication operator </li></ul><ul><li>They are both binary </li></ul>
14. 14. Arithmetic operator:  <ul><li>This operator has two meanings: </li></ul><ul><ul><li>subtraction operator (binary) </li></ul></ul><ul><ul><li>negation operator (unary) </li></ul></ul>e.g. 31 - 2 e.g. -10
15. 15. Arithmetic operator: / <ul><li>Division operator </li></ul><ul><li>CAREFUL! The result of integer division is an integer: </li></ul>e.g. 5 / 2 is 2, not 2.5
16. 16. Arithmetic operator: % <ul><li>The modulus (remainder) operator. </li></ul><ul><li>It computes the remainder after the first operand is divided by the second </li></ul><ul><li>It is useful for making cycles of numbers: </li></ul><ul><ul><li>For an int variable x : if x is: 0 1 2 3 4 5 6 7 8 9 ... (x%4) is: 0 1 2 3 0 1 2 3 0 1 ... </li></ul></ul>e.g. 5 % 2 is 1, 6 % 2 is 0
17. 19. Relational operators <ul><li>These perform comparisons and the result is what is called a boolean: a value TRUE or FALSE </li></ul><ul><li>FALSE is represented by 0; anything else is TRUE </li></ul><ul><li>The relational operators are: </li></ul><ul><ul><li>< (less than) </li></ul></ul><ul><ul><li><= (less than or equal to) </li></ul></ul><ul><ul><li>> (greater than) </li></ul></ul><ul><ul><li>>= (greater than or equal to) </li></ul></ul><ul><ul><li>== (equal to) </li></ul></ul><ul><ul><li>!= (not equal to) </li></ul></ul>
18. 21. Logical operators (also called Boolean operators) <ul><li>These have Boolean operands and the result is also a Boolean. </li></ul><ul><li>The basic Boolean operators are: </li></ul><ul><ul><li>&& (logical AND) </li></ul></ul><ul><ul><li>|| (logical OR) </li></ul></ul><ul><ul><li>! (logical NOT) -- unary </li></ul></ul>
19. 23. Assignment operator: = <ul><li>Binary operator used to assign a value to a variable. </li></ul>
20. 24. <ul><li>Remember, an expression results a value . So, what is the result of an assignment expression? </li></ul><ul><ul><li>The value of an assignment expression is its right operand </li></ul></ul><ul><ul><li>e.g. </li></ul></ul><ul><ul><li>int a=10; </li></ul></ul><ul><ul><li>cout << a=7; </li></ul></ul><ul><ul><li>Output: 7 (not 10). </li></ul></ul><ul><ul><li>Because the value of expression a=7 is 7 (i.e its right operand) </li></ul></ul><ul><li>Assignment is a special expression. Beside results a value, it also does other thing which is putting the value into its left operand. This is called a side effect . </li></ul><ul><li>In the previous example, a side effect has occurred to variable a after evaluating the assignment expression (a=7). Now, the variable has a new value which is 7. </li></ul>
21. 25. Special assignment operators <ul><li>write a += b; instead of a = a + b; </li></ul><ul><li>write a -= b; instead of a = a - b; </li></ul><ul><li>write a *= b; instead of a = a * b; </li></ul><ul><li>write a /= b; instead of a = a / b; </li></ul><ul><li>write a %= b; instead of a = a % b; </li></ul>
22. 26. Special assignment operators <ul><li>Increment, decrement operators: ++, -- </li></ul><ul><ul><li>Instead of a = a + 1 you can write a++ or ++a </li></ul></ul><ul><ul><li>Instead of a = a - 1 you can write a-- or --a </li></ul></ul><ul><li>These operators cause side effect </li></ul><ul><li>What is the difference? </li></ul>num = 10; ans = num++; num = 10; ans = ++num; First increment num, then assign num to ans. In the end, num is 11 ans is 11 First assign num to ans, then increment num. In the end, num is 11 ans is 10 post-increment pre-increment
23. 27. Result of postfix Increment
24. 28. Result of Prefix Increment
25. 29. Precedence & associativity <ul><li>How would you evaluate the expression 17 - 8 * 2 ? Is it 17 - (8 * 2) or ( 17 - 8) * 2 ? </li></ul><ul><li>These two forms give different results. </li></ul><ul><li>We need rules! </li></ul>
26. 30. Precedence & associativity <ul><li>When two operators compete for the same operand (e.g. in 17 - 8 * 2 the operators - and * compete for 8) the rules of precedence specify which operator wins. </li></ul><ul><ul><li>The operator with the higher precedence wins </li></ul></ul><ul><li>If both competing operators have the same precedence, then the rules of associativity determine the winner. </li></ul>
27. 31. Precedence & associativity ! Unary – * / % + – < <= >= > = = != && || = higher precedence lower precedence Associativity: execute left-to-right ( except for = and unary – )
28. 32. <ul><li>Example: Left associativity </li></ul><ul><ul><li>3 * 8 / 4 % 4 * 5 </li></ul></ul>
29. 33. <ul><li>Example: Right associativity </li></ul><ul><ul><li>a += b *= c-=5 </li></ul></ul>
30. 34. Precedence & associativity <ul><li>Examples: X =17 - 2 * 8 Ans: X=17-(2*8) , X=1 </li></ul><ul><ul><li>Y = 17 - 2 - 8 Ans: Y = (17-2)-8, Y=7 </li></ul></ul><ul><ul><li>Z = 10 + 9 * ((8 + 7) % 6) + 5 * 4 % 3 *2 + 1 ? </li></ul></ul>Not sure? Confused? then use parentheses in your code!
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.