Here are the answers to the checkpoint questions: 1. The three expressions that appear inside the parentheses of a for loop are: A) Initialization expression B) Test expression C) Update expression 2. A) i = 0 B) i < 50 C) i += 1 D) for i in range(0, 50, 1): print("I love to program!") 3. A) 0, 2, 4, 6, 8, 10 B) 20, 18, 16 4. A while loop is best when you don't know how many times the loop needs to run up front. A for loop is best when you need to iterate a specific number of times
Introduction to for loops, their structure with initialization, testing, and updating counter variables. Reasons for use include knowing iterations count and altering conditions.
Methods to update loop counters, including incrementing and decrementing, as well as considerations for choosing the right loop type.
Explanation of nested loops, where one loop runs inside another. Emphasizes repetition in tasks and gives examples like simulating a digital clock.
Stepwise construction of a digital clock using nested for loops to track hours, minutes, and seconds, including format and AM/PM addition.
Review questions covering for loop components, practical coding scenarios, and when to use specific loop types.
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’)
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.