Skip to content
Lesson 7

Loops And Repetition

Most of life is doing the same thing again and again. We pretend it is not, because each repetition feels different from the inside. But step back, and the patterns are obvious. You eat three times a day. You sleep once…

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

Most of life is doing the same thing again and again. We pretend it is not, because each repetition feels different from the inside. But step back, and the patterns are obvious. You eat three times a day. You sleep once a day. You go to school five days a week. You celebrate Eid twice a year. The world runs on repetition. Computers, being honest machines, embrace this fact. They have a small idea called a loop, and almost every useful program contains one.

Start with the tasbeeh in your nani's hand. After Maghrib she sits on the prayer mat, and her thumb moves bead by bead. Subhan Allah, Subhan Allah, Subhan Allah. The same words, thirty three times. She does not have to think about how to do it the second time, or the seventeenth time, or the thirty second time. The hand knows. The bead is the counter. When the last bead is reached, she switches to Alhamdulillah, again thirty three. Then Allahu Akbar, thirty four. This is, exactly, the structure of a loop. Repeat an action a fixed number of times, then move on.

The five daily prayers themselves form another loop, this time with a counter that runs over a day instead of a sitting. Fajr, Zuhr, Asr, Maghrib, Isha. Five times the same outer structure, niyat, qiyam, ruku, sajda, with different rakaat counts inside. The body learns the loop so deeply that a sleepy nine year old in Multan can perform Fajr without remembering how she got to the prayer mat. The loop has become muscle. Programmers feel a small jealousy when they watch this. They wish their loops ran half as cleanly.

Take a school morning. Six children of one family in Saddar must be ready by seven twenty. Without a loop, the mother of the house has to think about each child as a separate problem. With a loop, she has one rule, for each child from oldest to youngest, wake them, hand them the uniform, hand them the lunch box, push them to the bathroom. Same body, applied six times, with the child being the only thing that changes. This is exactly how a programmer would write it. For each child in the list, do the morning routine. The list could be six, or sixty, or six hundred. The code does not care. That is the magic.

There is also a loop with no fixed count, only a stopping condition. Think of your father at the petrol pump on Davis Road. He says bharo, fill it. The pump runs. The pump does not know how many litres the tank can take, only that it should keep flowing until the handle clicks. The condition is, until the tank is full. In code, this is called a while loop. While the tank is not full, keep adding petrol. While the homework is not finished, keep solving sums. While the chai is not the right colour, keep stirring. Real life is full of these. So is software.

An exercise for the week. Watch one adult in your house and write down every loop they run. Your dadi making rotis, atta gole, belan, tawa, repeat for six rotis. The chowkidar in your gali walking the round, gate to corner to corner to gate, repeat all night. The chacha at the kiryana shop counting coins for change. Each is a loop. Some have a counter. Some have a stopping condition. Some, like the chowkidar, have both. Once you see them, write a sentence describing the body and the condition. By the end of the week you will have collected ten or fifteen real loops, and the day you write your first one in code it will not feel like a foreign idea. It will feel like something you have known your whole life under another name.

Estimated time: 12 min