Definite vs Indefinite Looping

Jordan Panasewicz
5 min readMay 3, 2021

Even if you’re new to coding, chances are you’ve already taken a dive into looping. There are infinite different use cases for looping, the main idea being that it is a way to repeat something cyclically until a certain condition is met.

In general, there are two main types of loops: Definite and Indefinite.

Definite loops repeat a certain set of instructions a certain number of times, and are implemented using count control — these are your For, For..In, and For..Of loops. Indefinite loops repeat a certain set of instructions until a specific condition is met — these are your While, and Do..While loops.

Now there are a few other important looping concepts, like labeled, break, and continue statements, but we will cover those in a future post.

Definite Loops

What essentially defines a Definite Loop is that the number of iterations is known before we start the execution of a loop body. It might not be the person writing the code who knows the number of iterations to happen, but there’s always a known number, like array.length for example, that will end the loop.

The “for” loop

The standard for loop executes a code block a specified number of times.

The loop and it’s conditions are laid out as follows:

for( Initialization; Terminating Condition; Increment/Decrement ){
. . .
}

In a for loop, we declare an initialized variable and it’s value, a condition of that variable to end our loop, and a way to increment or decrement the variable.

For example:

for( let i = 0; i <= 3; i++){
console.log(`i is now equal to ${i}`
}

The initialized variable is i = 0, we want to count to 3 so we set the terminating condition to i is less than or equal to 3, and we increment i by one each time we loop. So i starts at 0, will terminate our loop as soon as it becomes greater than 3, and increments by 1 with each loop.

Our output will look like

i is now equal to 0
i is now equal to 1
i is now equal to 2
i is now equal to 3

The “for..of” loop

The for..of loop is somewhat similar, but is used to loop over an iterable item, like an array or a string. We declare a variable that will be continually be reassigned the next value in the iterable item until the loop has reached the end of that item.

It’s basic syntax is as follows:

for( initialized variable of iterable ) {  
. . .
}

Unlike a standard for loop, there is no counter or increment made, this will loop through an entire array or string and perform the code block with each iteration.

For example:

const countdown = [3, 2, 1, "Happy New Year!" ]for( const item of countdown ){
console.log(item)
}

In this example item is our initialized variable that is reassigned to the next value in our iterable, in this case countdown. So item starts at the 0 index of countdown, will run the code block for every item in countdown, and will terminate when it reaches the end of countdown.

Our output will look like:

3
2
1
Happy New Year!

The “for..in” loop

Moving along, the for..in loop is somewhat similar to the for..of loop, however the initialized variable iterates through the object literals keys, and accesses its values.

It’s basic syntax is as follows:

for( initialized variable in object ) {  
. . .
}

Again, there’s no counter or increment needed. We initialize a variable which will loop through an entire objects keys, perform the code block with each iteration, and end as soon as it’s accessed the entire object.

For example:

const swaggerWagon = {
year: 1992,
make: "subaru",
model: "outback",
condition: "very mediocre looking, but runs well" }
for( const key in swaggerWagon ) {
console.log(swaggerWagon[key])
}

Note how with the for..in loop we are actually looping through an objects keys, and we still have to access it’s VALUES with the [bracket notation].

Here our output is:

1992
subaru
outback
very mediocre looking, but runs well

Indefinite Loops

On the other side of the spectrum we have Indefinite Loops, defined by the idea that there is an unknown number of iterations to happen, but the loop will stop once a certain condition becomes true.

The “while” loop

The common while loop simply executes the instructions each time the condition specified evaluates to TRUE. A terminating condition must be declared at the beginning of the code block in order to help prevent an infinite loop.

(Note: it‘s possible to find yourself in an infinite loop with ANY of these looping methods).

It’s basic syntax is as follows:

while (terminating condition) {  
. . .
}

So while the code block terminating condition is true, the loop will continue on. As soon as the terminating condition is FALSE, the loop will end. It is important to adjust SOMETHING in your while loop to avoid an infinite loop.

For example:

let n = 0;while (n < 3) {
n++;
}
console.log(n);

In this example we declare a counter as n, and using a while loop with the terminating condition “n < 3”, so this loop will continue until that is FALSE, or n becomes greater than 3. We MUST change the value of n inside of our loop, otherwise we will enter an infinite loop. Here we simply increment our variable n by 1 with each loop. Because our console.log is outside of the loop, our output is simply the final value of n when our terminating condition is no longer true:

3

The “do..while” loop

The do..while loop is very similar to the while loop, with one exception; it runs at least one time, no matter what. This is because the terminating condition is declared AFTER the initial loop is set.

It’s basic syntax is:

do {  
. . .
}
while (terminating condition);

Let’s replicate the while loop example using a do..while loop:

let n = 0;do {
n++;
} while (n < 3)
console.log(n);

This example is no different, because n starts at 0, is incremented with each loop until the same terminating condition is met, so our output is the exact same as with the while loop.

The MAJOR difference with the do..while loop is that it will run at least one time, even if the terminating condition is triggered.

For example:

let n = 3;do {
n++;
} while (n < 3)
console.log(n);

In this case, our terminating condition is already false, as n is not less than 3. The code block executes, so n increments to 4, and then the terminating condition gets recognized, so our output is now:

4

Conclusion

Obviously there’s a lot of power in looping, and a lot of tasks can be performed using many of the different options in different ways.

I definitely recommend playing around with the different looping methods to see what use cases seem most valuable and intuitive to you!

Cheers, and I’ll see you again soon!

--

--

Jordan Panasewicz

Denver, CO based. Software Sales turned Software Engineer.