Transcript for:
Programming Logic and Design: Sentinel Value

welcome back to programming logic and design in this section we're going to be discussing the sentiment valley which we use to end a program all right using a sentinel value two in the program to understand this statement you need to understand what it is to make a decision when we make a decision we are testing a value whether it is a yes or whether it is a no and usually we use it in a decision symbol and how a decision symbol looks is a diamond shape now we use a dummy value we use a dummy value that somebody that your user will not need to be using so it will be something stupid like zero you can't go zero multiplied by two because you know what the odds it's going to be it's going to be zero so I don't mean a dummy meaning a pacifier something that goes into your mouth I mean something that makes you stupid okay so a stupid value we call the dummy value our sentinel varium now we can also use an end-of-file within our decision here so instead of my number we will use e of f so end of file we use end of file when we have an input has been passed the end of file we do not want to runtime errors so we're using a decision here to create to make sure that we do not get a runtime error so basically our end of file is a mark at the end of a file that automatically acts as a sentinel so our blank line at the end of the file here will be our sentinel value let's walk through figure 1.9 yem figure 1.9 says we're going to start our program and we're going to input a number we're going to input let's say we input two so we input two two is now been compared to zero is two equal to zero yes or no no it is not then we're going to set my answer equal to two x 2 which is 4 and output form so in our upward screen if I had to draw my output screen as such it will be four will be outputted let's do another example let's say we input three three is my new number it overrides two in other words it replaces two in a box of memory three is now being compared to zero sorry three is now being compared to zero is three equal to zero no it is not so we're going to go three multiplied by two is six six is now being stored in my little box of memory and what is going to be output it will be six let's try zero zero is now being replaced is now replacing my number zero is now being compared to zero is zero equal to zero yes it is then we end our program now why are we using a sentinel value here we are using a sentinel value so we can stop this beautiful loop if we don't have a sentinel value this will just go on and on and on ten thousand ten million ten trillion times it will not stop any way to stop will be to exit the program and then the program crashes so we don't want that we try and try and create programs that don't crash so that is why we creating a dummy value yeah a sentinel value let's have a look at our end of file here let's say our file consists of the numbers 1 2 3 0 1 2 3 and then it's nothing after that let's input the first line which will be 0 0 is 0 into file yes or no no it's not then we're going to go 0 multiplied by 2 which is going to be storing 0 we will output 0 so in my output screen we're going to have 0 then one is now going to be my new number is one in the file no it's not they were going to go 1 x 2 which is now 2 2 is my new my answer and then we can output 2 we're going to go 3 is the next one that's going to come read from the file 3 is 3 and a foul no it's not 3 is going to be 3 x 2 which is 6 6 is my new answer and therefore we going to output 6 then it's going to loop again looking looking looking looking looking looking for something else over here but there is nothing here it's a blank space that blank space is the my number now a blank space yes it is so therefore we are going to stop the program and it's going to end beautifully sometimes you can stop your programs with a beautiful message which we'll learn later on but just for now I just want you guys to get the idea that a sentinel value is much needed to end our loops please join me for the next video where we were discussing programming and user environments