Neal Stublen [email protected]. Loop Structure Sometimes it’s useful to repeat yourself The same...

20
PROGRAMMING FUNDAMENTALS Neal Stublen [email protected]

Transcript of Neal Stublen [email protected]. Loop Structure Sometimes it’s useful to repeat yourself The same...

Page 1: Neal Stublen nstublen@jccc.edu. Loop Structure  Sometimes it’s useful to repeat yourself  The same actions need to occur more than once  Ensures.

PROGRAMMING FUNDAMENTALS

Neal Stublen

[email protected]

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.