4.1 - Loops (2026-09-09)
Overview
Chris Bourke introduces C-style for and while loops, explaining initialization, continuation conditions, updates, and loop bodies through runnable counting examples. He compares loop syntax and scope, demonstrates increment and compound-assignment operators, and gives practical guidance for choosing a loop and avoiding infinite loops.
Key takeaways
- Every loop needs initialization, a continuation condition, an update that progresses toward termination, and a body that performs the repeated work.
- A C for loop checks its condition before each iteration and performs its update afterward; `i <= 10` includes 10, while `i < 10` does not.
- A loop's update and condition must agree in direction: decrementing a value while testing whether it remains below an upper bound can prevent termination.
- Declaring a variable in a C for-loop initializer limits its scope to the loop, while a variable declared before a while loop must be declared separately.
- Compound assignments such as `i += 2` support counting by steps, and integer division such as 5 divided by 2 truncates to 2.
- Use a for loop when the iteration range is clear, such as 10 through 100 by tens; use a while loop when repetition depends on a changing condition or input.
Chapters
- After opening music, Chris Bourke announces a computing organizations fair from 4:00 to 6:00 at the City Union.
- He recommends SESAB resume-night and Career Fair 101 events as practice for internships, resumes, and interviews.
- The lesson begins with loops as a way to repeat code across different amounts of data without copy-pasting.
- A loop starts with an initialization statement, such as setting an index variable to 1.
- A continuation condition determines whether another iteration runs; its opposite describes when the loop terminates.
- An update statement must make progress toward termination, or the loop can run indefinitely.
- The loop body contains the work repeated on each iteration.
- The for-loop header contains initialization, continuation, and update sections inside parentheses, separated by semicolons.
- The example initializes `i` to 1, continues while `i <= 10`, and prints `i` in the loop body.
- The condition is checked before each iteration, and `i++` runs after the body, producing the values 1 through 10.
- The update section has no trailing semicolon, a convention shared by C-style languages such as C++, Java, and PHP.
- Python Tutor can visualize C execution step by step, showing when `i` is initialized, tested, printed, and updated.
- The for-loop initialization runs once, the continuation condition is checked before each iteration, and the update runs at each loop's end.
- `i++` abbreviates adding 1, `i--` subtracts 1, and operators such as `+=`, `-=`, `*=`, and `/=` update a variable in place.
- Integer division truncates the fractional part: dividing an integer value of 5 by 2 leaves 2.
- In C, declaring `int i` in a for-loop initializer limits `i` to the loop; using it afterward produces an undeclared-variable error.
- Declaring `i` before the loop extends its scope, but Bourke recommends limiting variable scope as much as possible.
- A loop that counts from 1 through 10 leaves an externally declared `i` at 11 after the final update fails the condition.
- Index variables commonly use `i`, `j`, and `k`; a countdown from 10 to 1 uses `i--` with a condition such as `i > 0`.
- Starting at 0 and continuing while `i < 10` prints 0 through 9, while changing the condition to `i <= 10` includes 10.
- Using `i += 2` with a strict upper bound of 10 prints 0, 2, 4, 6, and 8.
- Starting at 3, continuing while `i <= 15`, and adding 3 prints 3, 6, 9, 12, and 15.
- A countdown requires both a decrementing update and a condition that matches the downward direction; decrementing while `i <= 10` instead keeps moving away from termination.
- Bourke recommends consistent spacing and indentation, with the closing brace aligned with the `for` keyword and the loop body indented.
- A loop can perform several operations per iteration, such as printing `i` and adding it to a `total` variable.
- Starting `total` at 0 and accumulating the values 0 through 9 produces 45.
- Resetting `i` to 0 inside the loop defeats the update and causes an endless sequence; Control-C stops a running program.
- A while loop places initialization before the loop, keeps the continuation condition in its parentheses, and usually puts the update at the end of the body.
- Moving the update to the beginning changes behavior: a loop initialized at 0 can print 1 through 10 instead of 0 through 9.
- Unlike a for-loop initializer, a while-loop condition cannot declare a variable whose scope is limited to that loop.
- Forgetting the update while testing `i < 10` repeatedly prints 0; Control-C terminates the resulting infinite loop.
- Bourke's general guideline is to use a for loop when the iteration count is known, such as printing 10, 20, through 100 with `i += 10`.
- A while loop is generally suited to repetition that depends on an input or condition whose duration is not known in advance.
- For even numbers up to an input `n`, the example starts at 2, increments by 2, and uses `i <= n` to include an even endpoint.
- When `n` is odd, the next even value exceeds `n` and fails the condition; the starting value also determines whether 0 is included.
- The closing exercise asks how to count the digits of an input integer, such as returning 4 for 1234.
- A logarithm-based shortcut is discussed: base-10 logarithms indicate a number's magnitude, with an adjustment needed to obtain a digit count.
- The class is left to develop a loop-based method for counting digits before the next session.
Transcript excerpt
0:02 [music] [music] Heat. Heat. [music] [music] Heat. Heat. Heat. [music] Yeah. Heat. [music] [music] [music] Heat. Heat.
0:52 [music] Heat. Heat. [music] [music] Yeah. Heat. [music] [music] >> [music] [music] >> Heat. [music] Heat. [music]
1:46 Round round about [music] again. So, you are the only one in what I say. [music] [music] Yeah. Heat. [music] [music] [music] Heat. Heat. Heat. [music] The rain [music] always starts with go
2:38 way. [music] Okay. So, before we start, I wanted to bring to your attention this week's bits and bites. Uh remember that there's an involvement fair today uh from 4:00 to 6:00 in the main union uh in the main city union. Uh and that's uh for computing organizations only. So, it's kind of like the ones that happened a
3:26 week or two ago. Uh but it's only for computing. Uh also, uh there are a couple of opportunities here coming up for career uh development. uh SESAB is holding a resume night as and then there's also a career fair 101. That's not the actual career fair. Their career fair is later I think in October. Uh but uh it'll of course it'll be posted here. Uh but you know it's it's a good practice to go there and find out what you should be doing to prepare for it. Uh what you should bring to it, your resume, a generic cover letter, things like that. Uh, and it's never too early to start doing that process even though you're in your first few weeks of a CS1 course. Uh, first of all, you can still get an internship uh during your freshman year or the summer after your freshman year. Uh, but also, even if you
4:15 don't intend to get an internship, it's a good pra idea to go and get some
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Chris Bourke.