Why Using a “For” Loop in Coding

Lucas F. Lu
5 min readMar 17, 2020

--

What we do to stop loops from getting out of control

A guide for beginners of any background to start writing codes

Thus far, we know a loop without a constraint will run forever. Therefore, we need a way to stop the execution of a loop.

In addition to adding a counter and checking against its value within a while loop, we have something else that provides more control.

Introducing — the “For” loops

In our scenario mentioned in the previous article, we want our program to loop for only 5 times so that exactly 5 people can be allowed to enter the room.

On one hand, we certainly can use “while” loop to accomplish this task, but on the other hand, with this clear defined restraint on the condition, we can adopt the use of “For” loop.

Why using a “For” loop

We use “for” loops because we know exactly when to start and stop as well as the number of repetitions. This differs from the reason for using a “while” loop, which, we don’t really know how many times the loop will repeat, but solely depending on whenever the condition to stop is triggered.

In other words, “while” loop is used when we don’t care about the number of repetitions, while “for” loop is used when we know how many times we want the loops to repeat.

How to use a “For” loop

Let’s take a look at an example for our scenario that uses a “For” loop.

<?php    $counter=1;    for($counter; $counter<=5;$counter++)    {        echo 'Good morning master '.readline('What is your name: ');    }?>

There are certainly a lot of things going on here in this example. But relax, they are actually pretty simple to understand.

Code Explanation:

Once again, we need a variable to keep track of the number of repetition. Thus we need to define a variable called “counter” and initialized it to have a value of 1.

We use keyword “for” to instruct the computer to use “for loops” instead of “while loops”

Once more, what goes inside of the brackets is the condition for the loop to run and restraints that cause the loop to stop.

Importantly, we see these delimiters (;) appear inside the brackets. They are there for a good reason and let’s find out why from a computer’s point of view.

When a computer examines the condition to run a “for” loop, it firstly wants to know what variable it can use as part of the examination. In other words, it wants to know what is involved in the process of examination.

Therefore, the very first part of a “for” loop is to define that variable used. Sort of like a statement that tells the computer what variable to use. And since this is a statement or an instruction to the computer, it needs a delimiter (;) to inform the computer that the declaration is finished, and what goes after that delimiter will tell a totally different story.

The second part of the “for” loop is the condition. Defining how the computer is going to compare its variable declared previously and determine the condition to run and stop.

In our case, we want the loop to run as long as our counter is less than 5. In other words, the loop will stop when the counter’s value reaches 5.

Finally, the last part of the “for” loop definition is what we do with the variable after each looping process. Looking familiar? “$counter++” means “$counter = $counter + 1”. Meaning the computer will increase the value of the counter by 1 at the end of each loop.

Once again, everything written between the curly brackets (“{“ and “}”) is the scope of the loop. Meaning the list of tasks to be completed during each loop.

To sum it up, “for” loop definition consist of 3 parts sequentially. First, the variable used, second, a condition involving the variable for the loop to run. Lastly, what we do with the variable at the end of each loop.

I hope I did a good job explaining how to use the “for” loop, or maybe I over complicated the explanation. Please let me know if I did.

One last thing about loops

There is one last scenario I want to cover when it comes to using loops. It is not used often, but has its reason to be studied.

Let’s assume based on our previous example. What if there are already 6 people in the chat room? The result is simple: the loop will not run because the room is full.

But what if we want to let at least one person enter the chat room regardless if the room is full or not? That is, to run the process of asking for a name at least once then check if we want to repeat the loop or not.

Thankfully, there is a brand new toy for that matter.

Introducing — the “Do…While” loop

Like the name itself indicates, this form of loop will perform an action first then check to see if it needs to repeat again.

To put everything on paper, our program now looks like the following.

<?php    $counter=6;    do    {        echo 'Good morning master '.readline('What is your name: ');    }while($counter<=5)?>

Code Explanation:

Again, a variable counter is needed because we need to keep track of people in the room

Keyword “do” is used to instruct the computer to perform the contents of the loop first then check for condition to continue looping

Keyword “while” is placed at the very end of the looping structure to define the condition at which it should continue and repeat.

Summary

  • “for” loop’s definition has 3 parts. First, the variable needed. Second, the definition of a condition that uses the variable. Lastly, what we do with the variable at the end of each loop.
  • We use the “for” loop because we know exactly when to start and stop as well as the number of repetitions.
  • Use the “While” loop when we don’t care about the number of repetitions.
  • We use the “Do…while” loop when we want to run at least once for the loop.
  • Condition definition is placed at the very end of the looping structure for the “Do…while” loop.

There you had it! That’s everything we need to learn about loops for now. In the next article we are going to talk about another form of loop. Wait, what?

Relax, I was kidding, that’s just my way to tell you to have a cup of coffee and chill. We are going to explore something totally new next time, so stay tuned.

--

--

Lucas F. Lu

Laravel Expert, AI enthusiasts. Recently fell in love with helping people rebuild their lives by learning how to program.