Download presentation
Presentation is loading. Please wait.
1
©2004 Brooks/Cole Chapter 5 Repetition
2
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set of data. More useful is to be able to process many sets of data in the same way Repetition statements allow us to do this. Java has three repetition statements –while –do … while –for
3
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The while statement Syntax while ( ) Semantics –test the condition –if true, execute the body and go back to test –if false, continue to next statement in program
4
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Anatomy of a while Loop
5
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Loop Example int count = 1; while (count <= 10) { System.out.println( count + " "); count++; } Body of loop needs to update the count variable –Otherwise, you will have an infinite loop Above is example of counting loop
6
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Loop Example Schematically, a while looks like the following var = initVar(); while (testVar()) { doBody(); var = updateVar(); } Update can be any kind of change to var –increment, decrement, input, …
7
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Interactive Input Reading a fixed number of values
8
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accumulating Input Values Suppose we want to average the values we read using a loop. –Since we use the same variable for all the input values, we need to add each value to the total as we enter it. Later we will learn to use arrays to save the values we read in.
9
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accepting and Adding a Number to a Total
10
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accumulation Flow of Control AddNumbers.java AverageNumbers.java
11
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sentinel Loops Sometimes, we don't know how many values need to be entered ahead of time. In this case, we can read values until a specified sentinel value is entered. Example: Sentinels.java –A number greater than 100 will signal the end of the input
12
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Interrupting loops Sometimes you need to exit a loop before it is finished –The break statement jumps to next statement after the loop –loop and a half The continue statement causes a jump to the next iteration of the loop
13
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 For loops for syntax for(init; condition; update) statement; Equivalent while init; while (condition) { statement; update; } Counting loops are common enough that Java provides a special syntax for them
14
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The for Statement’s Flow of Control 1.init is done before the loop starts 2.condition is tested 3.statement is executed 4.update is executed 5.go back to 2
15
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Nested loops The body of a loop can include any valid Java statement If the body includes another loop, we say the loops are nested
16
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Nested loops The inner loop is executed completely for each iteration of the outer loop The body of the inner loop is executed 3*4=12 times
17
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 do - while loops Sometimes, it is more convenient to test the loop condition after the loop body has executed. do { statement; } while (condition); The body of the do-while loop always executes at least once
18
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The do Statement’s Flow of Control
19
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Input validation Loops are often used to validate input do readData; while (dataNotValid);
20
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Testing for valid data with Scanner The Scanner class has some methods you can use to check for the right kind of data. boolean hasNext() boolean hasNextInt() boolean hasNextDouble() You can use a loop to read for as long as there is data of the proper type.
21
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Reading to end of valid input Scanner kbd = new Scanner( system.in); int value; while ( Scanner.hasNextInt()) { value = kbd.nextInt(); process data }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.