Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.

Similar presentations


Presentation on theme: "Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer."— Presentation transcript:

1 Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu Department of Computer and Information Science, School of Science, IUPUI

2 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

3 Dale Roberts 3 Fig. 4.4 | while repetition statement UML activity diagram.

4 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)

5 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

6 Dale Roberts 6Outline GradeBook.java (1 of 3) Assign a value to instance variable courseName Declare method setCourseName Declare method getCourseName

7 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

8 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

9 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.

10 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.

11 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

12 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

13 Dale Roberts 13 Common Programming Error 4.6 Choosing a sentinel value that is also a legitimate data value is a logic error.

14 Dale Roberts 14 Fig. 5.3 | for statement header components.

15 Dale Roberts 15 UML activity diagram for the for statement in Fig. 5.2.

16 Dale Roberts 16 5.3 for Repetition Statement (Cont.) for ( initialization; loopContinuationCondition; increment ) statement; can usually be rewritten as: initialization; while ( loopContinuationCondition ) { statement; increment; } { statement; increment; }

17 Dale Roberts 17 5.4 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 )

18 Dale Roberts 18Outline DoWhile Test.jav a Line 8 Lines 10-14 Progra m output Declares and initializes control variable counter Variable counter’s value is displayed before testing counter’s final value

19 Dale Roberts 19 Fig. 5.8 | do...while repetition statement UML activity diagram.

20 Dale Roberts 20Outline GradeB ook.java (1 of 5) Lines 8-14

21 Dale Roberts 21Outline GradeB ook.java (2 of 5) Lines 50-54 Display prompt

22 Dale Roberts 22Outline GradeB ook.java (3 of 5) Line 57 Line 72 controlling expression Lines 72-94 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

23 Dale Roberts 23Outline GradeB ook.java (4 of 5) Line 91 default case default case for grade less than 60

24 Dale Roberts 24Outline GradeB ook.java (5 of 5)

25 Dale Roberts 25Outline GradeB ookTest.java (1 of 2) Lines 13-15 Call GradeBook public methods to count grades

26 Dale Roberts 26Outline GradeB ookTest.java (2 of 2) Program output

27 Dale Roberts 27 Fig. 5.20 | Java’s single-entry/single-exit sequence, selection and repetition statements.

28 Dale Roberts Acknowledgements Deitel, Java How to Program


Download ppt "Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer."

Similar presentations


Ads by Google