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
Sentinel Value Java | Sentinel loop Java | Sentinel Controlled loop Java
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/Java 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 of using a sentinel value to control a while loop in Java is given below (Numbers Example).
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; public class SentinelValueDemo { public static void main(String[] args) { int number, sum, count; count = 0; sum = 0; Scanner input = new Scanner(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.
- 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.
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 of using a sentinel value to control a while loop in Java is given below (String Example).
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; public class SentinelValueDemo { public static void main(String[] args) { String name; int familyMemberCount=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.
- 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 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
- Best Laptops for Java Programming
- How to Connect MySQL Database in Java using Eclipse
- How to Connect MySQL Database in Java using NetBeans
- Core Java Interview Questions and AnswersÂ
- Best Websites to learn Java Online for FREE