Loops: for and while - English B2 (Upper Intermediate) English A1-C2 Study Notes
Overview
Imagine you have a chore, like washing 10 dishes, or you need to do something until a specific condition is met, like stirring soup until it's thick. Would you write down 'wash dish 1, wash dish 2, wash dish 3...' ten times? That would be super boring and take forever! In programming, we have something called **loops** that help us do repetitive tasks without writing the same instructions over and over. They're like magic shortcuts for telling a computer to do something many times, or until a certain goal is reached. This makes our code much shorter, neater, and easier to understand, just like saying 'wash all the dishes' instead of listing each one individually. Understanding loops is super important because almost every computer program, from video games to social media apps, uses them constantly to get things done efficiently. They are the workhorses of programming!
What Is This? (The Simple Version)
Think of loops like a recipe that tells you to repeat a step.
Imagine you're making cookies. The recipe doesn't say 'add one cup of flour, stir, add one cup of flour, stir, add one cup of flour, stir' if you need three cups. Instead, it says 'add one cup of flour, stir, repeat 3 times'. That 'repeat 3 times' is like a loop!
In programming, a loop is an instruction that tells the computer to do a specific set of actions (called a block of code) multiple times. There are two main types of loops we'll talk about:
- For loops: These are used when you know exactly how many times you want to repeat something. Think of it like a countdown timer: 'Do this 5 times, then stop.'
- While loops: These are used when you want to repeat something until a certain condition is met. Think of it like a chef stirring soup: 'Keep stirring while the soup is thin.' Once the soup is thick, the stirring stops.
Real-World Example
Let's imagine you're a teacher and you need to give a sticker to every student who got a perfect score on a test. You have a list of 20 students.
Using a 'for' loop idea:
- You know you have 20 students. So, you'd think: "I need to check each of the 20 students, one by one."
- You'd go to the first student, check their score. If perfect, give a sticker. Then move to the next.
- You'd repeat this process exactly 20 times, once for each student on your list.
Using a 'while' loop idea:
- Imagine you're playing a game where you have to collect 10 coins. You don't know exactly how many steps it will take to find all 10 coins, but you know you need to keep going while you have less than 10 coins.
- You keep searching, picking up coins, and checking your coin count.
- You stop searching when your coin count reaches 10 (or more).
See how 'for' is about a fixed number of repeats, and 'while' is about repeating until a condition changes?
How It Works (Step by Step)
Let's break down how these loops generally work in a computer program: **For Loop (Counting Loop):** 1. **Setup the Counter:** The computer first sets up a special number (a **counter**) to keep track of how many times it has repeated. 2. **Check the Limit:** It checks if the counter has reached ...
Unlock 3 More Sections
Sign up free to access the complete notes, key concepts, and exam tips for this topic.
No credit card required ยท Free forever
Key Concepts
- Loop: A programming instruction that tells a computer to repeat a set of actions multiple times.
- For Loop: A type of loop used when you know exactly how many times you want to repeat a task.
- While Loop: A type of loop used when you want to repeat a task until a specific condition becomes false.
- Iteration: A single run or execution of the instructions inside a loop.
- +4 more (sign up to view)
Exam Tips
- โAlways identify if you need to repeat a fixed number of times (for) or until a condition is met (while) before writing your loop.
- โFor `while` loops, carefully check that your loop's condition will eventually become false to avoid an infinite loop.
- +3 more tips (sign up)
More Digital Literacy Notes