Join WhatsApp

Join Now

Join Telegram

Join Now

When I Learned What Loops Could Do, My Coffee Went Cold

By Sagar Miraje

Published On:

Follow Us

I still remember this one morning, fresh into my teaching career. I had just finished prepping a session on loops. It was going to be the usual syntax, a few examples, a quiz maybe. I made myself a hot cup of filter coffee, walked into the classroom, and greeted the batch of 25 college kids staring at me with half interest and full phones. “Today,” I said, “we’re going to learn about repeating things. Not like your school PT teacher yelling ‘again!’ 50 times but with code.” 

The projector hummed, the lights dimmed, and I drew a small box on the whiteboard. I wrote: 

for(int i = 0; i < 5; i++) 
   printf(“%d “, i); 
 

And I turned to the class: “What will this print?” 

A few murmured “zero to four,” one girl confidently said “five times,” and one brave soul asked: “Sir, is this like a loop?” 

“Yes,” I said. “This is a loop. A for loop. It’s like when you know exactly how many times you want to do something. Structured. Controlled. Disciplined. Like catching a train you know the time, the platform, and the number of stops.” 

That morning, my coffee went untouched. I didn’t realize it then, but that was the first time I saw students visibly understand repetition in code not just mimic it. 

Loops: The Human Problem That Computers Solve Elegantly 

Let me begin with something all of us hate: repetition. 

Imagine being the boss of a 1000 employee company, and you want to send a holiday notice: “Office is closed on Sunday, September 5. Enjoy your holiday!” Now, would you really sit and send this message 1000 times? 

You could use Gmail, but you’d still be clicking and copying and pasting. 

Or… you could write a loop. 

Let’s say you had a list of 1000 email addresses. Here’s how a simple loop could send that message: 

int i = 0; 
while(i < 1000) { 
   // send email to employee[i] 
   i++; 

 

That’s it. You’ve automated something that would have taken you hours. And that, I tell my students, is the first real magic of loops. Computers are good at doing the same thing over and over, fast and without getting bored. 

Humans? Not so much. 

The Two Faces of Repetition: for and while 

Now, I often tell my students: for and while are like two different personalities in a team. 

  • for is the punctual employee. Knows how many tasks need to be done and gets to work. Perfect when you know the number of repetitions. 
  • while is the flexible freelancer. Keeps working until the task is done. Doesn’t ask “how many,” just “should I continue?” 

Let’s see them both in action. 

The for loop 

for(int i = 1; i <= 5; i++) { 
   printf(“Step %d\n”, i); 

 

This loop is like saying: “Do this five times.” Clean. Predictable. 

I remember one student, Akash, who said, “Sir, this feels like setting an alarm clock. I know I have five alarms 6 AM to 6:20 AM every 5 minutes and I won’t wake up until the last one goes off.” 

Not bad, I told him. Structured control yes. 

The while loop 

Now look at this: 

int i = 1; 
while(i <= 5) { 
   printf(“Step %d\n”, i); 
   i++; 

 

It prints the same thing, but the mood is different. 

Here, you start without a full plan. You just keep going as long as the condition is true. Like a chef who keeps tasting the soup until it’s perfect he doesn’t say “I’ll stir this five times.” He says, “I’ll stop when it tastes right.” 

A True Story: When the Loop Went Wild 

A few years ago, I gave a student a task to print numbers from 10 down to 1. She used a while loop, like this: 

int i = 10; 
while(i >= 1) { 
   printf(“%d “, i); 

 

She ran the program. It hung. 

“What happened?” I asked. 

She squinted and said, “It’s stuck!” 

“Did you forget something?” 

It took a second before she slapped her forehead. “I forgot to decrement i!” 

This is what I call the classic infinite loop trap. You see, if the condition never becomes false, the loop runs forever. There’s no escape. 

The same mistake wouldn’t happen as easily in a for loop, because its structure naturally includes increment or decrement logic. That’s why I say: if you know your steps, use for it saves you from forgetting things. 

Under the Hood: How for and while Really Work 

Let’s break this down. 

A for loop has three parts in one line: 

for(initialization; condition; update) 
 

That’s equivalent to: 

CopyEdit 

initialization; 
while(condition) { 
   // statements 
   update; 

 

In fact, every for loop can be written as a while loop. But not every while loop should be written as a for. 

Here’s a comparison I often give: 

  • for is like a recipe: step 1, step 2, repeat N times, done. 
  • while is like cooking by taste: keep stirring until the flavor is just right. 

Real life Uses: Where I Use Each 

Using for 

  • Looping through known length arrays 
  • Printing tables (like multiplication table) 
  • Running a fixed number of tests or iterations 
  • Menu options that repeat a fixed number of times 

Example: 

for(int i = 1; i <= 10; i++) { 
   printf(“2 x %d = %d\n”, i, 2*i); 

 

Simple. Elegant. 

Using while 

  • Waiting for user input until valid 
  • Reading a file till end 
  • Repeating a process until success 
  • Monitoring a sensor 

Example: 

char answer; 
while(answer != ‘y’) { 
   printf(“Have you completed the assignment? (y/n): “); 
   scanf(” %c”, &answer); 

 

I once had a student build a game menu using while(1) and a switch case. It kept the program running until the user decided to quit. That’s where while truly shines. 

Nested Loops: One in Another 

Sometimes, you want to loop inside a loop. 

Like printing patterns: 

for(int i = 1; i <= 5; i++) { 
   for(int j = 1; j <= i; j++) { 
       printf(“* “); 
   } 
   printf(“\n”); 

 

The outer loop controls the lines. The inner one controls the stars. I always call this “matrix looping,” and it’s a joy when students get the rhythm of it. 

Common Pitfalls I’ve Seen 

  1. Missing update step in while 
    Forgetting i++ leads to infinite loops. Always trace your logic. 
  1. Using for when while is better 
    Some students force a for when the loop depends on user input. That makes code less readable. 
  1. Putting semicolons after loop header 
    This: 

while(i < 5); 
 

is not what you think. That semicolon ends the loop before it begins. 

  1. Off by one errors 
    Students often write i <= n when they meant i < n. Teach them to test boundary cases. 
  1. Misunderstanding scope 
    Declare loop variables properly. Avoid things like: 

for(int i = 0; i < 5; i++) printf(“%d”, i); 
printf(“%d”, i); // Error: i is not accessible here 
 

A Final Thought 

Loops are the heartbeat of many programs. Once students understand how to control repetition, it opens up possibilities: automation, iteration, simulation, even game logic. 

Sometimes I end the class with a joke: “The only thing scarier than infinite recursion… is an infinite loop that just prints ‘hello’ forever.” 

But really, teaching loops has taught me something too about patience, clarity, and the art of solving big problems by solving small ones repeatedly. 

Next time you find yourself repeating something, stop. Ask yourself: Can I loop this? 

Can I replace every while loop with a for loop? 

Technically yes, but not always logically. If the number of repetitions is unknown, while is the better choice. 

What happens if I forget to update the loop variable?

You risk creating an infinite loop, especially in while where updates aren’t enforced by syntax. 

What is the difference between do while and while? 

do while runs at least once even if the condition is false. while checks the condition before the first run. 

Should I always prefer for because it looks cleaner? 

Use for when you know the iteration count. Don’t force it where it doesn’t fit. 

Can I nest for inside while or vice versa? 

Absolutely. Nesting loops is common, especially for pattern printing or matrix operations. 


I am Sagar Miraje, a Computer Science graduate with a Bachelor of Technology and 7 years of experience as a C language programming developer. I specialize in writing efficient, low-level code for systems programming, embedded applications, and performance-critical software. My strength lies in optimizing memory usage, handling pointers, and working close to the hardware to deliver fast and reliable solutions. Over the years, I’ve contributed to core system modules, debugged complex runtime issues, and collaborated with cross-functional engineering teams. I’m passionate about problem-solving and always eager to push the limits of what C programming can achieve.

Leave a Comment