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)

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

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.

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.

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