# Loops

The next topic we'll go over is loops. You can think of loops as a way to tell the computer to do something over and over again. If you ever had to clean up your room every week, wouldn't it be great if you could clean your room once and then have a computer do it exactly the same way over and over again. That's basically what a loop does, you write a block of codes and wrap it in a loop which tells the computer to execute it as many times as you like. In terms of why you would do this in a program, you would write a loop to go through a list of users and identify which ones you should send an email to. So let's take a look at a basic loop:

Copy code
      
    

So this is a basic loop that runs 10 times. The way the computer knows to loop 10 times is because of this part: (let x=0; x<10; x++). Let's break it down, the first part is the pre-condition for the loop: let x=0;. This means we start the loop with a variable x initialized to 0. The next part is the post-condition that tells us when to end the loop: x<10. And the last part is the increment expression that run on each loop: x++. So each time the loop runs, the variable x is increased by 1. And when x reach 10 the loop stops.

Next: Functions

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.