Presentation is loading. Please wait.

Presentation is loading. Please wait.

L5:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.

Similar presentations


Presentation on theme: "L5:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef."— Presentation transcript:

1 L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef

2 L5:CSC © Dr. Basheer M. Nasef 2 Control Statements II

3 L5:CSC © Dr. Basheer M. Nasef 3 F To implement selection control using switch statements. F To write expressions using the conditional operator. F To display formatted output using the System.out.printf method and to format strings using the String.format method. F (GUI) To get user confirmation using confirmation dialogs.

4 L5:CSC © Dr. Basheer M. Nasef 4 switch Statement Flow Chart

5 L5:CSC © Dr. Basheer M. Nasef 5 switch Statements

6 L5:CSC © Dr. Basheer M. Nasef 6 switch Statements

7 L5:CSC © Dr. Basheer M. Nasef 7 switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses.

8 L5:CSC © Dr. Basheer M. Nasef 8 switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; }  The value1,..., and valueN must have the same data type as the value of the switch-expression.  The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch- expression.

9 L5:CSC © Dr. Basheer M. Nasef 9 switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } Note that value1,..., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x.

10 L5:CSC © Dr. Basheer M. Nasef 10 switch Statement Rules The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. The case statements are executed in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.

11 L5:CSC © Dr. Basheer M. Nasef 11 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Suppose ch is 'a':

12 L5:CSC © Dr. Basheer M. Nasef 12 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } ch is 'a':

13 L5:CSC © Dr. Basheer M. Nasef 13 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Execute this line

14 L5:CSC © Dr. Basheer M. Nasef 14 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Execute this line

15 L5:CSC © Dr. Basheer M. Nasef 15 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Execute this line

16 L5:CSC © Dr. Basheer M. Nasef 16 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Next statement; Execute next statement

17 L5:CSC © Dr. Basheer M. Nasef 17 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Suppose ch is 'a':

18 L5:CSC © Dr. Basheer M. Nasef 18 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } ch is 'a':

19 L5:CSC © Dr. Basheer M. Nasef 19 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Execute this line

20 L5:CSC © Dr. Basheer M. Nasef 20 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Execute this line

21 L5:CSC © Dr. Basheer M. Nasef 21 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Next statement; Execute next statement

22 L5:CSC © Dr. Basheer M. Nasef 22 Using Strings in switch Statements you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named month:StringSwitchDemo SwitchCaseGrade

23 L5:CSC © Dr. Basheer M. Nasef 23 Conditional Operator if (x > 0) y = 1 Else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2

24 L5:CSC © Dr. Basheer M. Nasef 24 Conditional Operator Suppose you want to assign the larger number between variable num1 and num2 to max. You can simply write a statement using the conditional expression: max = (num1 > num2) ? num1 : num2; if (num1>num2) System.out.println(num1 + “is max”); else System.out.println(num2 + “is max”); System.out.println( (num1>num2)? num1 + “is max” : num2 + “is max”);

25 L5:CSC © Dr. Basheer M. Nasef 25 Conditional Operator if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); System.out.println( (num % 2 == 0)? num + “is even” : num + “is odd”);

26 L5:CSC © Dr. Basheer M. Nasef 26 Formatting Output Use the new JDK 1.5 printf statement. System.out.printf(format, items); Where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, character, boolean value, or a string. Each specifier begins with a percent sign.

27 L5:CSC © Dr. Basheer M. Nasef 27 Frequently-Used Specifiers Specifier OutputExample %b a boolean value true or false %c a character 'a' %d a decimal integer 200 %f a floating-point number %e a number in standard scientific notation e+01 %s a string "Java is cool"

28 L5:CSC © Dr. Basheer M. Nasef 28 Operator Precedence var++, var-- +, - (Unary plus and minus), ++var, --var (type) Casting ! (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction), >= (Comparison) ==, !=; (Equality) ^ (Exclusive OR) && (Conditional AND) Short-circuit AND || (Conditional OR) Short-circuit OR =, +=, -=, *=, /=, %= (Assignment operator)

29 L5:CSC © Dr. Basheer M. Nasef 29 Operator Precedence and Associativity The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case the expression in the inner parentheses is executed first.) When evaluating an expression without parentheses, the operators are applied according to the precedence rule and the associativity rule. If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are left-associative.

30 L5:CSC © Dr. Basheer M. Nasef 30 Operator Associativity When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are left-associative. a – b + c – d is equivalent to ((a – b) + c) – d Assignment operators are right-associative. Therefore, the expression a = b += c = 5 is equivalent to a = (b += (c = 5))

31 L5:CSC © Dr. Basheer M. Nasef 31 Example Applying the operator precedence and associativity rule, the expression * 4 > 5 * (4 + 3) - 1 is evaluated as follows:

32 L5:CSC © Dr. Basheer M. Nasef 32 Operand Evaluation Order Supplement III.A, “Advanced discussions on how an expression is evaluated in the JVM.”

33 L5:CSC © Dr. Basheer M. Nasef 33 (GUI) Confirmation Dialogs int option = JOptionPane.showConfirmDialog (null, "Continue");

34 L5:CSC © Dr. Basheer M. Nasef 34 Problem: Guessing Birth Day GuessBirthDateUsingConfirmationDialog The program can guess your birth day. Run to see how it works.

35 L5:CSC © Dr. Basheer M. Nasef 35 Problem What is y after the following switch statement is executed? x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; }

36 L5:CSC © Dr. Basheer M. Nasef 36 Problem Use a switch statement to rewrite the following if statement and draw the flow chart for the switch statement: if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34;

37 L5:CSC © Dr. Basheer M. Nasef 37 Problem: (Sorting three integers) Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables n1, n2, and n3, respectively. The program sorts the numbers so that n1 ≤ n2 ≤ n3.


Download ppt "L5:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef."

Similar presentations


Ads by Google