1 / 5

For loops in programming

For loops in programming. Assumes you have seen assignment statements and print statements. Why do we want a loop in a program?. Many programs need to process huge amounts of data Each piece of data needs to go through the same instructions

Download Presentation

For loops in programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. For loops in programming Assumes you have seen assignment statements and print statements

  2. Why do we want a loop in a program? • Many programs need to process huge amounts of data • Each piece of data needs to go through the same instructions • One option is to write out the instructions over and over again • That’s not very practical or flexible • What if I wrote a program to process invoices for 100 customers and I got some more customers? Rewrite the program?? No! • A loop lets me specify what needs to be repeated and how many times it needs to be repeated

  3. Two kinds of loops • Definite • You know that you want to execute the loop a certain number of times • “there are 10 students’ grades” • “Draw a standard chess board” (always 8 x 8 squares) • “Quiz the user about the capitals of the 50 states” • “do this for every character in the string” • “do this for every item in the list” • “do this for every line in the file” • Indefinite • You don’t know how many times you want to execute the loop but you know there is a condition that is the stopping point • “do this until the user wants to stop” • “let the user enter numbers until they type -1” • “do this until the user wins the game” • “repeat this until this number reaches a certain level”

  4. For loop – a definite loop • The syntax of the for loop statement for variable in some list of values to assign to the variable : (colon!) Follow this line by all statements that should be repeated with an extra indent - This is how the interpreter knows what is in the loop and what’s not • The semantics of the for loop statement • At the start of the loop check to see if the loop should execute • If not, skip the body and continue with next statement after loop • If it should execute, Assign variable with the first item in the list Execute the body • Repeat the test at the top of the loop to see if loop should execute again

  5. Controlling the loop • You don’t want a loop that doesn’t stop! • Python uses several ways to control a loop • The range function (gives integers) • The contents of a list of values (gives the elements whatever type) • The contents of a string (gives characters) • The contents of a file (gives strings)

More Related