dark mode light mode Search Menu

Loops, a Fairy Tale

Boston Public Library on Flickr

Loops, such as the FOR loop or WHILE loop, are programming constructs for repeating a set of instructions until some termination criterion is met. Two primary things define a loop: 1) What you do inside the loop and 2) the conditions to stop looping.

The blacksmith Drex was losing his patience. His new apprentice, Simon, wasn’t working out. In fact, Drex had never had a worse apprentice in his thirty-five years as Garroow’s master blacksmith. Simon could barely lift the hammer, let alone swing it with sufficient force to shape metal. However, worse than that, Simon also lacked the necessary intelligence to carry out even simple tasks. Had it not been for his diminutive size, Drex might have thought that Simon was actually an ogre.

Drex found himself constantly repeating instructions: “Now, hit the metal again.”?

“And again.”?

“And again . …”

Drex’s patience was wearing thin. He hated repeating himself.

Finally, Drex decided to try an experiment. “Simon, hit the metal twice,” he commanded.

Clank. Clank. Simon complied.?
“Now, turn it over and flatten it,” Drex commanded.

Simon flipped the deformed-looking horseshoe and hit it once. Then he paused and looked back at Drex, confused.

Drex sighed loudly. Was that really too much for Simon to handle? The boy was hopeless.

“It’s a loop!” shouted Drex. He knew that Simon wouldn’t understand, but at least shouting made Drex feel better. “A simple, simple loop.”

“A loop?” asked Simon.?
“Haven’t you ever heard of a loop?”

Simon shook his head sadly.

Drex realized that they had hit the core of the problem. How could Simon function as a reasonable blacksmith without understanding how loops worked? Then again, Drex had no idea how Simon could function as a human without understanding loops.

“A loop is defined by two things: something to do and a way to know when to stop doing it. You keep doing that one thing over and over until you stop,” Drex explained calmly, reciting the favorite description by Garroow’s famous scholar Dr. Whileton. Of course, Dr. Whileton tended to repeat himself, so he would have explained it at least a few times.

Simon stared back blankly.?
“Think about a one-mile race,” Drex suggested. “You run around the track until you have gone a mile. That’s four laps, right? So, running is the thing you do and having run a full mile is how you know when to stop. The track even looks like a loop.”

“I run until someone tells me to stop,” said Simon.

“Of course you do,” muttered Drex.

“In this case,” continued Drex, “I want you to keep hammering that horseshoe until it’s flat. As soon as it’s flat, you can stop. WHILE the horseshoe is not flat, hit it with the hammer.”

“Okay,” agreed Simon happily. He promptly set about hitting the horseshoe over and over again. By the end, Simon breathed heavily from the effort, but he had succeeded in flattening the horseshoe.

Drex was stunned. How had Simon understood that?

“Good. Now go get the coals hot,” Drex commanded.

Simon looked confused again.

Drex sighed. “It’s another loop. Pump the bellows 10 times. FOR each number that you count from 1 to 10, give the bellows a pump.”

“Okay.” Simon again got to work, pumping the bellows exactly ten times. He counted loudly each time: “One … two … three … four … five … six … seven … eight … nine … ten.”

Over the course of a week, Drex determined that Simon would repeat tasks if they were well specified in a loop. Drex would tell Simon exactly what task to repeat and exactly how long to repeat it. Sometimes he told Simon to count up to a certain number. Other times he phrased the command like a WHILE loop, telling Simon to continue doing something until he had met a goal.

Simon responded well to these structured commands. The blacksmith’s shop was filled with the noise of Simon cheerfully counting and hammering. “One … bang … two … bang …”

Eventually, Drex introduced nested loops, issuing instructions such as “WHILE the sword is not thin enough, turn it over and FOR each number from 1 to 5, hit it with the hammer.” Simon would happily go about banging the sword into shape while turning it over after every five hits.

Unfortunately, this formalized approach only worked to a point. Disaster finally struck when Drex tried to teach Simon more complicated computational concepts. As Drex and Simon stood outside the burning blacksmith’s shop, Drex admitted defeat. Before leaving town for an open blacksmith’s position in New Athens, he found Simon a better job—counting laps for runners on the local track team.

If you liked this story, the author has written many more collected in several books linked below, including in July 2016 Search: A Tale of Algorithms, Computation, and Conspiracy. This story is reprinted with permission of the author, Jeremy Kubica.

Learn More

Search: A Tale of Algorithms, Computation, and Conspiracy


Computational Fairy Tales


Search: A Tale of Algorithms, Computation, and Conspiracy

To be released July 2016, you can order early and get a discount.

Best Practices of Spell Design


Computational Fairy Tales


SmallTalks: Jeremy Kubica and Computational Fairy Tales