Sentinel Controlled While Loop

The following java program application uses Sentinel Controlled While Loop. You might not know how many times a set of statements need to be executed, but you do know that the statements need to be executed until a special value is met. This special value is called a sentinel. . I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code:
  1. import java.util.*;
  2.  
  3. public class SentinelControlledWhileLoop
  4. {
  5.         static Scanner console = new Scanner(System.in);
  6.        
  7.         static final int SENTINEL = -999;
  8.        
  9.         public static void main (String[] args)
  10.         {
  11.                 int number; //variable to store the number
  12.                 int sum = 0; //variable to store the sum
  13.                 int count = 0; //variable to store the total numbers read
  14.                
  15.                 System.out.println("Enter positive integers ending with " + SENTINEL);
  16.                
  17.                 number = console.nextInt();
  18.                
  19.                 while (number != SENTINEL)
  20.                 {
  21.                         sum = sum+number;
  22.                         count++;
  23.                         number = console.nextInt();
  24.                 }
  25.                 System.out.printf("the sum of the %d " +
  26.                         "numbers = %d%n", count, sum);
  27.                 if (count != 0)
  28.                         System.out.printf("The average = %d%n", (sum / count));
  29.                 else
  30.                         System.out.println("No input");
  31.         }
  32. }
Sample Run: Enter positive integers ending with -999 10 19 -999 The sum of the 2 numbers = 19 The average = 9 This program works as follows: The statement System.out.println("Enter positive integers ending with " + SENTINEL);prompts the user to enter numbers ending with -999. The statement number = console.nextInt();reads the first number and stores it in the variable number. The while statement while (number != SENTINEL)checks whether number is not equal to SENTINEL. If number is not equal to SENTINEL, the body of the while loop executes. The statement  sum = sum+number;updates the value of sum by adding number to it. The statement  count++; increments the value of count by 1. The statement number = console.nextInt();stores the next number in the variable number. The statements
  1.  sum = sum+number;
  2.                         count++;
  3.                         number = console.nextInt();
repeats until the programs reads -999. The statement
  1.  System.out.printf("the sum of the %d " +
  2.                         "numbers = %d%n", count, sum);
outputs the sum of the numbers, and the statements
  1.  if (count != 0)
  2.                         System.out.printf("The average = %d%n", (sum / count));
  3.                 else
  4.                         System.out.println("No input");
output the average of the numbers.

Add new comment