For Loops, with Fruit loops!

Michael Solone
4 min readAug 6, 2018

--

One of a computers greatest strength is its ability to process and calculate copious amounts of data in very short time. One programming method that can harness that power is known as a “for loop”, in this post I would like to explain the process of “for loops” using the only logical example, Fruit Loops!

Here we go!

The basic principle of a “for loop” is to the tell the computer to do something, a certain amount of times… here’s an example using Javascript:

This prints out every number starting at zero and up to but not including 5.

for (i = 0; i < 5; i++) {

console.log(i)

}

Lets break it down

( i = 0 ) : this sets the starting point of the counter at zero

( i < 5 ) : this tells the loop to stop right before i = 5

( i++ ) : this tells the counter to increment up by 1 every time a single loop completes

( console.log(i) ) : this is the action that happens in the loop, in this case it prints what ever number “i” is to the console.

Now obviously this is a really easy process for humans to do on their own, but computers can calculate millions of pieces of data in an instant, so it could log all the numbers up to a thousand — heck even a million — in less time than it takes to read this paragraph, pretty powerful huh!?

Now back to the Fruit Loops!

Let’s say that you have a giant array (fancy programmer speak for a list)that contains the color of each individual fruit loop in a bag of cereal…a quick search of the web tells me that the average box of Fruit Loops has 1500 individual pieces, thanks to http://www.texascooking.com/features/apr2005frootloops.htm

so that a pretty big list..

Oh yeah! I know you like that!

Example!!

fruitLoopColors = [‘green’, ‘orange’, ‘green’, ‘purple’, ‘yellow’,’orange’, ‘red’, ‘red’, ‘purple’, on and on and on all the way to 1500 Fruit Loops]

Now since the best flavor is red I want to know how many of them there are in my box, so I can hoard them all for myself…muhahahahahhaha! No, but seriously red is the best you can’t have any!

What happens if you touch the red loops!

Counting that many Fruit Loops would be exhausting and as you can probably tell by how much I like to eat sugary kids cereal, I get exhausted really easily… so lets make the computer do it!

for ( i = 0; i < fruitLoopColors.length; i++) {

redFruitLoops = 0

if (fruitLoopColors[i] === ‘red’) {

redFruitLoops += 1

} }

Lets break this down

( i = 0 ) : this still sets the starting point of the counter at zero

( i < fruitLoopColors.length) : this tells the loop to stop once “i” reaches the total number of items in our fruit loop list, so basically it will keep going until it hits every fruit loop in the box.

( i++ ) : this tells the counter to increment up by 1 every time a single loop completes

( if (fruitLoopColors[i] === ‘red’ ): this is an “if” statement, which will only execute if the condition inside the parenthesis is met.

( redFruitLoops += 1 ) : when the “if” condition is met the total number of redFruitLoops will increase by 1

To recap, the for loop will go through the entire box of cereal and only count when the Fruit Loop is red, so when its done you will have the total number of red fruit loops in the box!

So, now that you know the power of “for loops” I’m going to eat my red Fruit Loops! You can have the gross purple ones!

ALL MINE!

--

--