### do ... while

Figure 161. A do ... while loop
 int sum = 0, value; do { System.out.print( "Enter value, 0 to terminate: "); value = scan.nextInt(); sum += value; } while (0 != value); System.out.println("Sum: " + sum);Enter value, 0 to terminate: 3 Enter value, 0 to terminate: 1 Enter value, 0 to terminate: 0 Sum: 4 Figure 162. do ... while syntax
do
(block | statement)
while (booleanExpression); No. 70

#### Even or odd?

Q:

Write an application which asks for integer values telling its user whether a given value is even or odd. Providing the special value 0 shall terminate your application:

Enter an integer (0 to terminate): 77
77 is an odd number
Enter an integer (0 to terminate): -3
-3 is an odd number
Enter an integer (0 to terminate): 26
26 is an even number
Enter an integer (0 to terminate): 0
0 is an even number
Goodbye!

### Tip

1. Use the modulo % operator.

2. Choose an appropriate loop type with respect to your application's termination on user input «0».

A:

We obviously need a loop to ask for further input unless the last entered value was 0. In any case the loop's statement will be executed at least once:

Enter an integer (0 to terminate): 0
0 is an even number
Goodbye!

A do ... while(...) rather than a while(...) loop is thus appropriate. It allows for entering the loop's body before any check is about to happen:

try(final Scanner scanner = new Scanner(System.in)){

int userInput;
do {
System.out.print("Enter an integer (0 to terminate): ");
userInput = scanner.nextInt();
if (0 == userInput % 2) {
System.out.println("    " + userInput + " is an even number");
} else {
System.out.println("    " + userInput + " is an odd number");
}
} while (0 != userInput);
System.out.println("Goodbye!");
} No. 71

#### Square root approximation

Q:

Derived from the Newton–Raphson method we can approximate a given value $a$'s square root $a$ by the following recursively defined series:

Start:
$x 0 = a 2$
Recursion step:
$x n + 1 = 1 2 ⁢ ( x n + a x n )$

Create a program which interactively asks a user for a positive value and calculate it's square root like e.g.:

Enter a non-negative value: 2.0
The square root of 2.0 is close to 1.414213562373095
It's square is 1.9999999999999996

### Tip

Due to the limited precision of machine arithmetics you may continue until your approximation value no longer changes.

A:

We introduce two variables x_current and x_next referring to $x n$ and $x n + 1$ respectively:

try (final Scanner scan = new Scanner(System.in)) {

System.out.print("Enter a non-negative value: ");
final double a = scan.nextDouble();

double x_next = a / 2, x_current;

do {
x_current = x_next;                          // Save current approximation value
x_next = (x_current + a / x_current) / 2;    // Calculate next series value
} while (x_next != x_current);                 // Did we get any closer?

System.out.println("The square root of " + a + " is close to " + x_next);
System.out.println("It's square is " + x_next * x_next);
}