Loops Explained

Robert Long
4 min readAug 28, 2020

--

The loop is a fundamental control structure in programming languages that allow you to repeat a body of code. It is useful for dealing with lists and indefinite conditions.

You may think that indefinite loops can cause problems, but at the heart of every UI application, there is a loop that keeps the application running until the user decides to close it. Even frameworks like NodeJS take advantage of event loops.

Overview of Loops

There are 3 common variations: the while loop, the for loop, and the less common foreach loop.

  • while loops are used for generic purposes it can handle lists and indefinite conditions
while(input != "exit") {
... body of code...
}
  • for loops are somewhat of an extension of the while loop. Instead of having just the condition section, it also introduces 2 additional elements to make handling the loops more convenient: the initializer section and the post operation section.
for(int i=0; i<items.length; ++i) {
... body of code...
}
  • foreach loops are similar to the for loops, but they are specifically targeted for collection and lists and do not have a conditional section. It has an initializer section followed by the in keyword and then the list that you would like to iterate though. The initializer section automatically declares a variable and points it to the current item in your list and is made available to use in the body of your loop.
foreach(var page in pages){
... body of code ...
}

While Loop Details

The while loop is the simplest of these three, so let’s take it a look at it. It consists of two parts:

1. Condition — when this is true, the body of the loop will execute.

2. Body — the lines of code to execute after the condition check passes.

The while loop can be used in place of the for loop and the for each loop, but will require more lines of code to do the same work that those control structures already provide.

For Loop Details

The for introduces 3 sections allowing you to introduce variables straight into the body of your loop so you can use it.

A common way to use it is to initialize an integer to 0, this is typically called i for index. As it iterates through, it increments the index by 1. In other words, i can behave like a zero-based counter that points to the current item in your list as it loops through your list. Alternatively, you can initialize your counter to point to the end of the list and let the post operation decrement the counter; in this way, you would be looping through the list backwards.

The for loop is not limited to just lists, because it is flexible, you can create any variable, conditions, or post operations you like.

credits to https://intully.com

The for loop’s structure is broken into 4 parts:

  1. Initializer — initializes variables for use in the body of the for loop
  2. Condition — when the condition is true, it will execute the body. If left empty, it will be assume as true and run indefinitely.
  3. Post Operation — after one iteration, it will execute the post operation. ++i means i = i + 1; essentially, increment i by 1.
  4. Body — the body is the procedure that will execute when the condition is true

Foreach Loop Explained

The foreach loop is an extension off of the for loop, but it is particularly useful for collections. The foreach loop will iterate through each item in the list and automatically create a variable of that type for you to use. This reduces the number of parts in a for each loop. Unlike the for loop and while loop, there is no condition here, instead this loop will take in an a collection through which it can iterate through.

Both the while and the for loop could simulate the foreach loop, but it would need to introduce an index i variable, declare and refer to the current element in the list, and increment the index after each successive iteration. The foreach loop automatically handles this for us.

  1. Initialize — during the iteration, it will create a variable and point to the current item in the iteration.
  2. Collection — list of items that can be iterated through. Iterable is the ability for the foreach loop to run through the list. Arrays and lists are typically iterable in most languages.
  3. Body — the lines that will execute for each item that we iterate though

--

--