READING Chapter 6 Section 1 "The while Statement" - repetition statement - type of control statement - repeatedly execute a block of code - aka loop statement - recursive method - method that calls itself - alternative to repetition - while statement - syntax while ( ) - - or - compound statement requires braces - aka loop body - loop body is executed as long as the boolean expression is true - diagram - diamond for loop condition - rectangle for loop body - arrow into diamond - true arrow from diamond to rectangle - arrow from rectangle to above diamond - false arrow out of diamond - count-controlled loop - execute loop body a fixed number of times - repetition control to improve the user interface - previously assumed the input was valid - more user friendly to handle invalid input - selection statement limitations - only allows one alternative - e.g. printing an error message - better to allow the user to re-enter the value until it is correct - priming read: reading of a value before the testing is done - sentinel-controlled loop: loop body is executed repeatedly until any one of the designated values (sentinel) is encountered - += Quick Check 1. int curNum = 11; int sum = 0; while (curNum <= 20) { sum = sum + curNum; curNum++; } This is a count-controlled loop. 2. String strEntered = JOptionPane.showInputDialog("Enter a real number:"); double dEntered = Double.parseDouble(strEntered); while (dEntered >= 0) { strEntered = JOptionPane.showInputDialog("Enter a different number:"); dEntered = Double.parseDouble(strEntered); } 3. Change the while loop to: while (sleepHour != 0) { if (sleepHour > 0) { sum += sleepHour; cnt++; sleepHour = getDouble("Enter sleep hours (0 - to stop:"); } }