percabangan bagaimana jika ya? bagaimana jika tidak? bagaimana jika benar? bagaimana jika salah?

36
PERCABANGAN PERCABANGAN Bagaimana Jika YA? Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana Jika BENAR? Bagaimana jika SALAH? Bagaimana jika SALAH?

Upload: dominik-brasington

Post on 31-Mar-2015

246 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

PERCABANGANPERCABANGANBagaimana Jika YA? Bagaimana Jika YA?

Bagaimana Jika TIDAK?Bagaimana Jika TIDAK?

Bagaimana Jika BENAR? Bagaimana Jika BENAR? Bagaimana jika SALAH?Bagaimana jika SALAH?

Page 2: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

22

• Buat program java untuk menghitung luas segi tiga.

• Tes program dengan input:• Alas = 20, tinggi = 10• Alas = 10, tinggi = -30

Catch-up ExerciseCatch-up Exercise

Page 3: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

33

Flow of ControlFlow of Control

• Flow of Control

Cara kerja komputer memproses perintah

dalam sebuah program:– Sequentially; baris per baris,– Conditionally; dengan perintah percabangan– Repetitively; menggunakan perintah perulangan

• Program yang selama ini dibuat adalah sekuensial.

• Dalam slide ini akan dibahas jenis Kondisional.

Page 4: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

44

Perintah SeleksiPerintah Seleksi

• Selection or branching statement menentukan the action that should be taken by the computer.

• Titik dimana a decision has to be made as to jalur yang akan diambil.

IF <Saya punya uang 1 M>yes no

<buy a house><buy a car><give to charity>

<ambil kredit><baik ke semua orang><perbanyak doa>

no

Page 5: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

55

PerintahPerintah if-else if-else

• In Java, we can write a selection statement using if-else

• Syntax:if <ekspresi boolean >

<statement_1>else

<statement_2>

• Jika ekspresi boolean bernilai true, maka statement_1 yang akan dieksekusi.

• Jika ekspresi boolean bernilai false, maka statement_2 yang akan dieksekusi.

Page 6: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

66

Example 1Example 1

• Perhatikan fragmen program berikut ini:• int age;• Scanner keyboard = new Scanner (System.in);• age = keyboard.nextInt();

if (age < 21) System.out.println("too young to vote");else

System.out.println("register as voter");• What is the output if the user enters

– 16– 23– 21

Page 7: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

77

count = 4;

total = 5;

if (count < 3)

total = 0;

else

total = total + count;

System.out.println(total);

Example 2Example 2

Page 8: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

88

Compound StatementsCompound Statements

• To include multiple statements in a branch, enclose the statements in braces.

if (count < 3)

{

total = 0;

count = 0;

}

Page 9: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

99

Menghilangkan Bagian Menghilangkan Bagian elseelse

• Jika bagian else dihapus and the expression after the if is false, no action occurs.

• syntaxif (Boolean_Expression)

Statement

• exampleweight = 50;

ideal = 60;

if (weight > ideal)

caloriesPerDay -= 500; //kalori = kalori-500;

Page 10: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1010

Ekspresi BooleanEkspresi Boolean• When processing a selection statement

such as an if statement, the computer must evaluate a boolean expression.

• Recall the boolean primitive data type that holds the value true or false.

• A boolean expression is sebuah ekspresi yang menghasilkan nilai true or false.

• Boolean expressions biasanya memakai operator perbandingan (also known as relational operators)

Page 11: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1111

Operators PerbandinganOperators Perbandingan

Page 12: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1212

Exercise [Latihan]Exercise [Latihan]

1. Write a Java program that asks the user for a score and displays "You pass" if the score is greater than or equal to 50 and "You fail" otherwise.

2. Write a Java program that asks the user for a grade and displays "Wow!" if the grade is 'A' and "Try harder next time" otherwise.

Page 13: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1313

Operator Logika Operator Logika

• Formula ekspresi boolean seringkali menjadi kompleks.• Modifikasilah program sebelumnya, so that it will display

"You pass" if the assignment score is greater than or equal to 50 DAN the examination score is greater than or equal to 60.

• We will need to use operator logika for more complex boolean expressions:– && (and) – || (or)– ! (not)

Page 14: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1414

Truth Table/ Tabel KebenaranTruth Table/ Tabel Kebenaran

• Refer to the following truth table for logical operators, assuming P and Q are boolean expressions:P Q P && Q P || Q !P !Q

true true true true false false

true false false true false false

false true false true true true

false false false false true true

Page 15: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1515

Presedensi OperatorPresedensi Operator

• We have looked at operator Arithmetic, operator Relational and Operator Logical.

• Jika operators tersebut dipakai bersama dalam sebuah expression, they must be evaluated menurut the precedence rules:– Parentheses (or brackets)– Type cast (type)– Logical not !– Arithmetic * / % * / %– Arithmetic + – + –– Relational operators > >= < <= == !=– Logical AND and OR && ||

Page 16: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1616

Ekspresi Boolean MajemukEkspresi Boolean Majemuk

• Boolean expressions can be combined using the “or” (||) operator.

• exampleif ((quantity > 5) || (cost < 10))

...

• syntax(Sub_Expression_1) || (Sub_Expression_2)

Page 17: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1717

Compound Statements/Compound Statements/Perintah MajemukPerintah Majemuk

• When a list of statements is enclosed in braces ({}), they form a single compound statement.

• syntax{

Statement_1;

Statement_2;

}

Page 18: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1818

ExerciseExercise

• Evaluate the following expressions– (10 < 5) || (3 > 7)– !true && false– !(5==8)– true || !false– !(3 > 5 && 4 + 1 < 3 * 2) || 3 != 3

Page 19: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

1919

The The ifif Statement Statement

• We can also use if without else:

• Syntax:– if <boolean expression>

<execute these statements>

• If more than one statement is to be executed, they must be enclosed in curly brackets:

if ((rich =='y') || (famous == 'y')){ System.out.println("I will be your friend"); System.out.println("You are either rich or famous");}

Page 20: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2020

IndentationIndentation

• Important to improve the clarity of your program.

• Show which statements are part of the if.• However, remember that indents are

ignored by the compiler.• What is the output of the following

program fragment for num = 8? num = 15?if (num < 10)

num = num + 10;System.out.println("num is " + num);

System.out.println("Finished!");

Page 21: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2121

Nested [bersarang] Nested [bersarang] ifif

• We can nest an if (or if-else) statement inside another if (or if-else) statement.

• Consider the following:– Jika seorang mahasiswa adalah angkatan tahun 1,

maka nilai lulusnya adalah 50, jika mahasiswa angkatan tahun 2, maka nilai lulusnya adalah 60 dan untuk angkatan tahun 3, maka nilai lulusnya adalah 40.

• Write the code fragment to represent this.• Remember, every else must have an if (but not

necessary vice-versa)

Page 22: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2222

ExampleExample

if (year == 1) if (mark >= 50)

System.out.println("You pass");else

System.out.println("You fail");else if (year == 2)

if (mark >= 60)System.out.println("You pass");

elseSystem.out.println("You fail");

else if (year == 3)if (mark >= 40)

System.out.println("You pass");else

System.out.println("You fail");else

System.out.println("Cannot be determined");

if-else pairs

nested if-else

Page 23: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2323

ExerciseExercise

• Write a Java program that will ask the user for a number and display the square root of the number if it is positive.– Use the Math.sqrt() method.

• Next modify the program so that if the user enters a negative number, an error message will be displayed.

Page 24: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2424

ExerciseExercise

• Write a Java program that asks the user for his or her name and gender then displays"Hello Mr. xxx" or "Hello Ms. xxx" as appropriate. If an invalid value is entered for the gender, just return the name "xxx".

Page 25: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2525

Nested StatementsNested Statements

• An if-else statement can contain any sort of statement within it.

• In particular, it can contain another if-else statement.– An if-else may be nested within the “if”

part.– An if-else may be nested within the “else”

part.– An if-else may be nested within both parts.

Page 26: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2626

Nested Statements, cont.Nested Statements, cont.

• Each else is paired with the nearest unmatched if.

• If used properly, indentation communicates which if goes with which else.

• Braces can be used like parentheses to group statements.

Page 27: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2727

Nested Statements, cont.Nested Statements, cont.

• syntaxif (Boolean_Expression_1) if (Boolean_Expression_2)

Statement_1) else

Statement_2)else if (Boolean_Expression_3)

Statement_3) else

Statement_4);

Page 28: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2828

Multibranch Multibranch if-elseif-else Statements Statements

• syntaxif (Boolean_Expression_1)

Statement_1

else if (Boolean_Expression_2)

Statement_2

else if (Boolean_Expression_3)

Statement_3

else if …

else

Default_Statement

Page 29: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2929

Multibranch Multibranch if-elseif-else Statements, cont. Statements, cont.

• equivalent code

Page 30: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3030

The The switchswitch Statement Statement

• The switch statement is a mutltiway branch that makes a decision based on an integral (integer or character) expression.

• The switch statement begins with the keyword switch followed by an integral expression in parentheses and called the controlling expression.

Page 31: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3131

The The switchswitch Statement, cont. Statement, cont.

• A list of cases follows, enclosed in braces.• Each case consists of the keyword case

followed by– a constant called the case label

– a colon– a list of statements.

• The list is searched for a case label matching the controlling expression.

Page 32: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3232

SwitchSwitch Statement Statement• The switch statement is a branching statement that allows

multiple options.• This is similar to using nested if-else statements but it is

simpler and often clearer.switch (gender){

case 'm': case 'M':System.out.println("Hello, Mr. " + name);break;

case 'f': case 'F':System.out.println("Hello, Ms. " + name); break;

default:System.out.println("Hello, " + name);break;

}

Page 33: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3333

Syntax – Syntax – switchswitch switch (variable / expression){

case label1:statement;break;

case label2:case label3:

statement;statement;break;

case label4: case label5:statement;break;

default:statement;break;

}

Must be of type byte, short, int or char ONLY

Case label: •must be constant value•compatible type to the variable/ expression•cannot be duplicated

optional section to be executed if none of the case labels match the variable / expression

If the value of the variable /expression matches the case label, the statements following the label will be executed until a break is reached.

Page 34: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3434

ExerciseExercise

• Write a Java program that displays the following menu and allows the user to choose one of the options given. Then display the required nursery rhyme.

Choose the rhyme:1. winkle twinkle little star2. Humpty Dumpty3. London BridgeYour choice? 2Humpty Dumpty sat on the wallHumpty Dumpty had a great fall

Page 35: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3535

Java Character SetJava Character Set• Java uses unicode to represents characters. Unicode uses

2 bytes (16 bits) and are thus capable of representing a total of 65536 different symbols, but only half of these are used.

• The first 256 unicode characters are the same as the ASCII characters.

• All characters (whether unicode or ASCII) are represented using numerical code.– The character '0' is represented by the numerical code of 48, '9'

57, 'A' 65, 'a' 97, etc. • The expressions below are true:

– '3' < '9'– '3' > 9– 'B' > '4'– 'a' > 'A'

Page 36: PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

3636

ExerciseExercise

• Write a program that asks the user to enter two grades, student1Grade and student2Grade.

• The grade must be a character from 'A' to 'F'. If the grade for either student is lower than 'C' then display 'Help required'.

• Then display whether student1 or student2 got the better grade.

• Use methods in your program• Plan carefully!