Example:
int i; String s; while (s != null) { i = Integer.parseInt(s); System.out.println(i*i); s = JOptionPane.showInputDialog("Input an integer"); }
Example: The statement to read the next element to be processed is missing.
int i; String s; s = JOptionPane.showInputDialog("Input an integer"); while (s != null) { i = Integer.parseInt(s); System.out.println(i*i); }
Example: Print out 10 stars.
int i = 0; while (i <= 10) { // should be (i < 10) System.out.print("*"); i++; }
To avoid such types of errors, it is often convenient to test the loop with simple (i.e., small) values for the variables. In the example above, if we check the loop by printing 1 star, instead of 10 (by substituting 10 with 1 in the condition of the loop), we immediately notice that the loop would print 2 stars, instead of 1.