For Loops and Nesting Looping Structures
For Loops  For loops are a pre-test loop  In order to utilize a for loop you need 3 things: 1. Needs to initialize a counter 2. Must test the counter variable 3. It must update the counter variable  Code: for initialization in range(start, stop, increment): statement1 statement2
Example for i in range(0, 5, 1): print(“Hello”) Step 1: Perform the initialization expression Step 2: Evaluate the test expressions Step 3: Execute the body of the loop Step 4: Perform the update Assign 0 to i i < 5 Update iPrint “Hello” True False
Reasons to use a for loop  Know the number of times the loop should iterate  Using a counter  Need a false condition to terminate the loop
Demonstrating when to use a for loop num = 1; while ( num <= 10): print( num, “tt”, num * num) num+=1; for i in range(1, 10, 1): print( num, “tt”, num * num) Initialization expression Initialization expression Test Update UpdateTest
Other Ways to Update  Most programmers and books refer to the update expression as the incrementor  Not only can you increment the expression but decrement as well  Additionally you can increase and decrease the variable by values other than 1  i++ i+=1 i = i + 1  i-- i -=1 i = i - 1  i+=2  i -=5
Determining Which Loop to Choose  Each of the 3 loops is ideal to use in different situations:  The while loop. The while loop is a pre-test conditional loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. For example, checking health in a game until someone dies  The for loop. The for loop is a pretest loop that has built in expressions for initializing, testing and updating a counter. This makes it convenient to control the number of times the loop executes. For
Nested Loops  A nested loop is a loop inside the body of another loop  Inner (inside), outer (outside) loops: for row in range(1, 3, 1) //outer for col in range(1, 3, 1)//inner print(row * col)  Inner loop goes through all repetitions for each repetition of outer loop  Inner loop repetitions complete sooner than outer loop  Total number of repetitions for inner loop is product of number of repetitions of the two loops.  Can nest different styles of loops together
Nesting Loops  Nested loops are necessary when a task is performs a repetitive operation and that task itself must be repeated.  for initialize in range(start, stop, update): while(condition): for initialize in range(start, stop, update): while(condition):  Just like when we worked with nested conditionals  if (condition): if (condition):  For example, a digital clock is a good example of needing to nest for loops.
Digital Clock  How can we use loops to simulate a digital clock?  What would each loop be responsible for keeping track of?  There would be 3 loops:  One would track the hour  One would track the minutes  One would track the seconds  NOT AN ANALOG CLOCK, why??????  What would it look like?
Digital Clock  First let’s begin by creating a loop to display the seconds up to a minute.  Can anyone describe that loop??? for sec in range(0, 60, 1): print(format(sec, ‘>2’)  Question: What is the initialization, test and update for the previous for loop?  Question: What would it look like if we printed to the left with setw(2)?
Digital Clock  Now let’s add minutes by nesting our previous loop: for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(min, ‘>2’), ”:”, format(sec, ‘>2’)
Digital Clock  Write out the digital clock code to print the hour as well
Digital Clock for hour in range(0, 12, 1): for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(hour, ‘>2’), ”:”, format(min, ‘>2’), ”:”, format(sec, ‘>2’)  Question: What if we wanted to add AM/PM
Digital Clock Assign 0 to hour hour < 12 Assign 0 to sec Assign 0 to min True False min < 60 False True sec < 60 Fals True Print clock
Checkpoint: 1. Name the three expressions that appear inside the parentheses(header) of the for loop. 2. You want to write a for loop that displays “I love to program!” 50 times. A. What initialization expression will you use? B. What test expression will you use? C. What update expression will you use? D. Write the loop 3. What will the following code segments produce: A. for count in range(0, 6, 1): print(count + count) A. for j in range(20, 14, -2): print(j) 4. When should I use each type of loop?

For Loops and Nesting in Python

  • 1.
    For Loops andNesting Looping Structures
  • 2.
    For Loops  Forloops are a pre-test loop  In order to utilize a for loop you need 3 things: 1. Needs to initialize a counter 2. Must test the counter variable 3. It must update the counter variable  Code: for initialization in range(start, stop, increment): statement1 statement2
  • 3.
    Example for i inrange(0, 5, 1): print(“Hello”) Step 1: Perform the initialization expression Step 2: Evaluate the test expressions Step 3: Execute the body of the loop Step 4: Perform the update Assign 0 to i i < 5 Update iPrint “Hello” True False
  • 4.
    Reasons to usea for loop  Know the number of times the loop should iterate  Using a counter  Need a false condition to terminate the loop
  • 5.
    Demonstrating when touse a for loop num = 1; while ( num <= 10): print( num, “tt”, num * num) num+=1; for i in range(1, 10, 1): print( num, “tt”, num * num) Initialization expression Initialization expression Test Update UpdateTest
  • 6.
    Other Ways toUpdate  Most programmers and books refer to the update expression as the incrementor  Not only can you increment the expression but decrement as well  Additionally you can increase and decrease the variable by values other than 1  i++ i+=1 i = i + 1  i-- i -=1 i = i - 1  i+=2  i -=5
  • 7.
    Determining Which Loopto Choose  Each of the 3 loops is ideal to use in different situations:  The while loop. The while loop is a pre-test conditional loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. For example, checking health in a game until someone dies  The for loop. The for loop is a pretest loop that has built in expressions for initializing, testing and updating a counter. This makes it convenient to control the number of times the loop executes. For
  • 8.
    Nested Loops  Anested loop is a loop inside the body of another loop  Inner (inside), outer (outside) loops: for row in range(1, 3, 1) //outer for col in range(1, 3, 1)//inner print(row * col)  Inner loop goes through all repetitions for each repetition of outer loop  Inner loop repetitions complete sooner than outer loop  Total number of repetitions for inner loop is product of number of repetitions of the two loops.  Can nest different styles of loops together
  • 9.
    Nesting Loops  Nestedloops are necessary when a task is performs a repetitive operation and that task itself must be repeated.  for initialize in range(start, stop, update): while(condition): for initialize in range(start, stop, update): while(condition):  Just like when we worked with nested conditionals  if (condition): if (condition):  For example, a digital clock is a good example of needing to nest for loops.
  • 10.
    Digital Clock  Howcan we use loops to simulate a digital clock?  What would each loop be responsible for keeping track of?  There would be 3 loops:  One would track the hour  One would track the minutes  One would track the seconds  NOT AN ANALOG CLOCK, why??????  What would it look like?
  • 11.
    Digital Clock  Firstlet’s begin by creating a loop to display the seconds up to a minute.  Can anyone describe that loop??? for sec in range(0, 60, 1): print(format(sec, ‘>2’)  Question: What is the initialization, test and update for the previous for loop?  Question: What would it look like if we printed to the left with setw(2)?
  • 12.
    Digital Clock  Nowlet’s add minutes by nesting our previous loop: for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(min, ‘>2’), ”:”, format(sec, ‘>2’)
  • 13.
    Digital Clock  Writeout the digital clock code to print the hour as well
  • 14.
    Digital Clock for hourin range(0, 12, 1): for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(hour, ‘>2’), ”:”, format(min, ‘>2’), ”:”, format(sec, ‘>2’)  Question: What if we wanted to add AM/PM
  • 15.
    Digital Clock Assign 0to hour hour < 12 Assign 0 to sec Assign 0 to min True False min < 60 False True sec < 60 Fals True Print clock
  • 16.
    Checkpoint: 1. Name thethree expressions that appear inside the parentheses(header) of the for loop. 2. You want to write a for loop that displays “I love to program!” 50 times. A. What initialization expression will you use? B. What test expression will you use? C. What update expression will you use? D. Write the loop 3. What will the following code segments produce: A. for count in range(0, 6, 1): print(count + count) A. for j in range(20, 14, -2): print(j) 4. When should I use each type of loop?

Editor's Notes

  • #9 Think of it as searching a hotel. The outer loop operates the elevator and the inner loop searches the hotel rooms on each floor. Each loop controls a different aspect of the program.