Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1.

Similar presentations


Presentation on theme: "Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1."— Presentation transcript:

1 Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

2 Operators  Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result  Operand can be variables, literals, or methods.  e.g. x = 10 // assignment operation; // operand: x and 10; // operator: = y = x + 1 // ? 2

3 Operators  Depending on the number of its operands, an operator can fall into one of these:  Unary operator  Binary operator  Ternary operator  Unary operator needs 1 operand, binary operator needs 2 operands, and ternary operator needs 3 operands. 3

4 Operators  Types of operator  Assignment (=)  Arithmetic (+, -, *, /, %)  Unary (+, -, ++, --)  Equality and Relational (==, !=, >, =, <=)  Conditional AND – OR (&&, ||)  Boolean Logical (&, |, ^, !)  Ternary Conditional (?:)  String concatenation (+)  Bitwise and Bit shift (&, |, ~, >>, >>) 4

5 Assignment Operators  One of the most common operators  It assigns the value on its right to the operand on its left, which must be a variable  The type of the expression must be assignment compatible with the type of the variable. An explicit cast (conversion) may be needed. 5

6 Assignment Operators  Simple assignment operator (=) int x,y,z; float length; x = 10; z = x; z = 2 * y; … next = input.nextFloat();  Type casting (conversion) int x = 7/2; /* x equals 3 (implicit cast) */ float y = 3;/* y equals 3.0f (implicit cast) */ double z = 3.5; /* same as double z = 3.5d; */ float fl = (float)z; /* explicit cast */ 6

7 Assignment Operators  Compound assignment operators (+=, -=, *=, etc.) 7 Using Simple Assignment and Arithmetic Operators Using only Compound Assignment Operator a = a + b;a += b; a = a – b;a -= b; a = a * b;a *= b; a = a / b;a /= b; a = a % b;a %= b; etc.… etc

8 Assignment Operators a *= b + 1 is analogous to a = a * (b + 1) or a = a * b + 1 ? See precedence table… 8

9 Arithmetic Operators 9

10 Operator Precedence & Associativity 10

11 Arithmetic Operators () symbols can be used to increase priority level, e.g. x=2+3*5 1. 3*5 2. 2+3*5 3. X= 2+3*5 x=(2+3)*5 1. 2+3 2. (2+3)*5 3. x=(2+3)*5 11

12 Arithmetic Operators  Example: 12

13 Arithmetic Operators 13  Example:  Order of evaluation: x = (x * y + y * z)/(x * y – y * z);  Example:  Order of evaluation: x = (x * y + y * z)/(x * y – y * z); 12 3 4 5 6 7 8

14 Unary Operators 14

15 Unary Operators  The increment/decrement operators can be applied before (prefix) or after (postfix) the operand.  e.g. result++; // result +=1; ++result; // result += 1 Both will end in result being incremented by one.  The only difference is that the prefix version ( ++result ) evaluates to the incremented value, whereas the postfix version ( result++ ) evaluates to the original value.  If you are just performing a simple increment/decrement, it doesn't really matter which version you choose.  But if you use this operator in part of a larger expression, the one that you choose may make a significant difference. 15

16 Unary Operators 1. int i = 3; i++; 2. System.out.println(i); 3. ++i; 4. System.out.println(i); 5. System.out.println(++i); 6. System.out.println(i++); 7. System.out.println(i); 16

17 Unary Operators 1. int i = 3; i++; 2. System.out.println(i); // "4" 3. ++i; 4. System.out.println(i); // "5" 5. System.out.println(++i); // "6" 6. System.out.println(i++); // "6" 7. System.out.println(i); // "7" 17

18 Relational and Equality Operators  Can be applied to the primitive numeric types  Only the equality operators == and != are allowed to operate on boolean values  All yield boolean values. 18

19 Relational and Equality Operators int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); 19

20 Conditional AND-OR Operators  The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions.  These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed. 20

21 Conditional AND-OR Operators 21 AB!A!BA && BA || B True False True False TrueFalseTrue FalseTrue False True False True False

22 Conditional AND-OR Operators int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); 22

23 Conditional AND-OR Operators int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); else if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); 23

24 Conditional AND-OR Operators if (w && x) { // outer "if" if (y || z) { // inner "if" //... inner "if" body } 24

25 Boolean Logical Operators  A "logical AND" is true if and only if both its operands are true  A "logical OR" is true if and only if either of its operands are true.  An "exclusive OR" operator yields true if either, but not both, of its operands is true 25

26 Boolean Logical Operators 26

27 Ternary Conditional Operator  Provides a single expression that yields one of two values based on a boolean expression. e.g. value = (userSetIt ? usersValue : defaultValue); is equivalent to if (userSetIt) value = usersValue; else value = defaultValue; 27

28 Ternary Conditional Operator int value1 = 1; int value2 = 2; int result; boolean someCondition = true; result = someCondition ? value1 : value2; System.out.println(result); 28

29 String Concatenation  + can be used to concatenate two strings. e.g. String salam = "Welcome "; String personName = "Mr. President"; salam = salam + personName; salam += "!"; System.out.println(salam); Output: Welcome Mr. President! 29

30 Expressions  An expression is a construct made up of variables, operators, and method invocations,.. ..which are constructed according to the syntax of the language,.. ..that evaluates to a single value.  An expression may be any of these:  a single variable name,  a complex sequence of method invocations,  variable accesses,  object creations,  and the combination of the results of those subexpressions using other operators, further method invocations, and variable accesses. 30

31 Expressions Examples of expressions (bold red):  int x = 0;  y = 100;  System.out.println("y: " + y);  int result = 1 + 2; // result is now 3  if(value1 == value2) System.out.println("value1 == value2" ); 31

32 Statements  Statements are roughly equivalent to sentences in natural languages.  A statement forms a complete unit of execution.  Kinds of statements:  Expression statements  Declaration statements  Control flow statements 32

33 Statements  An expression statement is made by terminating an expression with a semicolon (;).  Examples of expression statements:  aValue = 8933.234; //assignment statement  aValue++; // increment statement  System.out.println("Hello World!"); // method invocation statement  Bicycle myBike = new Bicycle(); // object creation // statement 33

34 Statements  A declaration statement declares a variable.  Example:  double aValue = 8933.234; // declaration statement 34

35 Statements  Control flow statements regulate the order in which statements get executed.  Kinds of control flow statements:  Decision making:  if-then statement  if-then-else statement,  switch-case statement  Repetition/looping:  while statement  for statement  do-while statement  Branching statement:  break statement  continue statement  return statement 35

36 Input import java.util.Scanner; //.. Scanner input = new Scanner(System.in); System.out.print(“Please enter a length (cm) : "); float length = input.nextFloat(); 36

37 Exercise Masukkan tahun kelahiran anda: … Anda lahir pada masa… … Dan usia anda sekarang… tahun 37

38 Exercise  Masa Penjajahan (sebelum 1945)  Masa Kemerdekaan (1945) – peralihan dari penjajah ke Presiden Soekarno  Masa Revolusi (1946-1966) – Presiden Soekarno  Orde Baru (1966-1998) – Presiden Soeharto  Orde Reformasi (1998-sekarang):  Presiden B.J. Habibie (1998-1999)  Presiden Abdurrachman Wahid (1999-2001)  Presiden Megawati (2001-2004)  Presiden Susilo Bambang Yudhoyono (2004-sekarang) 38


Download ppt "Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1."

Similar presentations


Ads by Google