Loops

When writing programs, it’s always a good idea to save more time, write cleaner code, and be more efficient. Loops help us do these, and more.

With loops, you’ll write code that:

• Saves you time
• Helps your program be more efficient
• Helps you avoid a ton of errors and mistakes

Here’s how: Give a set of instructions to a computer that lets it repeat the instructions for a specified number of times.

You can do this by using a type of control structures called Loops.

Loops let you write the instructions once, so that a computer can execute those instructions multiple times.

In other words, loops let computers execute instructions multiple times, without you having to manually write out the instructions for each execution.

Each execution of the instructions is known as an iteration (or each execution of a loop). The number of iterations is also called the number of repetitions.

So how do we create a loop?

First, let’s do a recap of what we’ve done.

So far, we’ve learned how to:

Let’s now go on to see how to create the various types of loops that we have.

Types of Loops

1. For Loop: Repeats instructions for X number of times

2. While Loop: Repeats instructions until a certain condition changes

3. For Each Loop: Repeats the instructions for each item on a list… or in a collection

Let’s now look closely at each type of loop.

1. For Loops (or Count-controlled Loops)

In a for loop, we give the instructions to the computer once, while telling the computer the number of times to repeat the instructions.

What happens when a computer receives a for loop instruction?

• Step 1: The computer sets the counter at 0 and performs the instructions in the body of the loop
• Step 2: After executing the set of instructions one time (that is, after the first iteration), the computer moves the counter from 0 to 1
• Step 3: The counter’s now at 1 and so the computer goes through Steps 1 & 2 again, in this case, to move the counter from 1 to 2
• Step 4: … This goes on and on, until the desired number of iterations is completed

Here’s an example of a for loop, written to be executed seven times:

``````for (i = 0; i < 7; i++) {
placeDay('monday')
placeDay('tuesday')
placeDay('wednesday')
}``````

Note: i++ is the same as i = i + 1; which increases the value of variable i by one for each iteration.

2. While Loops(or Condition-controlled Loops)

In a while loop, the computer repeats a set of instructions while a certain condition is valid. Hence, the name “while loop.”

We use while loops when we don’t know the number of times (or number of iterations), but we know when to stop.

‘When to stop’ is the condition that controls the while loop. Hence the name “condition-controlled) loop.”

What happens when a computer receives a while loop instruction?

• Step 1: The computer checks if the condition is valid. If it is, the computer executes the tasks in the body of the while loop
• Step 2: The computer goes through Step 1 again and repeats the execution
• Step 3: … This goes on and on, until the condition is no longer valid; at which point, the computer stops executing the tasks

CAUTION!
Watch out for while loops where the condition is always true. If the condition is always true, the loop will never stop… and then you’ll have an infinite loop – you typically don’t want that.

Here’s an example of a while loop, executed six times:

``````let i = 0;
while (i < 6) {
placeDay('monday')
placeDay('tuesday')
placeDay('wednesday')
i++
}``````

3. For Each Loops (or Collection Loops)

In a for each loop, the computer repeats the instructions for each item in a list (or in a collection).

Typically, we use a for each loop when we need to execute a task for every item on a list, or when the order of the items in the list needs to be maintained.

To use a for each loop, we create a sequence of the order (or pattern) that we want, and then tell the computer (using the loop) to repeat the instructions for each item in the sequence.

Here’s an example of a list for a for each loop:

``bagPack = ['laptop', 'pen', 'calculator', 'notebook', 'thumbdrive']``

And here’s its for each loop:

``````For each item in bagPack:
placeStuff(item)``````

Here, item changes with each iteration.

That is, for the first iteration, the value of item would be ‘laptop’…. ‘pen’ for the second iteration… ‘calculator’ for the third iteration, and on and on.

Apart from a list, we can also use other similar data types.

This group of data types is what we call a collection, hence the for each loop is also known as a collection loop.