neal stublen [email protected]. loop structure sometimes it’s useful to repeat yourself the same...
TRANSCRIPT
![Page 2: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/2.jpg)
LOOPING
![Page 3: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/3.jpg)
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?
![Page 4: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/4.jpg)
Example Loops
while book is not finished
read another page
endwhile
while thirsty
drink water
endwhile
![Page 5: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/5.jpg)
Loop Control Variables
Usually, we want to continue looping until some condition is satisfied
The condition is represented by a loop control variableInitialized before entering the loopTest control variable with each iteration of
the loopTake some action in the body of the loop
that may update the loop control variable
![Page 6: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/6.jpg)
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
![Page 7: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/7.jpg)
Nested Loops
Loops can be “nested” so one loop occurs within another loop
Examples?Spreadsheet tables have rows and columnsSmartphone home screen has pages, rows,
and columns
![Page 8: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/8.jpg)
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
![Page 9: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/9.jpg)
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?
![Page 10: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/10.jpg)
Loop Inefficiencyuser = 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
![Page 11: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/11.jpg)
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?
![Page 12: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/12.jpg)
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
![Page 13: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/13.jpg)
for Loops Explained
for count = 0; count < 10; count += 2
take some action
endfor
The loop initializes the control variable.
![Page 14: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/14.jpg)
for Loops Explained
for count = 0; count < 10; count += 2
take some action
endfor
The loop checks for a terminating condition.
![Page 15: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/15.jpg)
for Loops Explained
for count = 0; count < 10; count += 2
take some action
endfor
The loop modifies the control variable with each iteration.
![Page 16: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/16.jpg)
for Loops Explained
for count = 8; count >= 0; count -= 2
take some action
endfor
The initial value, condition, and step can also go backwards.
![Page 17: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/17.jpg)
Common Loop Applications
![Page 18: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/18.jpg)
Common Loop Applications Accumulate totals
Sum numeric values in a column from a spreadsheet
Validate dataImporting 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
![Page 19: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/19.jpg)
Summary
Loop structure Loop control variables Nested loops Common loop mistakes Using for loops Common loop applications
![Page 20: Neal Stublen nstublen@jccc.edu. Loop Structure Sometimes it’s useful to repeat yourself The same actions need to occur more than once Ensures](https://reader030.vdocuments.us/reader030/viewer/2022032517/56649c7f5503460f94935f63/html5/thumbnails/20.jpg)
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.