Logical and Relational Operation Session 5 Course: T-0974 Algorithm & Object-Oriented Programming I Year: 2010
Bina Nusantara Learning Outcomes After taking this course, student should be expected to apply and demonstrate using Relational and comparator operator, Logical operator and bitwise operator.
Bina Nusantara Lecture outline Relational & Comparator procedure Relational & Comparator operator Logic & Boolean procedure Logic & Boolean operator Truth table Bitwise Operation
Bina Nusantara Relational procedure Relational procedure == Comparator procedure. Comparing two values. Using 6 relational/comparator operator. The result has Boolean type. The values have number, ASCII, or String data type.
Bina Nusantara Comparator operator OperatorDescriptionExampleResult <less than1 < 2true <=less than or equal to1 <= 2true >greater than1 > 2false >=greater than or equal to1 >= 2false ==equal to1 == 2false !=not equal to1 != 2true
Bina Nusantara Comparator operator Character can be compared by refering to its ASCII number. –Example: ‘a’ is more bigger than ‘A’ because ASCII number for ‘a’ (97) is bigger than ASCII number for ‘A’ (65). Word can be compared by String helper. Comparator operator is different than asssignment operator. X = 14 store 14 to X. X == 14 compare if X is equal to 14.
Bina Nusantara Comparator Operation
Bina Nusantara Logic/Boolean Procedure Logical procedure == Boolean procedure. Execute 1 or 2 logic values. It has 4 types Logic/Boolean operator. The result has Boolean data type. Truth table
Bina Nusantara Boolean Operator OperatorNameDescription !notlogical negation &&andlogical conjunction ||orlogical disjunction ^exclusive orlogical exclusion
Bina Nusantara NOT (!) p!pExample truefalse!(1>2) is true, because (1>2) is false falsetrue!(1>0) is false, because (1>0) is true Operator not (!) inverts the original value. true false and false true
Bina Nusantara AND (&&) p1p2p1 && p2Example false (2>3) && (5>5) is false Because both (2>3) and (5>5) are false falsetruefalse(2>3) && (6>5) is false Because (2>3) is false truefalse (6>5) && (2>3) is false Because (2>3) is false true (3>2) && (5>=5) is true Because both (3>2) and (5>=5) are true AND Operator (&&) is true when all of its operands are true. If one of its operand is false, then AND is false.
Bina Nusantara OR (||) p1p2p1 || p2Example false (2>3) || (5>5) is false Because both (2>3) and (5>5) are false falsetrue (2>3) || (6>5) is true Because (6>5) is true truefalsetrue(6>5) || (2>3) is true Because (6>5) is true true (3>2) || (5>=5) is true Because both (3>2) and (5>=5) are true OR (||) Operator is true if one of every its operand is true. If all of its operands become false then OR is false.
Bina Nusantara XOR (^) p1p2p1 ^ p2Example false (2>3) ^ (5>5) is false Because both (2>3) and (5>5) are false falsetrue (2>3) ^ (6>5) is true Because (2>3) is false and (6>5) is true truefalsetrue(6>5) ^ (2>3) is true Because (6>5) is true and (2>3) is false true false(3>2) ^ (5>=5) is true Because both (3>2) and (5>=5) are true Operator XOR (^) is true when both if its operands has different condition. When its operands has same condition then XOR is false.
Bina Nusantara Tabel Kebenaran: Demo
Bina Nusantara Thruth Table: Demo
Bina Nusantara Thruth table: Leap Year
Bina Nusantara Did You Know? When evaluating p1&&p2, Java evaluates p1 first. –If p1 is true then Java evaluates p2. –If p2 is false then Java doesn’t evaluate p2. When evaluating p1||p2, Java evaluates p1 first –If p1 is true then Java doesn’t evaluate p2. –If p1 is false then Java evaluates p2.
Bina Nusantara Advanced Learning Bit : the smallest unit in data. 1 byte = 8 bits. 1 bit has 0 or 1. Java support bitwise operator to do bit shift. Example : 255
Bina Nusantara Advanced Learning Bit’s values is same as boolean –1 : true –0 : false Its procedure is same as logical procedure. & && (AND) | || (OR) ^ ^ (XOR) ~ ! (NOT/NEGATE) Additional operator << : shift left (unsigned) >> : shift right (signed) >>> : shift right (unsigned)
Bina Nusantara Advanced Learning OperatorNamaExampleDeskripsi &AND & Result: If both of its bits is 1 then it produces 1. |OR | Result: If one of its bits is 1 then it produces 1. ^XOR ^ Hasil: If both of its bits have different values then it produces 1. ~NEGATE~ Hasil: If bit = 1 0 If bit = 0 1 <<LEFT SHIFT << 2 Hasil: Shift 2 bit to the left then fill the latter with zeros. >>RIGHT SHIFT (SIGNED) >> 2 Hasil: >> 2 Hasil: Shift 2 bit to the right then fill the latter with zeros. Its Least Significant Bit is sign bit. >>>RIGHT SHIFT (UNSIGNED) >>> 2 Hasil: Shift 2 bit to the right then fill the latter with zeros.
Bina Nusantara Advanced Learning Shorthand for bitwise operation : &=, |=, ^=, >=, >>>= Bitwise only can be used for real number like : byte, short, int and long. Bitwise for Char type converts to int which refer to ASCII number.
Bina Nusantara Advanced Learning
Bina Nusantara Advanced Learning
Latihan Buatlah program menerima input Nama, Total belanja Jika belanja >100rb, maka disc 10% selain itu tidak ada discount. (gunakan kelas Scanner) Bina Nusantara University 25
import java.util.*; class Belanja { public static void main(String args[]) { String nama; boolean member=true; double belanja=0.0; Scanner in =new Scanner(System.in) System.out.println("Masukkan Nama"); nama=in.nextLine(); System.out.println("Masukkan Belanja"); belanja=in.nextInt(); Bina Nusantara University 26 if ((belanja >100000|| member==true) && belanja <500000) { belanja =belanja -(belanja*0.1); System.out.println ("Total belanja"+ belanja); } elseif (belanja >=500000) { belanja =belanja -(belanja*0.2); System.out.println ("Total belanja"+ belanja); } else { System.out.println ("Total belanja"+ belanja); }
Bina Nusantara References Introduction to Java Programming. 6ed. Liang p94-98, p1279 Dasar Pemrograman Java 2. Abdul Kadir p76-83 The Complete Reference Java. 5ed. Herbert Schildt p62-76 Java 2 Weekend Crash Course. Julio Sanchez p85-96 Bitwise: – – – –