= 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 ); }"> = 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 ); }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program."— Presentation transcript:

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 of the day

2 Last Time’s Program import 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 );

3 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 ); }

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

5 Repetition Structures while ( condition ) some statement; while ( condition ) { some statement; normally increment counter; }

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

7 Repetition Structures public 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 ); }

8 1 st Program of the Day: Average1 notice how the while repetition structure is used. note how variables get new values.

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

10 Looking at Average1.java: pt. 1 import 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; }

11 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 ); }

12 Comparing Average1 & Average2 //processing phase while ( gradeCounter <= 10 ) { grade = JOptionPane.showInputDialog ( "Enter integer grade: " ); gradeValue = Integer.parseInt ( grade ); total = total + gradeValue ; gradeCounter=gradeCounter + 1; } //termination phase average = total / 10 ; JOptionPane.showMessageDialog ( null, "average is “ + average ); //processing phase while ( gradeValue != -1 ) { total = total + gradeValue ; gradeCounter = gradeCounter + 1; grade = JOptionPane.showInputDialog ( "Enter integer grade: " ); gradeValue = Integer.parseInt ( grade ); } //termination phase average = total / gradeCounter ; JOptionPane.showMessageDialog ( null, "average is “ + average );

13 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


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

Similar presentations


Ads by Google