Iteration: repeating instructions with loops
边玩边学
回答这些题目来赚取能量,然后钓鱼探索。无需账号。
课程笔记
What is Iteration?
- Iteration means repeating a set of instructions (a block of code) more than once.
- It is one of the three basic building blocks of programming, alongside sequence (doing things in order) and selection (making choices).
- Loops let you write the same code once and run it many times, saving time and reducing errors.
- Each repetition of the loop is called an iteration.
A loop repeats while a condition holds, or for a fixed number of repetitions.

Count-Controlled Loops (for loops)
- A count-controlled loop (often a for loop) repeats a fixed number of times.
- It uses a loop variable (also called a counter) that changes each time the loop runs.
- The loop variable often starts at a value, continues while a condition is true, and increases or decreases by a step.
- Example: `for i in range(5):` runs the indented code 5 times, with `i` taking values 0, 1, 2, 3, 4.
- Use a for loop when you know exactly how many times you want to repeat.
A count-controlled loop runs a fixed number of times, here outputting the numbers 1 to 5.

Condition-Controlled Loops (while loops)
- A condition-controlled loop (often a while loop) repeats as long as a condition is true.
- The condition is checked before each repetition; if it is false at the start, the loop body never runs.
- Example: `while x < 10:` repeats until `x` becomes 10 or more.
- Use a while loop when you do not know in advance how many times the loop will run.
- The loop must eventually make the condition false, or it will run forever.
A flag controls the loop: it keeps asking until the correct password is entered.

Loop Variables and Updating
- The loop variable is a variable that changes each iteration, often used to control the loop or to track progress.
- In a for loop, the loop variable is automatically updated by the loop.
- In a while loop, you must update the variable yourself inside the loop body.
- Common update: `counter = counter + 1` (or `counter += 1`) to increase the counter by 1.
- The loop variable can be used inside the loop, for example to calculate sums or to index into a list.
Infinite Loops and How to Avoid Them
- An infinite loop is a loop that never ends because the condition never becomes false.
- Infinite loops can freeze a program or cause it to run forever.
- To avoid them, make sure the condition can become false (e.g., update the loop variable).
- Check that the loop variable moves towards the stopping condition (e.g., increases if the condition is `<`).
- Test your loop with small values to see if it behaves as expected.
A loop boundary that is too small misses the last item; one that is too large processes extra items.

Choosing the Right Loop
- Use a for loop when the number of repetitions is known in advance (e.g., repeat 10 times, or iterate over a list).
- Use a while loop when the number of repetitions depends on a condition that may change (e.g., keep asking until the user enters a valid answer).
- For loops are often clearer and less error-prone for fixed counts.
- While loops are more flexible for unknown numbers of repetitions.
- Sometimes a for loop can be written as a while loop, and vice versa, but one is usually more natural.
Combining Loops with Selection
- You can put an if statement inside a loop to make decisions during each iteration.
- This lets you process each item differently based on its value.
- Example: `for num in numbers: if num % 2 == 0: print(num)` prints only even numbers.
- You can also put a loop inside an if statement, or nest loops inside loops (nested loops).
- Combining loops and selection allows you to solve complex problems like searching, counting, and filtering.
Common Loop Patterns
- Counting: use a loop to count how many times something happens.
- Summing: accumulate a total by adding each value in a loop.
- Searching: loop through items to find a specific one.
- Input validation: use a while loop to keep asking for input until it is valid.
- Nested loops: a loop inside another loop, often used for grids or tables.
Key Terminology
- Iteration: one repetition of a loop.
- Loop body: the block of code that is repeated.
- Loop condition: the boolean expression that controls a while loop.
- Loop variable: the variable that changes each iteration.
- Infinite loop: a loop that never terminates.
幻灯片
练习题
免费预览——61 题中的 8 题。注册即可查看全部。
1.What is the term for the control flow that repeats a block of instructions a set number of times?
Easy- Acount-controlled loop
- Bcondition-controlled loop
- Cinfinite loop
- Dselection
2.A 'while' loop is an example of a count-controlled loop.
EasyTrue or false?
3.Which of the following is a typical use of a count-controlled (for) loop?
Easy- ARepeating until the user enters 'quit'
- BPrinting numbers 1 to 10
- CChecking a password until correct
- DWaiting for a button press
4.What is the main danger of a condition-controlled loop if the condition never becomes false?
Easy- AIt runs too slowly
- BIt becomes an infinite loop
- CIt skips the block
- DIt uses too much memory
5.Which of the following are examples of iteration? (Select all that apply)
Easy- Afor loop
- Bwhile loop
- Cif statement
- Drepeat-until loop
6.In a 'for' loop, the loop variable automatically increases (or decreases) each time the loop runs.
EasyTrue or false?
7.Which loop would be most appropriate to keep asking for a password until the correct one is entered?
Medium- Afor loop
- Bwhile loop
- Crepeat-until loop
- Dboth while and repeat-until
8.What is the value of 'total' after this code runs? total = 0; for i in range(1, 4): total = total + i
Medium- A3
- B6
- C10
- D4