Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class 13. 2 2 Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.

Similar presentations


Presentation on theme: "1 Class 13. 2 2 Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking."— Presentation transcript:

1 1 Class 13

2 2 2 Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking to user input

3 3 while (expression) statement; while (expression) { statement; statement; // Place as many statements here // as necessary. }

4 4 1. Test this expression. 2. If the expression is true, perform the input statement. while (number ! = 99) {response = JOptionPane.showInputDialog( “Enter a number”); number = Integer.parseInt(response); } String response; int number =0;

5 5 1. Test this expression. 2. If the expression is true, perform the code within the { } while (number < 0) {response = JOptionPane.showInputDialog( “Enter a number”); number = Integer.parseInt(response); if (number < 0) JOptionPane.showMessageDialog(null, “error..do not enter numbers less than zero”, ”Error Message”,JOptionPane.ERROR_MESSAGE); } String response; int number =-1;

6 6 // infinite loop int test = 0; while (test < 10) { System.out.println(“Hello”); } int test = 0; while (test < 10) { System.out.println(“Hello”); test = test +1; } Loop

7 7 // infinite loop (ended here) int test = 0; while (test < 10); { System.out.println(“Hello”); test = test +1; } Loop

8 8 int num = 0; while (num <10) { num=num +1; System.out.println(num + “ “ + (num*num)); } Num is compared to 10. If it is less than 10, the code within the { } is executed. When the output statement executes, num is 1 greater than it was in the relational test.

9 9 Program Output 1 24 39 416 525 636 749 864 981 10100

10 10 public static void main(String a[]) {int numScores, count =0 ; String answer1; int total =0; double average; JOptionPane.showMessageDialog(null, “This program will give you the \n“ + “average of test scores. entered”); answer1=JOptionPane.showInputDialog(null, “How many test scores do you have ? “); numScores = Integer.parseInt(answer1); Counter Controlled Loop

11 11

12 12

13 13 while (count <=numScores) { String scoreEntered; int score; count = count +1; scoreEntered = JOptionPane.showinputDialog( null, “Enter score ” + count); score = Integer.parseInt(scoreEntered); total = total + score; } average = (double) total/count; JOptionPane.showMessageDialog(null, “The” + “average of the test scores is “ + average); } Counter Controlled Loop

14 14

15 15

16 16 Conclusion of Class 13


Download ppt "1 Class 13. 2 2 Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking."

Similar presentations


Ads by Google