Iteration
MIT 6.100L Intro to CS and Programming using Python, lecture 3, explained in plain English
Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.
Learn this lecture freeWatch or read the original lecture: MIT OpenCourseWare.
Part 1
This part explains how if-statements pick which code to run, why nested ifs get messy, and how a while loop repeats code until a condition becomes false.
Key ideas
- Branching uses if, elif, and else to choose which code runs. Indentation, the spaces at the start of a line, tells Python which lines belong to each block, and it is required.
- In an if, elif, else chain, Python runs only the first block whose condition is true, even if more than one is true. If none are true, the else block runs.
- Nesting if statements works for a fixed number of steps, but it breaks down when you do not know how many times the user will repeat an action.
- A while loop repeats an indented block as long as its condition is true. After each pass, Python rechecks the condition on its own, and the loop stops when the condition becomes false.
Words to know
- Iteration
- Repeating the same steps over and over, as long as a condition says to keep going.
- Indentation
- The spaces at the start of a line. In Python, they show which lines belong together as one group.
- Boolean
- A value that is either true or false. Conditions in if and while statements are Booleans.
- Code block
- A group of lines that belong together, marked by indentation under an if, elif, else, or while line.
Example
Think of a Netflix show with many episodes. Netflix keeps playing the next episode while there are more to watch, and it stops when the list is empty. A while loop works the same way: it keeps running the indented steps while the condition is true.
3 checked practice questions for this part are in the app.
Part 2
This part shows how a while loop repeats code as long as a condition stays true, and what goes wrong when the loop never stops.
Key ideas
- A while loop repeats the indented code under it again and again, as long as its condition is true. When the condition becomes false, the program goes on to the lines after the loop.
- The loop only ends if something inside it changes the condition. If nothing changes, the loop can run forever.
- String comparison is case sensitive, so 'RIGHT' and 'Right' are not the same as 'right'. The program treats them as different answers.
- When the loop counts down with n = n - 1, each pass makes n smaller. Eventually the condition n > 0 becomes false, and the loop stops.
- If you forget the line that changes the variable, the loop never ends. You can stop it by pressing Control-C (Command-C on a Mac) or clicking the red X.
Words to know
- condition
- A yes-or-no question the program checks, such as where == 'right' or n > 0. The loop keeps going while the answer is yes.
- infinite loop
- A loop that never stops running, because its condition never becomes false.
- case sensitive
- Treats capital letters and small letters as different. 'Right' and 'right' do not match.
- decrement
- To make a number one smaller, such as changing n to n minus 1.
Example
Think of a coffee shop that keeps making drinks while the line of customers is not empty. Each drink served shortens the line, so the shop eventually closes. If nobody ever leaves the line, the shop keeps working forever, just like an infinite loop.
3 checked practice questions for this part are in the app.
Part 3 · 20:00–30:00
This part shows how to use a counter variable to track how many times a while loop has repeated, and how the same counting pattern works in a factorial program.
Key ideas
- A variable is a named box that holds a value. You can make a new variable, such as n, to count how many times the loop has run, starting from 0. 20:20
- Each time through the loop, you add 1 to the counter with n = n + 1. Printing n lets you check that it is counting correctly. 21:46
- Once the counter works, you can use an if statement to check it. In this program, when n is greater than 2, the program prints a sad face. 22:31
- When a while loop counts through numbers, you set a loop variable before the loop, test it in the loop's condition, and change it inside the loop. If you never change it, the condition stays true and the loop never ends. 24:07
- In the factorial example, i is the loop variable and factorial is a running product. Each time through, the program multiplies factorial by i, then adds 1 to i. The for loop comes next as a shorter way to write this pattern. 29:37
Words to know
- variable
- A named place in the program that holds a value, which can change while the program runs.
- increment
- To go up by 1. In this lesson, i += 1 is shorthand for i = i + 1, which means adding 1 to i.
- loop variable
- The variable that controls a loop. You set it before the loop, check it in the loop's condition, and change it inside the loop.
- factorial
- In this example, 4 factorial is 1 times 2 times 3 times 4, which equals 24. The program builds this answer one multiplication at a time.
Example
Think of counting laps on a running track with a tally on a notepad. You start the tally at zero, and each time you finish a lap you add one mark. The tally tells you how many laps you have run, just as the counter variable n tells the program how many times the loop has repeated.
3 checked practice questions for this part are in the app.
Part 4 · 30:00–40:00
A for loop is a shortcut for running the same lines of code once for each value in a list of values, and the lesson shows how range() makes those lists of numbers.
Key ideas
- A while loop needs you to set up a variable, check a condition, and change the variable each time. It works, but it is easy to forget the last step, and then the loop never stops. 30:44
- A for loop is the better choice when you have a sequence of values to go through. Python handles the moving from one value to the next for you. 31:05
- The loop variable takes on each value in the sequence in turn. The indented lines run once for each value, and then the loop ends. 31:51
- range(5) gives the values 0, 1, 2, 3, and 4. It starts at 0 and stops before the number you give it, so the stop number is never included. 33:19
- A loop can keep a running total. The sum example adds each value of i to mysum, and by the end mysum holds the total of all the values. 38:13
Words to know
- infinite loop
- A loop that never stops because its stopping condition never becomes false.
- iterate
- To go through a list of values one at a time, doing something with each one.
- range
- A way to make a list of numbers that follow a pattern, such as counting up from 0.
- indentation
- The spaces at the start of a line that show which lines belong inside the loop.
Example
Think of a streaming service that, without any interaction from you, plays a set sequence of four episodes and then stops to ask if you are still watching. A for loop works the same way: it goes through a fixed list of items, one after another, and then it is done. You do not have to keep track of which episode is next yourself.
3 checked practice questions for this part are in the app.
Part 5 · 40:00–46:27
This part shows how to fix a loop that gives the wrong answer, what the += shorthand means, and when to use a for loop instead of a while loop.
Key ideas
- The += symbol is shorthand. Writing mysum += i means take the current value of mysum, add i to it, and save the result back into mysum. 40:52
- When a program gives an answer you did not expect, put print statements inside the loop to see the values it is actually using. This is called debugging. 42:16
- The range function stops just before the end number it is given. To include the last number, you must go one past it, so use end + 1. 43:43
- A for loop repeats a set number of times, based on the length of its sequence. A while loop is better when you do not know ahead of time how many times the code should repeat. 45:39
Words to know
- +=
- A short way to write 'add this amount to the variable and save the new total back into it.'
- range
- A tool that produces a list of numbers for a loop to walk through. It stops one step before the end number.
- debugging
- Finding and fixing mistakes in code, often by checking what the program is doing step by step.
- factorial
- The result you get by multiplying a whole number by every whole number below it, down to 1. For example, 4 factorial is 1 times 2 times 3 times 4.
Example
Think of counting the pages of a book from page 3 to page 5. If you stop before reaching page 5, you only count pages 3 and 4. You have to go one past the last page you want, or you miss it, which is why the lesson adds 1 to the end number.
3 checked practice questions for this part are in the app.
← Lecture 2: Strings, Input/Output, and Branching · Lecture 4: Loops over Strings, Guess-and-Check, and Binary →
Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.
LessonBound · Free lecture notes · Privacy · Terms · New York