What Part of a Loop Continues Until a Special Value is Input

The preceding example executes the loop ten times. If you want the user to decide whether to take another question, you can use a confirmation dialog to control the loop. A confirmation dialog can be created using the following statement:

When a button is clicked, the method returns an option value. The value is JOptionPane.YES_OPTION ( 0 ) for the Yes button, JOptionPane.NO_OPTION ( 1 ) for the No button, and JOptionPane.CANCEL_OPTION ( 2 ) for the Cancel button. For example, the following loop continues to execute until the user clicks the No or Cancel button.

int option = 0;

while (option == JOptionPane.YES_OPTION) {

  System.out.println("continue loop");

  option = JOptionPane.showConfirmDialog(null, "Continue?");

}

                       Java:  Controlling a Loop with a Sentinel Value

Another common technique for controlling a loop is to designate a special value when reading and processing a set of values. This special input value, known as a sentinel value, signifies the end of the loop.

writes a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. Do you need to declare a new variable for each input value? No. Just use one variable named data (line 9) to store the input value and use a variable named sum (line 12) to store the total. Whenever a value is read, assign it to data and added to sum (line 14) if it is not zero.

Example:  SentinelValue.java

 1 import javax.swing.JOptionPane;

 2

 3 public class SentinelValue {

 4 /** Main method */

 5 public static void main(String[] args) {

 6 // Read an initial data

 7     String dataString = JOptionPane.showInputDialog(

 8"Enter an int value:\n(the program exits if the input is 0)");

 9 int data = Integer.parseInt(dataString);

10

11 // Keep reading data until the input is 0

12 int sum = 0;

13 while (data != 0 ) {

14       sum += data;

15

16 // Read the next data

17       dataString = JOptionPane.showInputDialog(

18 "Enter an int value:\n(the program exits if the input is 0)");

19       data = Integer.parseInt(dataString);

20 }

21

22      JOptionPane.showMessageDialog(null, "The sum is " + sum);

23   }

24 }

A sample run of the program is shown in. If data is not 0 , it is added to the sum (line 14) and the next items of input data are read (lines 12–19). If data is 0 , the loop body is no longer executed and the while loop terminates. The input value 0 is the sentinel value for this loop. Note that if the first input read is 0 , the loop body never executes, and the resulting sum is 0 .

The program uses a while loop to add an unspecified number of integers.

Caution

Don't use floating-point values for equality checking in a loop control. Since floating-point values are approximations for some values, using them could result in imprecise counter values and inaccurate results. This example uses int value for data . If a floating-point type value is used for data , (data != 0) may be true even though data is exactly 0 .

Here is a good example provided by a reviewer of this book:

// data should be zero

double data = Math.pow(Math.sqrt(2), 2) – 2;

if (data == 0)

  System.out.println("data is zero");

else

  System.out.println("data is not zero");

Like pow , sqrt is a method in the Math class for computing the square root of a number. The variable data in the above code should be zero, but it is not, because of rounding-off errors.

The do-while Loop

The do-while loop is a variation of the while loop. Its syntax is given below:

do {

// Loop body;

  Statement(s);

} while (loop-continuation-condition); .

The loop body is executed first. Then the loop-continuation-condition is evaluated. If the evaluation is true , the loop body is executed again; if it is false , the do-while loop terminates. The major difference between a while loop and a do-while loop is the order in which the loop-continuation-condition is evaluated and the loop body executed.

The while loop and the do-while loop have equal expressive power. Sometimes one is a more convenient choice than the other. For example,

TestDo.java


 1 import javax.swing.JOptionPane;

 2

 3 public class TestDoWhile {

 4 /** Main method */

 5 public static void main(String[] args) {

 6 int data;

 7 int sum = 0;

 8

 9 // Keep reading data until the input is 0

10 do {

11 // Read the next data

12       String dataString = JOptionPane.showInputDialog(null,

13 "Enter an int value:\n(the program exits if the input is 0)",

14 "TestDo", JOptionPane.QUESTION_MESSAGE);

15

16       data = Integer.parseInt(dataString);

17

18       sum += data;

19 } while (data != 0);

20

21     JOptionPane.showMessageDialog(null, "The sum is " + sum,

22 "TestDo", JOptionPane.INFORMATION_MESSAGE);

23  }

24 }

kingdism1943.blogspot.com

Source: https://lessonsonjava.blogspot.com/2014/07/java-controlling-loop-with-confirmation.html

0 Response to "What Part of a Loop Continues Until a Special Value is Input"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel