What is a loop?

A loop is a way of telling the computer: "Repeat this task again and again until a certain condition says stop."

You write the instruction once, and the loop keeps running it for you. Every loop works on three ideas:

  1. Starting point: where the repetition begins.
  2. Condition: a question checked each time, such as "Should I keep going?"
  3. Change (update): something that moves forward each round so the loop eventually ends.

If nothing changes, the condition never becomes false, and the loop runs forever. This is called an infinite loop.

Why do we need loops?

  • To avoid repetition: Printing numbers 1 to 1000 by hand would mean writing 1000 lines. A loop does it with one instruction.
  • To handle unknown amounts of data: You don't know in advance how many items a user will add to a cart, but a loop can process however many there are.
  • To automate tasks: Checking every student's marks, sending a message to every contact, or searching through a list all rely on loops.
  • To keep code short and easy to maintain: If you need to change the task, you change it in one place instead of hundreds.

Types of loops, explained with real-life examples

1. for loop: when you know the count

Example: A teacher takes attendance for a class of 40 students. She knows exactly how many names to call, so she starts at roll number 1, goes up to 40, and stops.

2. while loop: when you only know the stopping condition

Example: You keep eating chapatis while you are still hungry. You don't know beforehand whether it will be 2 or 5. You check your hunger before each one.

3. do...while loop: runs at least once

Example: At a restaurant, you taste the food first and then decide whether to order more. The tasting always happens once, and the decision comes after.

4. for...of loop: going through values

Example: You open a fruit basket and pick out each fruit one by one: apple, banana, mango. You care about the items themselves.

5. for...in loop: going through labels (keys)

Example: You fill in a form with fields like Name, Age, and City. You go through each field label and look at what's written next to it.

6. forEach: doing the same action on every item

Example: A shopkeeper puts a price sticker on every product on a shelf. The same action is applied to each item, one after another.

Controlling a loop

  • break: stop completely. You're searching for your keys in 10 drawers. You find them in drawer 3, so you stop searching the rest.
  • continue: skip this round and move on. You're checking a stack of letters and skip the junk mail without reading it, then continue to the next letter.

Quick summary

LoopIdeaReal-life example
forKnown number of repeatsCalling 40 roll numbers
whileRepeat while a condition is trueEating while hungry
do...whileRun once, then checkTasting before ordering more
for...ofGo through each valuePicking fruits from a basket
for...inGo through each key/labelReading form fields
forEachSame action on every itemPutting price tags on products

In one line: a loop is the computer's way of doing repetitive work for you, stopping at the right moment, based on a condition you set.