9/20: the while repetition structure last time’s program repetition structures: what they are the...

13
9/20: The while Repetition Structure • last time’s program • repetition structures: what they are • the while repetition structure • homework due on Thursday • program of the day

Upload: toby-marshall

Post on 04-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

9/20: The while Repetition Structure

• last time’s program

• repetition structures: what they are

• the while repetition structure

• homework due on Thursday

• program of the day

Page 2: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Last Time’s Programimport javax.swing.JOptionPane;

public class LetterGrades { public static void main ( String args[] ) { String input; //original form of input double grade; //converted form of input

input = JOptionPane.showInputDialog ( "Enter a number grade” + “ (0-100):" );

grade = Double.parseDouble ( input );

Page 3: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Last Time’s Program if ( grade >= 89.5 ) JOptionPane.showMessageDialog ( null , grade + " is an A" ); else if ( grade >= 79.5 ) JOptionPane.showMessageDialog ( null , grade + " is an B" ); else if ( grade >= 69.5 ) JOptionPane.showMessageDialog ( null , grade + " is an C" ); else if ( grade >= 59.5 ) JOptionPane.showMessageDialog ( null , grade + " is an D" ); else JOptionPane.showMessageDialog ( null , grade + " is an F" );

System.exit ( 0 ); }}

Page 4: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Repetition Structures

• Already learned sequential structures “do this, then that”

• Already learned selection structures“if this, do that”

• Repetition Structures “while this, do that”Repeat some action as long as a condition is

true.

Page 5: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Repetition Structures• while ( condition )

some statement;

• while ( condition ) {some statement;normally increment counter;

}

Page 6: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Repetition Structurespublic class Moolah { public static void main ( String args[] ) { int x; x = 0; while ( x < 60 ) { System.out.print ( "$" ); x = x + 1 ; }

System.exit ( 0 ); }}

Page 7: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Repetition Structurespublic class Moolah2 { public static void main ( String args[] ) { int x = 0; while ( x <= 100 ) { System.out.print ( "$" ); if ( (x % 10) == 0 ) System.out.println ( "*" ); x = x + 1 ; } System.exit ( 0 ); }}

Page 8: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

1st Program of the Day: Average1

• notice how the while repetition structure is used.

• note how variables get new values.

Page 9: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Part II: Sentinel-controlled repetition

• Rather than counting the number of repetitions using a counter, we can use a “stop here” signal from the user.

• Average1.java program used counter-controlled repetition:– had to put in ten grades: no more, no less.

• Average2.java uses sentinel-controlled repetition:– will continue until the sentinel value is input by the

user.

Page 10: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Looking at Average1.java: pt. 1import javax.swing.JOptionPane;

public class Average1 { public static void main ( String args[] ) { int total, gradeCounter, gradeValue, average; String grade; total = 0; gradeCounter = 1; while ( gradeCounter <= 10 ) { grade = JOptionPane.showInputDialog ( "Enter grade" ); gradeValue = Integer.parseInt ( grade ); total = total + gradeValue; gradeCounter = gradeCounter + 1; }

Page 11: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Looking at Average1.java: pt. 2 total = 0;

gradeCounter = 1; while ( gradeCounter <= 10 ) { grade = JOptionPane.showInputDialog ( "Enter grade" ); gradeValue = Integer.parseInt ( grade ); total = total + gradeValue; gradeCounter = gradeCounter + 1; } average = total / 10; JOptionPane.showMessageDialog ( null , "Class average is " +

average, "Class Avg.", JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); }}

Page 12: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Comparing Average1 & Average2//processing phasewhile ( gradeCounter <= 10 ) { grade = JOptionPane.showInputDialog ( "Enter integer grade: " ); gradeValue = Integer.parseInt ( grade );

total = total + gradeValue ; gradeCounter=gradeCounter + 1;}

//termination phaseaverage = total / 10 ;

JOptionPane.showMessageDialog ( null, "average is “ + average );

//processing phasewhile ( gradeValue != -1 ) { total = total + gradeValue ; gradeCounter = gradeCounter + 1;

grade = JOptionPane.showInputDialog ( "Enter integer grade: " ); gradeValue = Integer.parseInt ( grade );}

//termination phaseaverage = total / gradeCounter ;

JOptionPane.showMessageDialog ( null, "average is “ + average );

Page 13: 9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program

Average2.java pg. 132

• Average1 program modified using sentinel-controlled repetition

• note the conversion of the integer values into a double value for average

• note the program flow