Back to blog

[Loops and Iterations]

Hello HaWkers! Jeff Bruchado here.

This is the fourth article in our series that will cover our journey to Learn Programming from Scratch.

Well, no wasting time, let's get started!

Within the universe of programming, loops play a fundamental role. When you work on real projects, you'll use loops all the time.

This article aims to clarify what loops are, how to use them, and how they behave within the Programming and JavaScript environment.

Repetition loops, or loops, are structures that allow repeating a code block several times, just as the name suggests, loops keep resuming execution from the beginning of the loop until reaching the condition that stops it.

Loops

Thus saving lines and facilitating code maintenance.

This way, you allow your code to prevent repetition, increasing its maintainability.

How the For Loop Works

The for loop is one of the most used structures in programming.

With it, you can define the initialization, stop condition, and iteration in a single line.

Let's explore its nuances and characteristics, but first why are loops important?

Consider the following problem:

Problem

Do you agree with me that the writing above is ugly and repetitive? With loops we can solve this problem.

Right, having understood the problem, this is the basic syntax of the for Loop:

For Syntax

And knowing this, instead of writing repetitive lines, as written above, you can write as follows:

For Each

This way you can greatly increase the quality of your code, making it easier to understand, easier to maintain, and give some maintenance in the future.

This is the main problem that loops in programming languages solve.

Next, we'll understand the nuances and ways we can use different loops with different data types.

Loops

For...of Loop

The for...of loop facilitates iterating through iterable objects, like arrays and strings, without needing to worry about indices. Let's see how it works:

For Of

While Loop

The while loop creates an iteration that continues while the defined condition is true. It's extremely useful in situations where we don't know how many times we'll need to iterate.

This is the syntax of the while loop:

While

And here's an example of how to use it:

While Example

Infinite Loop: A Trap to Be Avoided

While loops are powerful tools, they can also be dangerous traps.

An infinite loop can freeze your program, creating an operation that never ends.

Infinite Loop

Exploring proper exit conditions and preventing the inadvertent creation of infinite loops is vital for writing robust and efficient code.

And HaWker, I need to clarify that this problem is more common than it seems.

Try to notice when you access a page and realize it stopped responding, there might be some infinite loop running in the background causing the browser memory to overflow 💥.

Explosion

Do-While Loop

Similar to while, the do-while loop ensures that the code block is executed at least once, regardless of the condition, checking it only at the end of each iteration.

This is the syntax of do-while:

Do While

And here's an example of how to use it:

Do While Example

Loop with Array

Arrays and loops are inseparable partners in programming.

When working with arrays in JavaScript, we have several ways to iterate over them, such as using the map, filter, and reduce methods, which facilitate array manipulation in a functional and declarative way.

But there's also another way, which is probably the most used, the famous forEach:

For Each

And here's an example of how to use it:

For Each Example

Using Loops with String Methods

Loops are not just for numbers or arrays; they can also be used with strings!

We can, for example, iterate over each character of a string using a for loop.

For with String

Using Loops with Asynchronous Functions

Sometimes, it may be necessary to use loops with asynchronous functions, and modern JavaScript allows us to do this elegantly with the help of async/await.

Let's see an example:

Async Await

Note: This code should be executed in an environment that supports async/await, and within an asynchronous function.

Continue Keyword within Loops

The continue keyword allows skipping the current iteration of a loop, continuing directly to the next iteration. It's a valuable tool for preventing unnecessary code execution in specific situations within a loop.

Let's observe an example where we want to log only odd numbers from an array:

Continue Example

In this example, we're using continue to skip code execution for even numbers, logging only the odd numbers.

Break Keyword within Loops

The break keyword, on the other hand, allows exiting a loop completely, interrupting all future iterations.

It's used to optimize code, avoiding unnecessary executions when we've already achieved the desired result.

Let's see this in action:

Break Example

In this example, the loop is interrupted as soon as i becomes 5, avoiding unnecessary iterations and exiting the loop early.

Nested Loops

Nested loops are loops placed inside other loops.

Although powerful, they should be used with care, as they can easily lead to performance problems, especially when working with large data sets.

Let's see how we can use nested loops to create a multiplication table:

Nested Loops

Here, we have two loops:

one for i and another for j. This allows us to multiply each combination of i and j, creating a multiplication table.

Loops and Recursion

Sometimes, we can use recursion, a process where a function calls itself, as an alternative to loops.

Recursion can help you develop cleaner and easier-to-understand solutions for complex problems.

Recursion

Conclusion

Well HaWker, as you can see, this is quite a dense article, and extremely important.

As you can see, mastering loops is fundamental for any developer.

They offer a powerful and flexible way to iterate over data sets, execute repetitive code, and create more complex and efficient algorithms.

I hope this article helped you better understand and use these essential control structures in your daily life as a programmer.

And remember, I'm here to help you every step of the way!

Awesome HaWker! 👏🏻

We've reached the end of our fourth article. 😮‍💨

And if you made it this far, please reply to this email with "I managed to understand how loops work!". So I know that this article series is being useful for you.

If you have any questions, please! Feel free to reply to this email with your question, it will be a great pleasure to help you.

Only a true HaWker makes it to the end.

Congratulations!

I'm happy to have you here.

See you soon, let's go! 🦅

- Jeff Bruchado

Congratulations on making it this far, HaWker! 🎉

Now that you've learned about loops and iterations in programming, how about testing your knowledge? Take the quiz below and see how much you absorbed from our article.

It's a great way to reinforce learning and prepare for the next steps in your programming journey.

Click to access the quiz.

Good luck and have fun!

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments