Iterative Loops: Loop Conversion

    Iterative Loops: Loop Conversion

      Take a moment to think back on your childhood. Think of all those Berenstain Bears, Junie B. Jones, Goosebumps, and (of course) Dr. Seuss books you read over and over and over again until every page ended up with some sort of jam stain and the covers started to pull away from the binding.

      That’s it, just let the nostalgia wrap around you like your favorite stuffed animal, Sir Snuggs-a-Lot.

      Our personal favorite was The Lorax. We just really needed a kneed, okay? Who doesn't love things that can actually be turned into 25+ things? Convertible cars, zip-off cargo pants, air hockey/foosball tables, and catpacks are kind-of like that, but most actual objects can only convert into two—maybe three—things, max.

      The need doesn't exist in the real world (fortunately or unfortunately), but loops do and they can always be converted between while, for, and recursive loops. Talk about utility. We aren't going to talk about recursion here because there's a whole other guide for it. For now, we're going to focus on for and while loops.

      From For to While Loops

      Let's go back to lists for a moment. In many languages, words are just about the same as lists of characters, so if we wanted to modify a string, we could treat it like a list. To turn all the letters in a word into uppercase letters, we'd need to write a loop that runs through the word, modifying each character individually. Let's write a program to do this with the word "Beetlejuice."

      …As long as we don't print it three times, we should be fine, right? Right?!?

      word = "Beetlejuice"
      FOR EACH letter IN word:
      	letter = uppercase(letter)
      END FOR

      Changing this for-each loop into a while loop is actually pretty simple: we just need to tweak the ending condition. In order to make that condition endable, we'll also need to explicitly tell the while loop to look at the next character in the word at every iteration. For loops were born to do this, but while loops? Not so much.

      word = "Beetlejuice"
      wordLength = length(word)
      index = 0
      WHILE index LESS THAN wordLength:
      	word[index] = uppercase(word[index])
      	index = index + 1
      END WHILE

      Both of these loops act exactly the same. The only difference is that stuff the language handles internally in the for-each loop has to be written explicitly in the while loop. In this case, the extra code is about three extra lines. Our opinion? Because the for-each loop internally keeps track of the stuff you'd need to cover externally in a while loop, it's probably the better option. But, almost like talking in different human languages, you can usually express anything you want in any language regardless of whether they have this particular flavor of for loop or not.

      From While to For Loops

      Let's go in the opposite direction now, converting a while loop into a for loop. Say we went on vacation for a week and wanted a while loop to water some plants in our garden while we're away.

      IF gardenSoil EQUALS dry:
      	WHILE gardenSoil EQUALS dry:
      		water(plants)
      	END WHILE
      END IF

      For obvious reasons, we don't normally want our loops to potentially run for an infinite amount of time. That would end up in an incredible amount of wasted water—and some dead plants.

      Keep in mind that we're on vacation, though, and instead of hiring a plantsitter to keep track of things, we thought we'd go the Silicon Valley route of letting the computer control things. The computer has no way of knowing exactly how much water each plant's going to need before the soil's no longer dry.

      Still, there are ways to simulate while loop-itude in a for loop just to make sure things actually end. If we throw a condition into the loop, we can break the loop early while setting the end number of the for loop to something very large. Think of this loop as making doubly sure the loop will end.

      FOR counter FROM 0 TO 25:
      	IF gardenSoil EQUALS dry:
      		water(plants)
      	ELSE:
      		BREAK from for loop
      	END IF
      END FOR

      25 is an arbitrary number that keeps the loop from running infinitely. If the soil stops being dry before that time, though, the code's going to break from the loop. Till then, it'll keep running—with only a couple extra lines of code.

      It might not be as impressive as Transformers, but hey. Hardly anything is as impressive as an anime about cars that are secretly alien robots.