The Concept of Using Loops in Coding

Lucas F. Lu
5 min readMar 3, 2020

--

If only there are ways to repeat some tasks in the programming world…

A guide for beginners of any background to start writing codes

Previously, we learned about what variables are, why they exist, how and why we use them. Let’s switch gears and take a look at this new instruction from the programming family — Loops.

So we have built this little program that greets whoever enters the system, but it stops once someone puts in his/her name.

What if we are building this program for a chat room and we want to greet everyone who comes into the room? Obviously, we are going to have more than one person join the chat and we are going to need something that repeats itself, greets people as they come in.

Introducing — the “loops”

As the word (Loop) itself represents, loops are instructions that repeat a group of commands that are defined within its scope.

We use them for occasions that need repeating, just like our example above which repeatedly asks people to enter their names as they come into the chat room and greets them afterwards.

To better understand loops, we need to firstly understand two concepts; condition of looping and the scope of a loop.

Condition of looping:

Looping conditions are simple. It simply means that we need a condition to start looping.

Like our example, we start looping through the process of asking for names only when someone enters the chat room. Conversely, nothing is going to happen when nobody enters the room. That is, the event of someone enters the chat room, triggers the loop to run.

Scope of a loop:

The scope of a loop is essentially the only contents to be executed by the loop. This is how we control what needs to be repeated and what’s not. Anything defined outside the scope is not repeated. For our example, asking for names and greeting them are the only tasks to be repeated.

We use curly brackets (“{“ and “}”) in most languages (with some exceptions like python) to group lines of codes. Anything within the curly brackets is the scope of that group. When put under a loop, it becomes the scope of that loop. When put under other instructions, it becomes the scope of that instruction.

Now we know what loops are and why we need them, let’s find out how we can use it.

Adding loop to our program

The syntax of using loops is different among different languages, but with your understanding of the concept, learning the syntax is considered — Piece of cake.

In our example, we are going to use a loop called “while loop”, and in PHP, while loop is written like below.

While (condition of loop){    Scope of the loop ...}

After implementing the while loop into our previous program, the result will look like the following.

<?php    While (true)    {        $names=readline('What is your name: ');        echo 'Good morning master '.$names;    }?>

Whoa!? What’s going on?

There are certainly a lot of things going on that we haven’t covered yet. Please bear with me, they are actually quite simple to understand. You’ve learned most of them actually, just the matter of connecting the dots.

Firstly, what the heck is “true”?

Like we mentioned before, a loop needs a condition to start. In the mind of computers, conditions are translated into either “true” or “false” while in our mind, that is translated into “run” or “not run”. So in our case, we gave the loop a condition of “run” to start.

This condition of loop could also be something else, like “run only when it’s morning” or “run when guest number is less than 100”. This flexibility better helps us control when loop starts and subsequently controlling the event at which the scope of the loop to be executed.

Notice in our example, we gave the loop a condition of “run”. Meaning there are no restraints, just go ahead and “run, no matter what”. Certainly, this loop will never stop, unless we terminate the program.

But for now, this is what we need.

What about the delimiters?

Why don’t we have a delimiter at the end of the line which “while” keyword was used?

The reason is simple, because we are not finished on defining the “while loop” instruction. We need computers to keep on reading for what’s written in between the brackets as part of the while instruction.

Note: ideally, we should have put a delimiter (;) at the end of the closing bracket to tell the computer that the instruction ends here. It makes logical sense right?

Turns out, having delimiter after a closing bracket seems logically redundant. We don’t need another sign to indicate the ending of an instruction since closing brackets already does the same thing.

What are all these brackets?

The opening curly bracket ( { ) right below the keyword “while” signifies the beginning of the scope for the loop. Telling the computer that anything below until you see the ending curly bracket, are the only codes to be executed by this loop.

Following the bracket, we have our old friends that take care of the name taking and greeting business.

At the very end, we have the closing curly bracket ( } ) that signifies the ending of the scope for the loop. Telling the computer to stop executing what’s written below and repeat from the beginning until condition defined within the “while” keyword fails.

Note: Our loop thus far, will run forever since we didn’t give it a condition to stop. In the further article, we will touch on ways to stop a loop. For now, press on the keyboard, Control (Ctrl) + “C” in the command line window to stop the program.

Summary

  • We use loop when we want to repeat certain actions within program
  • A loop needs a condition to start
  • The scope of the loop is the only group of codes ran by the loop
  • We use curly brackets (“{“ and “}”) to define scopes
  • Conditions in computer are translated into “true” or “false” like (“run” or “not run”)
  • A condition of loop without restraints will run forever

I hope I did a pretty good job at explaining the concept of looping. I’m more focused on getting people to understand why we need to use loop than teaching how. So let me know if you need more help grasping this idea.

In the next article, we will further explore the tricks of using loops and several varieties of loops.

--

--

Lucas F. Lu

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