1Loop Basics and whileLoops9-28-2001Opening Discussion❚ What did we discuss last class?❚ One of the concepts introduced last classwas how to use recursion to createlooping logic in a program. Starting todaywe will look at the more customarymethod of having loops in programs.❚ There are three methods of implementingloops in C++. Can you name one?Other Notes❚ On assignment #2 you probably NEED todo a design. A good solution to it willinvolve multiple functions and will exercisequite a few of the things we talked aboutwith function calls, variables, andarguments. You need to THINK abouthow you will do things before you try towrite it.❚ Reading from a file in assignment #2.(You’re only going to fake it.)2while Loops❚ The simplest loop construct in C++ is thewhile loop. It has the following syntax.❚ When a while loop is reached duringexecution, the computer executes thecode inside until the condition is false atthe end of the block.❚ Let’s look at two examples.while(conditional){statements;}Conditionals in Loops❚ Conditions in while loops are booleanexpressions just like those we saw for ifstatements. In the if statements theconditional determined whether a sectionof code would be executed or not. In awhile loop it determines how many timesit will be executed.Initializing❚ Before you get to a while loop you typicallyneed to do something it initialize the state of thesystem. This generally means that you need tohave declared and put values into the variablesthat you are using in the conditional expression.❚ In a loop that counts from 0 to n-1 this wouldinvolve having n set up and declaring a countervariable that starts at 0. This can be viewed atsetting the loop preconditions.3Iterator❚ A loop also needs to do something that“advances” it to the next case it is going toprocess. Without this the state of theconditional will never change and you have aninfinite loop.❚ In a counting type loop the iterator willincrement or decrement the counter variable bysome value. Not every loop is a counter loopand other iterators could be reading from aninput source or moving through a linked datastructure.Processing❚ The code in the block of the while loopthat isn’t part of the iterator doeswhatever type of processing is intendedfor that loop.❚ Let’s look closely at what type ofprocessing was done in the two examplesthat we have been using.Loop Invariants❚ When trying to write correct loops it can be veryhelpful to isolate sets of statements that aretrue through the entire execution of the loop.The most helpful sets of statements are theones that are most closely related to what theloop should be doing.❚ Good invariants can actually allow you to doproofs of correctness where you show that aloops will terminate and will perform thecalculation you are interested in.4Minute Essay❚ Write a loop that calculates the sum ofthe squares of all the numbers between 1and n, where n is a local integer variable.❚ Next week we will continue with ourdiscussion of loops. Remember thatassignment #2 is due on Monday. I needprinted versions of your design and yourcode plus an electronic copy of the codevia e-mail. Please don’t use lpr
View Full Document