Presentation is loading. Please wait.

Presentation is loading. Please wait.

Neal Stublen Loop Structure  Sometimes it’s useful to repeat yourself  The same actions need to occur more than once  Ensures.

Similar presentations


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:

1 Neal Stublen nstublen@jccc.edu

2

3 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?

4 Example Loops while book is not finished read another page endwhile while thirsty drink water endwhile

5 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

6 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

7 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

8 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

9 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?

10 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

11 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?

12 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

13 for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop initializes the control variable.

14 for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop checks for a terminating condition.

15 for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop modifies the control variable with each iteration.

16 for Loops Explained for count = 8; count >= 0; count -= 2 take some action endfor The initial value, condition, and step can also go backwards.

17 Common Loop Applications

18  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

19 Summary  Loop structure  Loop control variables  Nested loops  Common loop mistakes  Using for loops  Common loop applications

20 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.


Download ppt "Neal Stublen Loop Structure  Sometimes it’s useful to repeat yourself  The same actions need to occur more than once  Ensures."

Similar presentations


Ads by Google