Presentation on theme: "Neal Stublen Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures."— Presentation transcript:
Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures that each repetition is done in exactly the same way Examples?
Example Loops while book is not finished read another page endwhile while thirsty drink water endwhile
Loop Control Variables Usually, we want to continue looping until some condition is satisfied The condition is represented by a loop control variable Initialized before entering the loop Test control variable with each iteration of the loop Take some action in the body of the loop that may update the loop control variable
Example Loops // Definite loop – fixed number of iterations num pagesLeft = 100 while pagesLeft > 0 read another page pagesLeft = pagesLeft - 1 endwhile // Indefinite loop – unknown number of iterations while sick take medicine sick = is patient feverish? endwhile
Nested Loops Loops can be “nested” so one loop occurs within another loop Examples? Spreadsheet tables have rows and columns Smartphone home screen has pages, rows, and columns
Example Nested Loops num reports = number of reports while reports > 0 num pagesLeft = number of pages in report while pagesLeft > 0 print another page pagesLeft = pagesLeft - 1 endwhile reports = reports - 1 endwhile
Common Loop Mistakes Infinite loops The condition that terminates the loop is missing or incorrect Failure to initialize the control variable? Failure to update the control variable? Failure to check the control variable correctly?
Loop Inefficiency user = 1 while user <= lookup number of users print “User “ + user + " of “ + lookup number of users print user information endwhile user = 1 totalUsers = lookup number of users while user <= totalUsers print “User “ + user + " of “ + totalUsers print user information endwhile
Thermostat Logic What logic would we need to implement a thermostat that controls a home furnace? What if we also wanted to control the air conditioner?
Using for Loops count = 0 while count < 10 take some action count = count + 2 endwhile for count = 0; count < 10; count += 2 take some action endfor
for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop initializes the control variable.
for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop checks for a terminating condition.
for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop modifies the control variable with each iteration.
for Loops Explained for count = 8; count >= 0; count -= 2 take some action endfor The initial value, condition, and step can also go backwards.
Common Loop Applications
Accumulate totals Sum numeric values in a column from a spreadsheet Validate data Importing records from a file, values can be validated to make sure they fit within an expected range Keep prompting for the same user input if it was not valid the first time
Summary Loop structure Loop control variables Nested loops Common loop mistakes Using for loops Common loop applications
Guess a Number What logic is necessary to have a user guess a random number between 1 and 100? After each guess, the user will be told if his guess was correct, too high, or too low. After seven guesses, the user has failed at the task.