Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?"— Presentation transcript:

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

2 2 Buat program java untuk menghitung luas segi tiga. Tes program dengan input: Alas = 20, tinggi = 10 Alas = 10, tinggi = -30 Catch-up Exercise

3 3 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.

4 4 Perintah 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 yes no no

5 5 Perintah if-else In Java, we can write a selection statement using if-else Syntax: if else Jika ekspresi boolean bernilai true, maka statement_1 yang akan dieksekusi. Jika ekspresi boolean bernilai false, maka statement_2 yang akan dieksekusi.

6 6 Example 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

7 7 count = 4; total = 5; if (count < 3) total = 0; else total = total + count; System.out.println(total); Example 2

8 8 Compound Statements To include multiple statements in a branch, enclose the statements in braces. if (count < 3) { total = 0; count = 0; }

9 9 Menghilangkan Bagian else Jika bagian else dihapus and the expression after the if is false, no action occurs. syntax if (Boolean_Expression) Statement example weight = 50; ideal = 60; if (weight > ideal) caloriesPerDay -= 500; //kalori = kalori-500;

10 10 Ekspresi 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)

11 11 Operators Perbandingan

12 12 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.

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

14 14 Truth Table/ Tabel Kebenaran Refer to the following truth table for logical operators, assuming P and Q are boolean expressions: PQP && QP || Q!P!Q true false truefalse truefalse truefalsetrue false true

15 15 Presedensi 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&& ||

16 16 Ekspresi Boolean Majemuk Boolean expressions can be combined using the “or” ( || ) operator. example if ((quantity > 5) || (cost < 10))... syntax ( Sub_Expression_1) || (Sub_Expression_2)

17 17 Compound Statements/ Perintah Majemuk When a list of statements is enclosed in braces ( {} ), they form a single compound statement. syntax { Statement_1; Statement_2; … }

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

19 19 The if Statement We can also use if without else: Syntax: –if 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"); }

20 20 Indentation 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!");

21 21 Nested [bersarang] if 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)

22 22 Example 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"); else System.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

23 23 Exercise 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.

24 24 Exercise 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".

25 25 Nested 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.

26 26 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.

27 27 Nested Statements, cont. syntax if (Boolean_Expression_1) if (Boolean_Expression_2) Statement_1) else Statement_2) else if (Boolean_Expression_3) Statement_3) else Statement_4);

28 28 Multibranch if-else Statements syntax if (Boolean_Expression_1) Statement_1 else if (Boolean_Expression_2) Statement_2 else if (Boolean_Expression_3) Statement_3 else if … else Default_Statement

29 29 Multibranch if-else Statements, cont. equivalent code

30 30 The switch 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.

31 31 The switch 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.

32 32 Switch 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; }

33 33 Syntax – switch switch (variable / expression) { case label1: statement; break; case label2: case label3: 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.

34 34 Exercise 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 star 2.Humpty Dumpty 3.London Bridge Your choice? 2 Humpty Dumpty sat on the wall Humpty Dumpty had a great fall

35 35 Java 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'

36 36 Exercise 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!


Download ppt "PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?"

Similar presentations


Ads by Google