welcome back to program and logic and design in this section we're going to be discussing using a loop control variable in our Loop structures using a loop control variable helps us create use a condition to remain true so the statements in your well Loops body just keeps executing so your Loop control variable will control the number of repetitions your Loop will execute the Loop's body in order for us to control the number of repetitions there is a couple of things that need to be done we need to initialize your Loop control variable before entering your while loop then you need to test that Loop control variable and that will be done in your condition of your well Loop and finally your body of your Loop must alter that value of the loop control variable now our repetitions are controlled by two things our counter which is used to create a definite control counter controlled Loop or a sentinal value which is used to create an indefinite Loop so in other words the user and determine how many times the Loop's going to execute students who are new to programming often struggle to understand how to construct Loops that is why I spend so much time on flowchart diagrams and walking through some Co code samples it's a good investment of your time to work on your flowchart diagrams and work on walking through your own code to know what is actually happening Des check your code need I remind you what an infinite Loop is an infinite Loop is when your program keeps going on and on and on and on you need to avoid this and to avoid it there are certain strategies in how you construct your Loop structures initializing testing altering which will help you not get those infinite Loops remember those infinite Loops make your programs lag so what is a definite Loop a definite Loop executes a predetermined number of times and a counter controlled Loop program counts Loop repetitions so therefore your definite Loop and counter controlled Loop means the same things because the program is counting the loop repetitions it's counting to a certain point which is predetermined number of times that is why a counter controlled Loop is a definite Loop remember Loop controlled variables are altered by incrementing or decrementing so we add one for incrementing or we decrement by one so how do we alter our Loop control variable we take the its previous number which has been stored into count and we add one to it and we store all of that into the left hand side count so as we keep looping we take the previous amount we add one to the previous amount and we store the new amount now into the variable count so if I had to give a definition to counter a counter is any numeric value that counts the number of times an event has occurred and it usually starts with z Z so if you have a look at this example here our num count here has been initialized to zero in order for me to explain how this program works and how it outputs hello four times I need to play computer you can see that num count equal to Zer we are initializing count to be zero so we starting count to be zero then we're going to evaluate in line four and line four says is count less than four yes it is then in line five we arep put hello then in line six we increment our previous count which was Zero we got 0 + 1 is 1 so now the new count is one then we revert back to line four we test our condition is 1 less than four yes it is we output hello then we increment our count so we go our previous count which is 1 + 1 is two so count is now two we evaluate our new count which is two is less than four yes it is we output hello we increment our previous count so 2 + 1 is three our new count is three we evaluate three to less than four yes it is we output hello we increment 1 so 3 + 1 is 4 we we now have a new count which is four we now evaluate four in line four again is four less than four no it's not it's false so we go straight to line eight so we we hop from line four to line eight and we output goodbye so looking at the flow chart we have we get into our condition here we ask the question if that is yes we up with hello and we alter our count variable so we alter it and we keep looping and keep looping until we reach four and we output hello four times this is our Loop control that is tested within this decision here and once it reaches four we exit our decision we exit our Loop and we say goodbye this is an example of a definite Loop so in other words it's definitely going to Output hello four times so when count is 0 1 2 or three it's going to Output hello four times what about an indefinite Loop an indefinite Loop has a sentinel value so it's performed a different number of times each time the program executes so in other words we are giving the user an opportunity to exit the program here in this case should I continue press y or press n if you press y we output hello do you want to continue yes or no yes so the loop control variable is altered here we loop again if they press yes and here we're going to alter the condition here the loop control variable here if we say n then we have changed it therefore we tested that it's not yes it is n and we output goodbye our Loop control variable is initialized by the user hence the user decides how many times the loop will execute this is two typical executions of the program which we just discussed previously in two different environments here is our Command Prompt and then here says our gooey Hello hello hello if they press yes yes yes yes all the way to here they press yes one two three times and then pressed n to say goodbye three steps should occur in every properly functioning Loop first one provide a starting value for a variable that will control the loop if you start at zero and you want to Output four hellos your test Loop control variable which determines how many times the loop body executes must then be three remember if you start at zero it's going to end at less than four alter the control Loop variable within the loop so this will allow you to change the loop control variable to help with the testing so if we Loop once you have kind of created evidence that you have done once you've counted it that's why you are altering it here is is an example of a payroll program showing how the loop control variable is used we have our Sentinel value which is name here name is going to be inputed as it's going to be initialized in our housekeeping module and that name is going to be used to quit remember this is going to be our indefinite Loop in other words it is going to loop around over and over and over and over again until the user types in Triplex remember every program needs a beautiful heading so therefore we've got our report heading and our column heading under housekeeping within our detailed Loop this is where we are going to alter our name so name will be the next set of data that will be inputed for name and name will then be tested to whether it is quit triple X and if it is not it will carry on looping until the user enter is Tri X that concludes this section please join me in the next video in which I'll be discussing nested loops