// This variation of the previous SentinelSum program uses a "forever loop" // with a break statement to solve a sentinel problem. import java.util.*; public class SentinelSum2 { public static final int SENTINEL = -1; public static void main(String[] args) { Scanner console = new Scanner(System.in); int sum = 0; // infinite "forever" loop with break in the middle while (true) { System.out.print("Enter a number (" + SENTINEL + " to quit): "); int number = console.nextInt(); if (number == SENTINEL) { break; // exit the loop } sum += number; } System.out.println("The total was " + sum); } }