Can someone explain to me what a sentinel does in Java? Or how it works?
Asked Answered
R

3

5

I am trying to understand what sentinel is or how it works with the program. Anyways this is the block of code I am trying to understand. I know it is a sentinel control loop, but I don't know what it does.

private static final int SENTINEL = -999

From what I have Googled is that by having a negative integer it indicates the end of a sequence. But how does it do that? Oh, and how do I initialize the sentinel? Is it already initialized?

public static int gameScore(int[] teamBoxScore) { //This is telling me the name of an array
int output = 0;
for (int v : teamBoxScore){ //I know that this the values of the array will be stored in the variable "v".
     if (v !=SENTIENL) {// 
         output += v; 
      }
}
return output;
} 

Please and Thank you! I am learning how to code with Java

Recombination answered 9/2, 2014 at 23:24 Comment(2)
Take a look at the definition of sentinel, by hovering cursor over sentinel tag.Dzoba
In terms of efficiency/performance a sentinel avoids the necessity to check for "end of loop" in every iteration.Wildman
R
10

There is nothing special about a "sentinel." It is simply any constant of your choosing that is not a legitimate value in a data set, so you can use it to mark the end of a sequence. For example, if a team's box score will never be less than zero, -999 (or any other negative value) can be used as a break/end marker.

Usually, there are better, cleaner ways to do this.

Randeerandel answered 9/2, 2014 at 23:28 Comment(2)
So the sentinel tells the program when to stop? But how?Recombination
You can check for this value in the if statement in your loop. If -999 is found, exit the loop.Randeerandel
C
3

to understant sentinel-controlled repetition let's see a simple example,

import java.util.Scanner; // program uses class Scanner

public class ClassAverage
{
  public static void main(String[] args)
  {
     // create Scanner to obtain input from command window
     Scanner input = new Scanner(System.in);

     // initialization phase
     int total = 0; // initialize sum of grades
     int gradeCounter = 0; // initialize # of grades entered so far

     // processing phase
     // prompt for input and read grade from user    
     System.out.print("Enter grade or -1 to quit: ");
     int grade = input.nextInt();                    

     // loop until sentinel value read from user
     while (grade != -1)
     {
        total = total + grade; // add grade to total
        gradeCounter = gradeCounter + 1; // increment counter

        // prompt for input and read next grade from user
        System.out.print("Enter grade or -1 to quit: "); 
        grade = input.nextInt();                         
     }

     // termination phase
     // if user entered at least one grade...
     if (gradeCounter != 0)
     {
        // use number with decimal point to calculate average of grades
        double average = (double) total / gradeCounter;                

        // display total and average (with two digits of precision)
        System.out.printf("%nTotal of the %d grades entered is %d%n",
           gradeCounter, total);
        System.out.printf("Class average is %.2f%n", average);
     }
     else // no grades were entered, so output appropriate message
        System.out.println("No grades were entered");
  }
} // end class ClassAverage

now let's run it

Enter grade or -1 to quit: 97
Enter grade or -1 to quit: 88
Enter grade or -1 to quit: 72
Enter grade or -1 to quit: -1

Total of the 3 grades entered is 257
Class average is 85.67

In a sentinel-controlled loop, prompts should remind the user of the sentinel. its a good Practice to do

reference: Java™ How To Program (Early Objects), Tenth Edition

Cervix answered 8/2, 2017 at 14:19 Comment(0)
H
1

Sentinel value is used to avoid extra checks inside loops.

For instance, when searching for a particular value in an unsorted list, every element will be compared against this value, with the loop terminating when equality is found; however to deal with the case that the value should be absent, one must also test after each step for having completed the search unsuccessfully. By appending the value searched for to the end of the list, an unsuccessful search is no longer possible, and no explicit termination test is required in the inner loop; afterwards one must still decide whether a true match was found, but this test needs to be performed only once rather than at each iteration.

Refer https://en.wikipedia.org/wiki/Sentinel_value

Hluchy answered 26/1, 2019 at 23:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.