Presentation is loading. Please wait.

Presentation is loading. Please wait.

 1992-2007 Pearson Education, Inc. All rights reserved. 1 5.7 break and continue Statements Break and continue statements – Alter flow of control break.

Similar presentations


Presentation on theme: " 1992-2007 Pearson Education, Inc. All rights reserved. 1 5.7 break and continue Statements Break and continue statements – Alter flow of control break."— Presentation transcript:

1  1992-2007 Pearson Education, Inc. All rights reserved. 1 5.7 break and continue Statements Break and continue statements – Alter flow of control break statement – Causes immediate exit from control structure Used in while, for, do…while or switch statements continue statement – Skips remaining statements in a loop body – Proceeds to next iteration Used in while, for or do…while statements

2  1992-2007 Pearson Education, Inc. All rights reserved. 2 Outline BreakTest.java Line 9 Lines 11-12 Program output Loop 10 times Exit for statement ( break ) when count equals 5

3  1992-2007 Pearson Education, Inc. All rights reserved. 3 Outline ContinueTest.java Line 7 Lines 9-10 Program output Loop 10 timesSkip line 12 and proceed to line 7 when count equals 5

4  1992-2007 Pearson Education, Inc. All rights reserved. 4 Software Engineering Observation 5.3 Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured programming techniques, for example, by adjusting the condition. These programmers do not use break or continue.

5  1992-2007 Pearson Education, Inc. All rights reserved. 5 Software Engineering Observation 5.4 There is a tension between achieving quality software engineering and achieving the best- performing software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.

6  1992-2007 Pearson Education, Inc. All rights reserved. 6 課堂練習 利用 continue 及 break 加快完成以下要求: (1) 請輸入 1 個大於等於 2 的正整數 n , 輸出 n 是否為質數 (2) 請輸入 1 個大於等於 2 的正整數 n , 輸出 2 … n 是否為質數

7  1992-2007 Pearson Education, Inc. All rights reserved. 7 5.8 Logical Operators Logical operators – Allows for forming more complex conditions – Combines simple conditions Java logical operators – && (conditional AND) – || (conditional OR) – & (boolean logical AND) – | (boolean logical inclusive OR) – ^ (boolean logical exclusive OR) – ! (logical NOT)

8  1992-2007 Pearson Education, Inc. All rights reserved. 8 5.8 Logical Operators (Cont.) Conditional AND ( && ) Operator – Consider the following if statement if ( gender == FEMALE && age >= 65 ) ++seniorFemales; – Combined condition is true if and only if both simple conditions are true – Combined condition is false if either or both of the simple conditions are false

9  1992-2007 Pearson Education, Inc. All rights reserved. 9 5.8 Logical Operators (Cont.) Conditional OR ( || ) Operator – Consider the following if statement if ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) System.out.println( “Student grade is A” ); – Combined condition is true if either or both of the simple condition are true – Combined condition is false if both of the simple conditions are false

10  1992-2007 Pearson Education, Inc. All rights reserved. 10 5.8 Logical Operators (Cont.) Short-Circuit ( 捷徑 ) Evaluation of Complex Conditions – Parts of an expression containing && or || operators are evaluated only until it is known whether the condition is true or false ( 需要時才去算右邊布林運算式的真假值 ) – E.g., ( gender == FEMALE ) && ( age >= 65 ) if gender is not equal to FEMALE , s tops immediately after (gender == FEMALE) , (ages >= 65) 就可省略,不用 計算

11  1992-2007 Pearson Education, Inc. All rights reserved. 11 Common Programming Error 5.8 In expressions using operator &&, a condition — we will call this the dependent condition — may require another condition to be true for the evaluation of the dependent condition to be meaningful. In this case, the dependent condition should be placed after the other condition, or an error might occur. For example, in the expression ( i != 0 ) && ( 10 / i == 2 ), the second condition must appear after the first condition, or a divide-by-zero error might occur.

12  1992-2007 Pearson Education, Inc. All rights reserved. 12 5.8 Logical Operators (Cont.) Boolean Logical AND ( & ) Operator – Works identically to && – Except & always evaluate both operands Boolean Logical OR ( | ) Operator – Works identidally to || – Except | always evaluate both operands

13  1992-2007 Pearson Education, Inc. All rights reserved. 13 5.8 Logical Operators (Cont.) Boolean Logical Exclusive OR ( ^ ) – One of its operands is true and the other is false Evaluates to true – Both operands are true or both are false Evaluates to false Logical Negation ( ! ) Operator – Unary operator

14  1992-2007 Pearson Education, Inc. All rights reserved. 14 Outline LogicalOperators. java (1 of 3) Lines 9-13 Lines 16-20 Lines 23-27 Conditional AND truth tableConditional OR truth tableBoolean logical AND truth table

15  1992-2007 Pearson Education, Inc. All rights reserved. 15 Outline LogicalOperators. java (2 of 3) Lines 30-35 Lines 38-43 Lines 46-47 Boolean logical inclusive OR truth table Boolean logical exclusive OR truth table Logical negation truth table

16  1992-2007 Pearson Education, Inc. All rights reserved. 16 Outline LogicalOperators. java (3 of 3) Program output

17  1992-2007 Pearson Education, Inc. All rights reserved. 17 Fig. 5.19 | Precedence/associativity of the operators discussed so far.

18  1992-2007 Pearson Education, Inc. All rights reserved. 18 課堂練習 利用 logical operator 加快完成以下要求: (1) 請輸入 1 個大於等於 2 的正整數 n , 輸出 n 是否為質數 (2) 請輸入 1 個大於等於 2 的正整數 n , 輸出 2 … n 是否為質數

19  1992-2007 Pearson Education, Inc. All rights reserved. 19 5.9 Structured Programming Summary Sequence structure –“ built-in ” to Java Selection structure – if, if…else and switch Repetition structure – while, do…while and for

20  1992-2007 Pearson Education, Inc. All rights reserved. 20 Fig. 5.20 | Java’s single-entry/single-exit sequence, selection and repetition statements.

21  1992-2007 Pearson Education, Inc. All rights reserved. 21 Fig. 5.21 | Rules for forming structured programs. 1.

22  1992-2007 Pearson Education, Inc. All rights reserved. 22 Fig. 5.23 | Repeatedly applying the stacking rule (rule 2) of Fig. 5.21 to the simplest activity diagram.

23  1992-2007 Pearson Education, Inc. All rights reserved. 23 Fig. 5.24 | Repeatedly applying the nesting rule (rule 3) of Fig. 5.21 to the simplest activity diagram.

24  1992-2007 Pearson Education, Inc. All rights reserved. 24 Fig. 5.25 | “Unstructured” activity diagram.

25  1992-2007 Pearson Education, Inc. All rights reserved. 25 課堂練習 -1 Ex-5.23 改寫 fig05_13 的程式,拿掉 continue ,但需有相 同輸出 Ex-5.10 請使用者輸入一個整數 n ,接著以 for 迴圈輸入 n 個實數,並列印其中的最大及最小值 Ex-5.12 請使用者輸入一個整數 n ,接著以 for 迴圈計算 n! ( 即 n * n-1 * … * 1) 的值,將 n! 的值列印出來 ( 請測試 n 大 於 20 的情形 ) Ex-5.15 請輸入 5 個正整數,依序輸出其橫條圖

26  1992-2007 Pearson Education, Inc. All rights reserved. 26 課堂練習 -2 Ex-5.21 畫出長度為 9 的菱形 Ex-5.22 請使用者輸入介於 1 – 19 的奇數 n ,畫出長度為 n 的菱形 Ex-5.24 請模擬 “The Twelve Days of Christmas” 這首歌,以 switch 列 印其歌詞。 [ 提示:重覆的部份不要 break] ( 上圖範例為前 4 天,請參 考: http://www.carols.org.uk/the_twe1ve_days_of_christmas.htm)


Download ppt " 1992-2007 Pearson Education, Inc. All rights reserved. 1 5.7 break and continue Statements Break and continue statements – Alter flow of control break."

Similar presentations


Ads by Google