Lesson 9 — Loops

A simple intro to JavaScript

Previous :: Next

It’s Easy as 123

Sometimes when you code you want things to repeat, so that you don’t have to write code over and over again. You would use a loop to

for Loops

for loops do something a certain number of times, and are set up like this.

for (statement1; statement2; statement3) {
//code
}

Statement1 occurs before the code starts and has a new variable being created that starts at a specific number.

Statement2 is what you use to check if the loop should run.

Statement3 occurs right after the loop finishes each time. This is where you change the variable in statement1 so that the loop continues on.

alert(“Lets count to 5!”);
for (var i = 1; i < 6; i++) {
alert(i);
}

Here we set up a variable i in statement1. The computer then yes to statement2 and checks if i is less than six. If it is, then the computer goes into the for loop and executes the code inside of it. After the computer has gone through all the code, it goes to statement3, where it increases the value of i by one. And finally, it goes back to statement2 and loops back around until i is equal to six.

You could also replace statement2 to make it look something like this, and it would still work the same way.

var maxNumber = 6;
alert(“Lets count to 5!”);
for (var i = 1; i < maxNumber; i++) {
alert(i);
}

while Loop

while loops are loops that check if something has happened and if it hasn’t then it will continue to loop through. A while loop will always test the condition first before it goes into the loop.

while (condition) {
//code
}

The condition is where the loop checks if it should go and continue looping or stop and move on.

var windows10 = confirm(“Would you like to download Windows 10 now?”);
while (windows10 != true) {
windows10 = confirm(“Are you sure that you don’t want to download Windows 10 now?”);
}

Here the while loop keeps going through until the user accepts to download Windows 10 (thanks, Microsoft).

do…while Loops

Unlike a while loop, a do…while loop goes through the code inside of it once before it checks if it should repeat the process.

do {
//code
} while (condition);

Here the condition, or the check to continue repeating the loop, is at the end. This means that the computer will always execute the code once before it checks if it should loop back around.

var windows10
do {
windows10 = confirm(“Would you like to download Windows 10 now?”);
} while (windows10 != true);

Inception, but Less Confusing

Another way to loop through things is by using recursion, or in simpler words, it would be where you call a function inside of another function in order to repeat a task. For example, you could use this method to write a factorial program.

function factorial(num){
if (num < 0) { // If the number is less than 0, reject it.
return -1;
} else if (num == 0) { // If the number is 0, its factorial is 1.
return 1;
}
var tmp = num;
while (num > 2) {
tmp = tmp * num;
num--;
}
return tmp;
}

var result = factorial(8);
alert(result); // Output: 40320

Gimme a Break

Sometimes you might want to exit a for,while, or do…while loop in the middle of it. For this you would use the break; statement. When the computer reads this, it automatically exits the loop and continues on. While this might work for most of the loops, it does not work for loops using recursion.

Just Continue On

Well, guess what? There’s a statement for that also. It’s called continue; and instead of breaking out of a loop, it only skips one iteration of a loop.

Previous :: Next