pemrograman dasar operators, expressions & statements ptiik - ub 1

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

Upload: hector-walsh

Post on 18-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

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

Pemrograman Dasar

Operators, Expressions &Statements

PTIIK - UB

1

Page 2: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 3: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 4: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Operators

Types of operator Assignment (=)

Arithmetic (+, -, *, /, %)

Unary (+, -, ++, --)

Equality and Relational (==, !=, >, <, >=, <=)

Conditional AND – OR (&&, ||)

Boolean Logical (&, |, ^, !)

Ternary Conditional (?:)

String concatenation (+)

Bitwise and Bit shift (&, |, ~, >>, <<, >>>)

4

Page 5: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Page 6: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 7: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 8: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Assignment Operators

a *= b + 1 is analogous toa = a * (b + 1)

ora = a * b + 1

?See precedence table…

8

Page 9: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Arithmetic Operators

9

Symbol Operator Operation Example+ additive operator (also used for

String concatenation)addition x = y + 6;

- subtraction operator subtraction y = x – 5;* multiplication operator multiplication y = y * 3;/ division operator division z = x/y;% remainder operator remainder A = 10 % 3;

Page 10: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Operator Precedence & Associativity

10

Priority Level

Operators Precedence Associativity

1 postfix expr++ expr-- left-associative (from left to right)2 unary ++expr --expr +expr -expr

~ !

left-associative (from left to right)

3 multiplicative * / % left-associative (from left to right)4 additive + - left-associative (from left to right)5 shift << >> >>> left-associative (from left to right)6 relational < > <= >= instanceof left-associative (from left to right)7 equality == != left-associative (from left to right)8 bitwise AND & left-associative (from left to right)9 bitwise exclusive OR ^ left-associative (from left to right)10 bitwise inclusive OR | left-associative (from left to right)11 logical AND && left-associative (from left to right)12 logical OR || left-associative (from left to right)13 ternary ? : right-associative (from right to left)14 assignment = += -= *= /= %= &= ^= |=

<<= >>= >>>=

right-associative (from right to left)

Page 11: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Arithmetic Operators

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

1. 3*52. 2+3*53. X= 2+3*5

x=(2+3)*51. 2+32. (2+3)*53. x=(2+3)*5

11

Page 12: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Arithmetic Operators

Example:

12

yzxy

yzxyx

Page 13: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

1 2

3

4 5

6

7

8

yzxy

yzxyx

Page 14: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Unary Operators

14

Symbols Operators Unary plus operator; indicates positive value (numbers are positive without this, however)

- Unary minus operator; negates an expression

++ Increment operator; increments a value by 1

-- Decrement operator; decrements a value by 1

! Logical complement operator; inverts the value of a boolean

+

Page 15: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 16: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 17: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 18: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Symbols Meaning

> greater than

>= greater than or equal to

< less than

<= less than or equal to

== equal to

!= not equal to

Page 19: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 20: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Symbols Meaning

&& Conditional AND

|| Conditional OR

Page 21: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Conditional AND-OR Operators

21

A B !A !B A && B A || B

True True False False True True

True False False True False True

False True True False False True

False False True True False False

Page 22: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 23: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 24: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Conditional AND-OR Operators

if (w && x) { // outer "if"

if (y || z) { // inner "if"

// ... inner "if" body

}

}

24

Page 25: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Symbols Meaning

& Logical AND

| Logical inclusive OR

^ Logical exclusive OR (XOR)! Logical negation

Page 26: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Boolean Logical Operators

26

A B !A A & B A I B A ^ BTRUE TRUE FALSE TRUE TRUE FALSETRUE FALSE FALSE FALSE TRUE TRUEFALSE TRUE TRUE FALSE TRUE TRUEFALSE FALSE TRUE FALSE FALSE FALSE

Page 27: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 28: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Ternary Conditional Operator

int value1 = 1;

int value2 = 2;

int result;

boolean someCondition = true;

result = someCondition ? value1 : value2; System.out.println(result);

28

Page 29: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 30: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 31: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 32: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 33: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 34: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Statements

A declaration statement declares a variable. Example:

double aValue = 8933.234;

// declaration statement

34

Page 35: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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

Page 36: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Input

import java.util.Scanner;

//..

Scanner input = new Scanner(System.in);

System.out.print(“Please enter a length (cm) : ");

float length = input.nextFloat();

36

Page 37: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

Exercise

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

37

Page 38: Pemrograman Dasar Operators, Expressions & Statements PTIIK - UB 1

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