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

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part I.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Control Statements: Part 1
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
while Repetition Structure
Lecture 7: Repeating a Known Number of Times
Control Statements: Part 1
Control Statements: Part 2
Ch 7: JavaScript Control Statements I.
Formulating Algorithms, Repetition Control Statements
Chapter 4- part 2 Control Statements: Loops 1
Control Statements: Part 1
Control Statements: Part 1
Program Control using Java - Theory
Advanced Programming Chapters 5 & 6: Control Structures
Chapter 4 Control Statements: Loops 1
MSIS 655 Advanced Business Applications Programming
3 Control Statements:.
Control Statements: Part 1
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Dale Roberts, Lecturer IUPUI
Control Statements:.
Presentation transcript:

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