Day[14] – A Little Bit of Rest Day – I’ll Teach you About Loops

Today was a bit of a rest day. Well, more like a lazy day. I wanted to work on some things but ended up taking some naps and resting for most of the day. I’m okay with this though. I don’t want to burn myself out.

I was still a bit productive because I had a meeting with my buddies on a project we are working on. We are trying to build a community around sharing healthy recipes for Filipinos.

Filipino food can be unhealthy. So, we are trying to encourage our members to have a healthier lifestyle by providing them with healthier alternatives. We have a launch day next week. So, we were hashing things up tonight.

I also watched some Colt Steele videos. I’m on array nows. I am trying to go fast forward the stuff that I already know so that I can go to the parts I don’t know quicker.

Anyway, I want to talk about loops today.

I think aside from conditionals, loops are very important in programming because it allows you to repeat code that needs repeating. Without loops, you would have to write code many many times.

For example, let’s say you want to print hello 3 times. Without loops, you’d have to do this.

console.log("hello");
console.log("hello");
console.log("hello");

Imagine if you want to print hello 100 times. That’d be a nightmare to type (or even to copy/paste).

This is where loops come in. I’ll talk about the 2 basic loops while and for

while

The while loop is composed of the following

  1. A starting point
  2. The while keyword
  3. A condition to evaluate
  4. The code you want to execute
  5. An iterator

Here is how we would print hello 3 times using the while loop

let x = 1; // This is the starting point of the loop

while (x <= 3) { //The while keyword plus the condition which checks if x is less than or equal to 3
 console.log("hello"); // The code we want to execute
 x++; // The iterator. This adds 1 to x for every loop
}

One thing you want to avoid when using loops is the infinite loop. This is why the iterator is important. If you remove the iterator on our example, it will print hello forever because x will always be one and the while condition will always evaluate true.

At the same time, if your condition is wrong, this can cause an infinite loop as well. If you change the condition to x > 0 for example, that will evaluate as true all the time as well even with the iterator.

The lesson here is that you have to make sure that your condition is properly set up and that you have an iterator.

for

The for loop is very similar to the while loop. However, it’s more concise. It is comprised of:

  1. The for keyword
  2. A starting point
  3. A condition to evaluate
  4. An iterator
  5. The code you want to execute

You’ll notice that everything is basically the same. Here’s how you would write the same code as above as a for loop.

for (let x = 1; x <= 3; x++) {
// Above: for keyword (starting point; condition to evaluate; iterator)
 console.log("hello"); // The code we want to execute
}

As you can see, the for loop is shorter than the while loop. There is really not much difference between the two. You can use them interchangeably.  However, generally, if you know how many items you have to iterate, you can use the for loop. If you don’t then use a while loop.

Here’s an example of when a while loop is needed.

let x = false;

while (x === false) {
  console.log("hello");
  x = true;
}

This is a simplistic example but you can see that a for  loop will not work with this type of loop.

Well, that’s it. I hope that this was informative for you.

See ya tomorrow!

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *