9/20: the while repetition structure last time’s program repetition structures: what they are the...
TRANSCRIPT
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
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 );
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 ); }}
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.
Repetition Structures• while ( condition )
some statement;
• while ( condition ) {some statement;normally increment counter;
}
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 ); }}
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 ); }}
1st Program of the Day: Average1
• notice how the while repetition structure is used.
• note how variables get new values.
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.
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; }
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 ); }}
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 );
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