welcome back to program logic and design in this section we're going to be discussing common Loop applications Loops are frequently used to accumulate totals and validate data some examples will be business reports that often include totals list of real estates sold and the total value of that you could obtain a total for a variable by going back through the entire set of data a second time but it would be inefficient to do so what we can do is we can build a running total as we go in our Loop using a cumulative variable instead this kind of gathers your values or your totals it's similar to a counter but a counter increments by one a accumulator increments by some value now there are three actions that an accumulator requires first thing it needs to be initialized to zero accumulators altered once for every data set processed so in other words every new Total is that height altered at the end of processing accumulators have to be outputed because why you why are you creating accumulator in the first place so in doing so we create a summary report or a summary section on a report and it contains only totals with no detailed data Loops are processed but detail information is not printed in your summary report so here is an an example of a monthin sales report we given the addresses and then just simply the prices and you can see our accumulator which is the 1,1 2,500 that will be your accumulator that variable that was used in your Loop to accumulate the total of all five of those addresses how did we get the accumulator well let's have a look where was the accumulator variable our accumulator variable was placed within our create report method here now create report method is going to be our detailed loop as such it is placed within our address do not quit Loop so what we do is we enter the address and in this case 150,000 and we take 150,000 is added to our accumulated price and what is our accumulated price well we initialized it right at the beginning to be zero so we add the 150,000 plus accumulated price and we add it into we create kind of our new accumulated price so when we Loop a second time adding in our next address which is 12 Maple Avenue and he's got 310,000 310 for our price which we inputed is added to our old accumul ctive price which we know is 150,000 those two are added and that is added to a cumulative price let's play computer so we input the address we input the 150,000 and what we do is we add our accumulative price which is zero plus our new price which is 150,000 and we get 150,000 which is going to be stored into our cumulative price variable so what we do is what is stored in a cumulative price already which is 150,000 that is added to our 1 310,000 and we are storing that new price into accumulative okay and then we do the same process for the next one for 8723 Maria Lane and we do do the same for a corn Street and we do the same for bahay then we are going to tap in zzz which gives us a grand total for our cumulative price of 1 02500 you can see in our finish up here module we are putting the total is a cative price so here is our total you can see the total there and we just output finish up you can see with in create report here is our address and price outputed within create report and that is why we are getting our Acorn Street price Maple Avenue price Mar Lane price the corn street price baham way price that is what our total is in our end of tasks module looking at the suit code of our real estate sales report program you'll see the initialization here in our declarations and our create report is our Loop report and our cumulative price is within that Loop so as we keep looping looping looping we keep adding the new price to the old price or the accum old accumulative price and those two are being stored into our cumulative price variable we storing it into there replacing the contents of that accumulated price variable and in our finish up module you'll see that total is being outputed under the accumulative price V variable so that is what we are outputting as our total we can use a loop to validate data to ensure that it falls within an acceptable range now programmers are often called in the middle of the night to fix bugs in their programs some of those bugs could have been avoided if defensive programming techniques had been implemented so defensive programming is preparing for all possible errors before they actually occur Ur when prompting a user for data no guarantee that data is valid validate data Mak sure data Falls in acceptable ranges month values between 1 and 12 Etc gigo garbage in garbage outs which means unvalidated input will result in ous output so for example you make sure that if month is less than low month or month is greater than high month then output end birth month input month so in other words what we're doing is that if they give a less than in other words a zero or a minus one or they type in minus two by accident you are going to give them another prompt or give them another opportunity to enter the birth month if they give greater than 12 in other words 13 by accident then you're giving them another opportunity to enter the birth month again but as you can see this is an F statement so in other words if they enter this let's say they enter 13 and it's incorrect you're not giving them another opportunity so this is where a loop will come in as an aid in validating data here you can see that we've got our Loop we enter in we'll give them countless opportunities to enter in the correct month now we've just shown that we can reprompt Loop there is an importance of limiting the number of iterations of a repr prompting Loop so a programmer can choose the option of forcing a value rather than repeating a romping Loop too many times the reason we do this is that romping can be frustrating to a user if it continues indefinitely maintain count of the number of repts you'll probably see that sometimes you get three chances are or three reprompt in order for the user to input the correct data and if they don't then you you kind of force the data items which means you override the incorrect data by setting the variable to specific value like zero or one Etc to make a round off as such now if you see here we given the temps three and we got a count within the loop here so we're counting how many times he has iterated and if it's exceeded those amounts or exceeded his three chances then we going to exit the loop have a look at our condition here count is less than attempts and and you can see the brackets which they're using here the parentheses they're saying that that plus this needs to be true together so here you can see the combination of using and and all within our comparisons and how effective it is in this tested condition here for the loop to keep repeating itself through three times another common Loop application is for validating a data type now I don't know how many times you have in your programs created a program where they keep entering in a string when they should be entering a number so in validating data typ is that we're going to be creating a loop for them to input the correct data type so validating data requires a variety of methods is numeric or similar method this is provided with language translator you use to write your programs it's kind of your black box so is CH or is whitespace or other two modules that you can use or methods you can use within your program to check that they haven't used any spaces that's what white space is or if it is a character they've entered in so if they have to type in yes or no just a simple y or a simple n for is chart you just check that it is a character in order to seed if it's not then you give them another chance to entering the correct character you are accepting user data as strings and what is quite nice is that you use built-in methods to convert to correct data types too which is Handy here is an example of using is numeric for our salary so they enter the salary and we just check if it's numeric if it's not a numeric then we give them a chance to try again and and we give give them a chance to try again and again and again number of times you should see that we should have given them a count here so in order for them to give only get three chances else we force the data type or we force a salary to be zero for instance now another com Loop application is the importance of Performing validation whenever possible to avoid the use of invalid and inconsistent data so many data items can be checked for reasonableness in conclusion use your Loops to help validate your work and help create reasonableness and consistency for your data this will make your programs user friendly please join me in the next video in which I'll be discussing comparing selections and loops