Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI
Dale Roberts 2 while Repetition Statement while statement Repeats an action while its loop-continuation condition remains true Uses a merge symbol in its UML activity diagram Merges two or more workflows Represented by a diamond (like decision symbols) but has: Multiple incoming transition arrows, Only one outgoing transition arrow and No guard conditions on any transition arrows
Dale Roberts 3 Fig. 4.4 | while repetition statement UML activity diagram.
Dale Roberts 4 Formulating Algorithms: Counter-Controlled Repetition Counter-controlled repetition Use a counter variable to count the number of times a loop is iterated Integer division The fractional part of an integer division calculation is truncated (thrown away)
Dale Roberts 5 Fig. 4.5 | Pseudocode algorithm that uses counter-controlled repetition to solve the class-average problem. 1Set total to zero 2Set grade counter to one 3 4While grade counter is less than or equal to ten 5Prompt the user to enter the next grade 6Input the next grade 7Add the grade into the total 8Add one to the grade counter 9 10Set the class average to the total divided by ten 11Print the class average
Dale Roberts 6Outline GradeBook.java (1 of 3) Assign a value to instance variable courseName Declare method setCourseName Declare method getCourseName
Dale Roberts 7Outline GradeBook.java (2 of 3) Declare method displayMessage Declare method determineClassAverage Declare and initialize Scanner variable input Declare local int variables total, gradeCounter, grade and average
Dale Roberts 8Outline GradeBook.java (3 of 3) while loop iterates as long as gradeCounter <= 10 Increment the counter variable gradeCounter Calculate average grade Display results
Dale Roberts 9 Common Programming Error Using the value of a local variable before it is initialized results in a compilation error. All local variables must be initialized before their values are used in expressions. Java initializes all variables to 0 by default.
Dale Roberts 10 Common Programming Error Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the loop never terminates.
Dale Roberts 11 Outline GradeBook Test.java Create a new GradeBook object Pass the course’s name to the GradeBook constructor as a string Call GradeBook ’s determineClassAverage method
Dale Roberts 12 Formulating Algorithms: Sentinel-Controlled Repetition Sentinel-controlled repetition Also known as indefinite repetition Use a sentinel value (also known as a signal, dummy or flag value) A sentinel value cannot also be a valid input value
Dale Roberts 13 Common Programming Error 4.6 Choosing a sentinel value that is also a legitimate data value is a logic error.
Dale Roberts 14 Fig. 5.3 | for statement header components.
Dale Roberts 15 UML activity diagram for the for statement in Fig. 5.2.
Dale Roberts for Repetition Statement (Cont.) for ( initialization; loopContinuationCondition; increment ) statement; can usually be rewritten as: initialization; while ( loopContinuationCondition ) { statement; increment; } { statement; increment; }
Dale Roberts Examples Using the for Statement Varying control variable in for statement Vary control variable from 1 to 100 in increments of 1 for ( int i = 1; i <= 100; i++ ) Vary control variable from 100 to 1 in increments of –1 for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in increments of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in decrements of 2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 )
Dale Roberts 18Outline DoWhile Test.jav a Line 8 Lines Progra m output Declares and initializes control variable counter Variable counter’s value is displayed before testing counter’s final value
Dale Roberts 19 Fig. 5.8 | do...while repetition statement UML activity diagram.
Dale Roberts 20Outline GradeB ook.java (1 of 5) Lines 8-14
Dale Roberts 21Outline GradeB ook.java (2 of 5) Lines Display prompt
Dale Roberts 22Outline GradeB ook.java (3 of 5) Line 57 Line 72 controlling expression Lines Loop condition uses method hasNext to determine whether there is more data to input switch statement determines which case label to execute, depending on controlling expression (grade / 10 ) is controlling expression
Dale Roberts 23Outline GradeB ook.java (4 of 5) Line 91 default case default case for grade less than 60
Dale Roberts 24Outline GradeB ook.java (5 of 5)
Dale Roberts 25Outline GradeB ookTest.java (1 of 2) Lines Call GradeBook public methods to count grades
Dale Roberts 26Outline GradeB ookTest.java (2 of 2) Program output
Dale Roberts 27 Fig | Java’s single-entry/single-exit sequence, selection and repetition statements.
Dale Roberts Acknowledgements Deitel, Java How to Program