Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we are going to discuss an important programming construct: the FOR loop. Does anyone know why we might want to use a loop in programming?
I think it helps us run the same code multiple times without repeating it ourselves.
Exactly! The FOR loop allows us to execute a block of code a specified number of times—useful when we know how many iterations we want to complete. Let’s look at the syntax. Can anyone try to summarize it?
It starts with 'for', then a variable name, followed by 'in range', and the starting and stopping points.
Good job! Here’s a memory aid: remember 'FRS' for 'For, Range, Step' to help you recall the framework of the loop.
Can we see an example of that?
"Absolutely! Here’s a simple example:
Now let’s discuss another application of the FOR loop: iterating over lists. How do you think a FOR loop could be used with a list of items?
Maybe to print each item in the list?
"Absolutely! Here is an example:
To wrap up our session on FOR loops, can someone remind me what key points we’ve covered today?
We learned that the FOR loop is used to repeat code a specific number of times.
And we also saw how to use it with ranges and lists.
Great summaries! Remember, using the FOR loop helps avoid repetitive code and saves time. Next time, we’ll dive into WHILE loops for tasks that require conditions instead of set iterations.
For loops go 'round, without a sound, run your code, from start to ground.
Imagine a chef using a FOR loop to gather ingredients. Each time they scoop flour, they count: one for the cake, two for the bread, three for the muffins, and so on until they're done!
Remember 'FLS' for 'For, List, Sequence' to recall the key elements of a FOR loop.
FRS - For, Range, Step, to remember the key components of a FOR loop's function.
Term
What is a FOR loop?
Click to flip
Term
What does the range function do?
Click to flip
Term
How do you iterate over a list?
Click to flip
A control structure that repeats a block of code a specified number of times.
A function that generates a sequence of numbers, defined by a start, stop, and step.
An ordered collection of items that can be iterated over in programming.
The process of executing a block of code multiple times.
The FOR loop is a programming construct that allows for the repetition of a block of code a specified number of times, making it essential for tasks where the number of iterations is known.
In this section, we explore the FOR loop, which facilitates the repetition of code a specific number of times using a defined syntax. We also discuss using FOR loops with range and lists, providing examples to illustrate the concept effectively.
The FOR loop is a key component in programming that allows for a block of code to be executed multiple times. It is particularly useful when the number of iterations is known ahead of time. The basic syntax for a FOR loop is:
for variable in range(start, stop, step):
statement(s)In the code example below, the FOR loop iterates from 1 to 5, printing out "Hello" followed by the current value of i:
for i in range(1, 6):
print("Hello", i)Output:
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5FOR loops are also effective for iterating through elements of lists. For example:
colors = ['red', 'blue', 'green']
for color in colors:
print(color)This outputs each color in the list:
red
blue
greenIn summary, the FOR loop is an essential tool in programming that allows for efficient iteration over a sequence of numbers or items, enabling repetitive tasks to be handled elegantly.