What is a loop sentinel value?

Your browser does not support JavaScript!
Please enable JavaScript to use the full functionality of the site.
Without JavaScript, you will not be able to expand the content tables in the PD Materials Page and CSP Index Page.

A sentinel value is a special value used to terminate a loop when reading data. In the following program, test scores are provided (via user input). Once the sentinel value of -1 is input, the loop terminates.  At that point, the average of the test scores will be printed.

# 02Loops example_08 SentinalValue.py

score = input("enter a test score, use -1 to stop ")    #enter the first value
total = 0
scoreCounter = 0                        

while score != -1:                      #loop until the sentinel value (-1) is entered
        total = total + score
        scoreCounter = scoreCounter + 1
        score = input("enter a test score, use -1 to stop ")  

print ("The average for the test is ",total/scoreCounter)

This problem can be extended in a number of ways.

  • Ask students to maintain the minimum and maximum score in the while loop.
  • Ask students to store the values input in a list which can then be sorted and analyzed (e.g., determine the median score, 25% percentile, the score that appears most often).

What is a loop sentinel value?
This work is supported by the National Science Foundation under grant number 1502462. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation"

Hello guys, Welcome to another tutorial, and in this tutorial, we will learn about Sentinel Value Java with examples and detailed explanations.

So Let’s start our tutorial Sentinel Value Java and Learn What is a sentinel value in Java with proper examples.

Also Read : Calculator Program in Java

Contents

  • 1 Sentinel Value Java | Sentinel loop Java | Sentinel Controlled loop Java
    • 1.1 What is a Sentinel Value in Java?
    • 1.2 What are sentinel values used for?
    • 1.3 Sentinel Value Java Example/Sentinel Controlled Loop(Numbers Example)
    • 1.4 String Sentinel Value Java
    • 1.5 What is the difference between a sentinel controlled loop and a count controlled loop?
    • 1.6 What is the advantage of using a sentinel controlled loop?
    • 1.7 What is another word for sentinel?
    • 1.8 Why should the value for sentinel be chosen carefully?
    • 1.9 Download Sentinel loop Java Source Code

Before arriving at the example, we try to understand what is a sentinel value in Java, and why do we use it?

What is a Sentinel Value in Java?

  • Sometimes the endpoint of input data is not known. A Java sentinel value can be used to notify the program to stop acquiring input.
  • If the input is from a user, the user could enter a specific value to stop collecting data. (For example, -1, where standard input is a positive number).
  • A sentinel value is simply a value that is used to terminate the while loop or do-while loop and stop getting inputs.
  • A sentinel value is usually used with while and do-while loops.

What are sentinel values used for?

  • For indicating when the user is done with entering values.
  • It should be something that is not a valid value but of the same data type.
  • It allows users to tell the program they’re done with input.
  • It is a unique value that isn’t part of the input to be processed. (For example, while entering positive numbers, a sentinel value might be -1, or while entering names, a sentinel value might be done, end, or quit).
  • It allows a different number of inputs each time.
  • It allows the user to end the loop based on a specific condition, and this is also called a sentinel controlled loop or indefinite loop because the condition does not depend on a counter.
  • The number of iterations of the sentinel-controlled loop is unknown, and the user determines how often the loop is going to run and when they would like it to stop. This can be very useful in some of your programs going forward, So you must be familiar with this.

Sentinel Value Java Example/Sentinel Controlled Loop(Numbers Example)

  • In this example, I will use the while loop, which will accept a series of numbers from the user until the user enters number zero(0).
  • When number zero(0) is entered, the program should output the sum of all numbers entered.
  • The programming example is given below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

import java.util.Scanner;

publicclassSentinelValueDemo{

    publicstaticvoidmain(String[] args){

        intnumber,sum,count;

        count=0;

        sum =0;

        Scanner input=newScanner(System.in);//Creating object of Scanner class to take keyboard input

        System.out.println("Enter any number, or 0 to stop");

        number=input.nextInt();//taking input from keyboard

        //number zero(0) is the sentinel value

        while (number!=0){

            sum=sum+number;

            count++;

            System.out.println("Enter another number, or 0 to stop");

            number=input.nextInt();

        }

        System.out.println("The sum of numbers = "+sum);//Printing sum of all the numbers inputted

    }

}

  • The output of the program is given below.
What is a loop sentinel value?
Sentinel Value Java Example-fig-1
  • As you can see from the output, the loop continued to run until the number zero (0) is entered.
  • The Flowchart of the Above program is given below.
What is a loop sentinel value?
Sentinel Value Java-fig-2

String Sentinel Value Java

  • I have used number as the sentinel value in the above program, and now I will use string as the sentinel value.
  • In this example, I will use the while loop, which will accept the name of the family members until the user enters the string “end”.
  • When the string “end” is entered, the program should output the count of family members.
  • The programming example is given below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import java.util.Scanner;

publicclassSentinelValueDemo{

    publicstaticvoidmain(String[] args){

Stringname;

        intfamilyMemberCount=0;

        Scanner input=new Scanner(System.in);//Creating object of Scanner class to take keyboard input

        System.out.println("Enter family member name, or 'end' to stop");

         name=input.nextLine();//taking input from keyboard

        //string 'end' is the sentinel value

        while (name.equalsIgnoreCase("end")!=true){

            familyMemberCount++;

            System.out.println("Enter another family member name, or 'end' to stop");

            name=input.nextLine();

        }

        System.out.println("You have "+familyMemberCount+" Members in your family");//Printing family member count

    }

}

  • The output of the program is given below.
What is a loop sentinel value?
Sentinel Value Java fig-3
  • As you can see from the output, the loop continued to run until the String “end” is entered.
  • The Flowchart of the above program is given below.
What is a loop sentinel value?
Sentinel Value Java -fig-4

What is the difference between a sentinel controlled loop and a count controlled loop?

  • In the count controlled loop, we already know how many times the execution will happen, but in the sentinel controlled loop, we do not already know how many times the execution will happen.
  • The sentinel controlled loop can run for the unknown numbers of time according to the user requirements.

What is the advantage of using a sentinel controlled loop?

  • One of the most significant advantages of using a sentinel controlled loop is that there is no surety and limitation of how many times the loop will execute. It ends according to the user’s input.

What is another word for sentinel?

  • The sentinel value is also known as
    • Flag value
    • Trip value
    • Rogue value
    • Signal value
    • Dummy data

Why should the value for sentinel be chosen carefully?

  • The sentinel value should be of the same data type but distinct from regular input so that no error could occur in the program.

Download Sentinel loop Java Source Code

  • If you need the source code, then you download it from the below link.

Sentinel Value Example (Number)

Sentinel Value Example (String)

So this was all from this tutorial about Sentinel Controlled loop Java with Examples. Feel free to comment below if you have any queries regarding this post. Thank You.

People Are Also Reading…

  • How to Play Mp3 File in Java Tutorial | Simple Steps
  • Menu Driven Program in Java Using Switch Case
  • Calculator Program in Java Swing/JFrame with Source Code
  • Registration Form in Java With Database Connectivity
  • How to Create Login Form in Java Swing
  • Text to Speech in Java
  • How to Create Splash Screen in Java
  • Java Button Click Event

What is sentinel control loop?

Sentinel-controlled repetition is sometimes called indefinite repetition because it is not known in advance how many times the loop will be executed. It is a repetition procedure for solving a problem by using a sentinel value (also called a signal value, a dummy value or a flag value) to indicate "end of data entry".

Is a sentinel value used to begin a loop?

In programming, sentinel value is a special value that is used to terminate a loop. The sentinel value typically is chosen so as to not be a legitimate data value that the loop will encounter and attempt to perform with.

What is loop sentinel in Java?

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.