# Java Operators

Published on July 29, 2014

Author: nantha98

Source: authorstream.com

PowerPoint Presentation: 1 Operators by Nantha Kumar Subramaniam PhD CITP Learning Outcomes: Learning Outcomes At the end of the lesson, you should be able to: Use various operators in Java, namely: arithmetic operators assignment arithmetic operators increment & decrement operators relational operators boolean operators Arithmetic Operators: Arithmetic Operators Arithmetic operators in Java: 3 Arithmetic Operators- Example: Arithmetic Operators- Example 4 Arithmetic Assignment Operators: Arithmetic Assignment Operators There are FIVE arithmetic assignment operators in Java: It provides short cut for the arithmetic operation 5 Example of Arithmetic Assignment Operators: Example of Arithmetic Assignment Operators 6 Operator Example Meaning += no += 7 no = no + 7 -= no -= 7 no = no - 7 *= No *= 7 no = no * 7 /= no /= 7 no = no / 7 %= no %= 7 no = no % 7 Increment Operator (++): Increment Operator ( ++ ) Increment operator is used to add the value of a variable by 1 Symbol: ++ Example (1): counter ++ ; Here, the value counter will be added by 1 after being used in the statement Example (2): ++ counter ; Here, the value counter will be added by 1 before being used in the statement 7 Decrement Operator (--): Decrement Operator ( -- ) Decrement operator is used to decrease the value of a variable by 1 Symbol: -- Example (1): counter -- ; Here, the value counter will be reduced by 1 after being used in the statement Example (2): -- counter ; Here, the value counter will be reduced by 1 before being used in the statement 8 More example for -- and ++ operators: More example for -- and ++ operators int i=1; int j=1; int x = 7 - ++ i; int y = 7 – j ++ ; 9 x=5, i =2 (the value of i will be added by 1 before being used in the statement) x=6, j=2 (the value of j only will be added by 1 after being used in the statement) Relational Operators: Relational Operators Java has boolean variables and boolean-valued expressions that can be used to express conditions that can be either true or false using a relational operator Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth and it used in relational expression Relational expression will return “true (1)” or “false (0)” value when used in an expression 10 Example of Relational Expression: Example of Relational Expression “age >= 15 && height > 100” “hours >= 100 ” “marks >= 80 && grade == ‘A’” 11 Always return “TRUE” or “FALSE” value Example of Relational Operators: Example of Relational Operators Relational operators- Examples: Relational operators- Examples 2 == 3 (False) 2 != 0 (True) 3 < 4 (True) 4 > 3 (True) 4 <= 4 (True) 3 >= 4 (False) int age=20; “age>10” ( True) 13 Boolean Operators: Boolean Operators There are three boolean operators in Java: AND ( && ) OR ( || ) NOT ( ! ) As with relational operator, boolean operators is commonly used in relational expression When using boolean operators in relational expression, the understanding of truth table is very important. Truth Table for AND: Truth Table for AND RE1 RE2 Result (RE1 && RE2) False False False False True False True False False True True True RE= Relational Expression Truth Table for OR: Truth Table for OR RE1 RE2 Result (RE1 || RE2) False False False False True True True False True True True True RE= Relational Expression Truth Table for NOT: Truth Table for NOT RE1 Result !(RE1) False True True False RE= Relational Expression Example (1): Example (1) int x=0; int y=1; “(x == 0) && (y==0)” 18 Note: Refer the Truth Table for AND shown earlier TRUE FALSE && FALSE √ Example (2): Example (2) int x=1; int y=0; int z=9; “(x==0) && (y==0) || (z>5)” 19 FALSE TRUE TRUE && || FALSE || TRUE TRUE √ Note: Refer the Truth Tables for OR and AND shown earlier Difference between = and ==: Difference between = and == There is a different between = and == symbols Assignments use the symbol ‘ = ’ to assign a value Example: count = 2; On the other hand, equivalence tests use the symbol ‘ == ’ and always produce a true or false value and commonly used in relational expression For example: 9 == 6 20 value 2 is assigned to variable count produce a false value Evaluating an Arithmetic Expression: Evaluating an Arithmetic Expression When evaluating an arithmetic expression, the precedence rules of the operators must be followed as shown in the table below: Arithmetic operators having higher priority will be performed first in an arithmetic expression If the arithmetic operators having same priorities, then the operation will be performed from left to right in the arithmetic expression 21 Example (1): Example (1) 2 + 4 / 2 22 1 st priority 2 nd priority 2 + 2 4 Example (2): Example (2) ( 2 + 4 ) / 2 23 1 st priority 2 nd priority 6 / 2 3 Example (3): Example (3) 2 + 4 - 2 24 1 st priority 1 st priority 6 - 2 4 Same priority levels, thus arithmetic operation will be performed from left to right in the arithmetic expression PowerPoint Presentation: 25 Thank you

07. 07. 2014
0 views

09. 07. 2014
0 views

09. 07. 2014
0 views

09. 07. 2014
0 views

09. 07. 2014
0 views

07. 07. 2014
0 views

29. 07. 2014
0 views