Repeating steps efficiently
Loops: Repeating Steps Efficiently
Key Points
- A LOOP repeats a set of instructions many times without having to write them out repeatedly
- FOR loop: repeats a known number of times — 'FOR i = 1 TO 10: print i' prints numbers 1–10
- WHILE loop: repeats AS LONG AS something is true — 'WHILE hungry: eat food'
- Loops are one of the most powerful tools in coding — they handle huge tasks in just a few lines!
📘 Examples
❓ How many times does this run? FOR i = 1 TO 5: print 'hello'
✅ 5 times! It prints 'hello' once for i=1, once for i=2, all the way to i=5!
❓ A robot claps 10 times. How would you use a loop?
✅ REPEAT 10 TIMES: clap. Just 2 lines of code instead of writing 'clap' 10 times!
❓ What's the difference between a FOR loop and a WHILE loop?
✅ A FOR loop repeats a specific number of times. A WHILE loop repeats as long as a condition is true — you might not know how many times it will run!
🌟 Fun Fact!
The first computer 'bug' was a REAL insect! In 1947, Grace Hopper's team found a moth stuck in a Harvard computer relay causing errors. They taped it in their logbook and wrote 'First actual case of bug being found'. 🐛