Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection Statements Chapter 4 Loops Chapter 5 Methods 19.1-19.3 in Chapter 19 Recursion Chapter 6 Arrays Chapter 23 Algorithm Efficiency and Sorting Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1
To use while, do-while, and for loop statements to control the repetition of statements ( 4.2-4.4). To understand the flow of control in loop statements ( 4.2-4.4). To use Boolean expressions to control loop statements ( 4.2-4.4). To write nested loops ( 4.5). To know the similarities and differences of three types of loops ( 4.6). To implement program control with break and continue ( 4.7). Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2
while (loop-continuation-condition) { // loop-body; Statement(s); int count = 0; while (count < 100) { count++; count = 0; Loop Continuation Condition? false (count < 100)? false true Statement(s) (loop body) true count++; (A) (B) rights reserved. 0-13-222158-6 3
int count = 0; while (count < 2) { count++; Initialize count rights reserved. 0-13-222158-6 4
int count = 0; while (count < 2) { count++; (count < 2) is true rights reserved. 0-13-222158-6 5
int count = 0; while (count < 2) { count++; Print Welcome to Java rights reserved. 0-13-222158-6 6
int count = 0; while (count < 2) { count++; Increase count by 1 count is 1 now rights reserved. 0-13-222158-6 7
int count = 0; while (count < 2) { count++; (count < 2) is still true since count is 1 rights reserved. 0-13-222158-6 8
int count = 0; while (count < 2) { count++; Increase count by 1 count is 2 now rights reserved. 0-13-222158-6 9
int count = 0; while (count < 2) { count++; Print Welcome to Java rights reserved. 0-13-222158-6 10
int count = 0; while (count < 2) { count++; (count < 2) is false since count is 2 now rights reserved. 0-13-222158-6 11
int count = 0; while (count < 2) { count++; The loop exits. Execute the next statement after the loop. rights reserved. 0-13-222158-6 12
The Math subtraction learning tool program generates just one question for each run. You can use a loop to generate questions repeatedly. This example gives a program that generates ten questions and reports the number of the correct answers after a student answers all ten questions. SubtractionTutorLoop Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a value is known as a sentinel value. Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. SentinelValue rights reserved. 0-13-222158-6 13
Statement(s) (loop body) do { // Loop body; Statement(s); true Loop Continuation Condition? false while (loop-continuation-condition); rights reserved. 0-13-222158-6 14
for (initial-action; loopcontinuation-condition; action-after-each-iteration){ // loop body; Statement(s); int i; for (i = 0; i < 100; i++) { System.out.println( "Welcome to Java!"); Initial-Action i = 0 Loop Continuation Condition? false (i < 100)? false true Statement(s) (loop body) true System.out.println( "Welcome to Java"); Action-After-Each-Iteration i++ (A) (B) rights reserved. 0-13-222158-6 15
int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); Declare i rights reserved. 0-13-222158-6 16
int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); Execute initializer i is now 0 rights reserved. 0-13-222158-6 17
int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); (i < 2) is true since i is 0 rights reserved. 0-13-222158-6 18
int i; for (i = 0; i < 2; i++) { Print Welcome to Java rights reserved. 0-13-222158-6 19
int i; for (i = 0; i < 2; i++) { Execute adjustment statement i now is 1 rights reserved. 0-13-222158-6 20
int i; for (i = 0; i < 2; i++) { (i < 2) is still true since i is 1 rights reserved. 0-13-222158-6 21
int i; for (i = 0; i < 2; i++) { Print Welcome to Java rights reserved. 0-13-222158-6 22
int i; for (i = 0; i < 2; i++) { Execute adjustment statement i now is 2 rights reserved. 0-13-222158-6 23
int i; for (i = 0; i < 2; i++) { (i < 2) is false since i is 2 rights reserved. 0-13-222158-6 24
Exit the loop. Execute the next int i; statement after the loop for (i = 0; i < 2; i++) { rights reserved. 0-13-222158-6 25
The initial-action in a for loop can be a list of zero or more commaseparated expressions. The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. Therefore, the following three for loops are correct. for (int i = 1; i < 100; System.out.println(i++)); for (int i = 0, j = 0; (i + j < 10); i++, j++) { // Do something int i=0; for(;i<100;){ i++; rights reserved. 0-13-222158-6 26
If the loop-continuation-condition in a for loop is omitted, it is implicitly true. Thus the statement given below in (a), which is an infinite loop, is correct. Nevertheless, it is better to use the equivalent loop in (b) to avoid confusion: for ( ; ; ) { // Do something (a) Equivalent while (true) { // Do something (b) while (true) { System.out.println("H"); //cause error unreachable statement rights reserved. 0-13-222158-6 27
Problem: Write a program that sums a series that starts with 0.01 and ends with 1.0. The numbers in the series will increment by 0.01, as follows: 0.01 + 0.02 + 0.03 and so on. TestSum Problem: Write a program that uses nested for loops to print a multiplication table. TestMultiplicationTable rights reserved. 0-13-222158-6 28
The three forms of loop statements, while, do-while, and for, are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the following for loop in (b): while (loop-continuation-condition) { // Loop body (a) Equivalent for ( ; loop-continuation-condition; ) // Loop body (b) A for loop in (a) in the following figure can generally be converted into the following while loop in (b) for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; (a) Equivalent initial-action; while (loop-continuation-condition) { // Loop body; action-after-each-iteration; (b) rights reserved. 0-13-222158-6 29
Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below: for (int i=0; i<10; i++); { Syntax Error System.out.println("i is " + i); int j=0; for (; j<10; j++); { System.out.println("j is " + j); //j is 10 Logic Error rights reserved. 0-13-222158-6 30
Similarly, the following loop is also wrong: int i=0; while (i < 10);{ Logic Error System.out.println("i is " + i); i++; //infinity loop In the case of the do loop, the following semicolon is needed to end the loop. int i=0; do { System.out.println("i is " + i); i++; while (i<10); Correct rights reserved. 0-13-222158-6 31
break immediately ends the innermost loop that contain it. continue only ends the current iteration. Program control goes to the end of the loop body. Both are used with an if statement. rights reserved. 0-13-222158-6 32
public class TestBreak { /** Main method */ public static void main(string[] args) { int sum = 0; int number = 0; while (number < 20) { number++; sum += number; if (sum >= 100) break; System.out.println("The number is " + number); System.out.println("The sum is " + sum); //The number is 14, // The sum is 105 TestBreak rights reserved. 0-13-222158-6 33
public class TestContinue { /** Main method */ public static void main(string[] args) { int sum = 0; int number = 0; while (number < 5) { number++; if (number == 1 number == 3) continue; sum += number; System.out.println("The sum is " + sum); // The sum is 11 // 2+4+5 ข าม 1,3 TestContinue rights reserved. 0-13-222158-6 34