Research Looping - mikehov/Dating-app GitHub Wiki

Looping Research

Mike Hovenier | Tech-4

Looping

A loop is a block of code that will repeat itself till its goal/condition. You have different kind of types off looping.

  • For loop
  • For-in loop
  • For-off loop (ES6 only)
  • While loop
  • Do-while loop

For loop

You will use a for loop if you know how many times you want to loop through something, so you know the answer. A for loop is more condensed than a while loop (so less code). The for loop loops through a block of code until the counter reaches a specified number.

This is a example of a for loop:

for (let i=0; i < 5; i++) {
  console.log('Hello World');
}

The answer for this example is five times Hello World in the console. 0 < 5 = true +1

1 < 5 = true +1

2 < 5 = true +1

3 < 5 = true +1

4 < 5 = true +1

5 < 5 = false loop stops.

So you will use a for loop if you know the answer. A for loop is also more condensed then a while loop.

A for loop has three sections: initialization — what are the current values. var = 0; condition — the statement and rules. for (let i=0; i < 5; i++) increment — the updater of the loop. console.log('Hello World');

For-in loop

This is a loop that works with strings or indexes instead of numbers.

// An array with some elements
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
 
// Loop through all the elements in the array 
for(var i in fruits) {  
    document.write("<p>" + fruits[i] + "</p>");
}

So this code will loop through all the elements till it looped through them all.

For-off loop

This loop is very new. It also works with arrays.

// Iterating over array
let letters = ["a", "b", "c", "d", "e", "f"];

for(let letter of letters) {
    console.log(letter); // a,b,c,d,e,f
}

// Iterating over string
let greet = "Hello World!";

for(let character of greet) {
    console.log(character); // H,e,l,l,o, ,W,o,r,l,d,!
}

While loop

You will use a while loop if you don't know how many times it will loop, so you don't know the answer. While loops aren't really get used that much. The while loop will continue looping, while the statement is true. This is a example of a while loop:

let x = 0;
let y = 0;

while (x < 4) {
  x++;
  y += x;
}

So what happens here is that this loop will stop after four times. 0 < 4 = true +1

1 < 4 = true +1

2 < 4 = true +1

3 < 4 = true +1

4 < 4 = false loop stops

x will be 4 and the y will be 10. The y is 10 cause of this line y += x; x will keeps adding the value to y till x is equal or higher than 4.

Always be careful with infinite loops:

let i = 0;

while (i < 5) {
  console.log(i);
}

This loop will never stop cause the answer will always be 0, this is really bad for you computer.

Do-while loop

The do-while loop is almost the same as the wile loop. The only big difference is that do-while loop always runs once. The while and the do-while loop will both keep running if the statement is true. But let's say the statement is false at the beginning, the while loop will then not run, but the do-while loop will run once. Cause the statement is at the end of the line and not the beginning. Example for a do-while loop:

var i = 1;
do {
    document.write("<p>The number is " + i + "</p>");
    i++;
}
while(i <= 5);

This code will keep running till its equal or higher than five.

Sources

JavaScript While, Do-While, For and For-In Loops - Tutorial Republic. (n.d.). Retrieved May 24, 2020, from https://www.tutorialrepublic.com/javascript-tutorial/javascript-loops.php

Richards, A. (n.d.). The Difference Between For Loops and While Loops in JavaScript. Retrieved May 24, 2020, from https://levelup.gitconnected.com/the-difference-between-for-loops-and-while-loops-in-javascript-6029f45faeba

The Coding Train. (n.d.). 4.1: while and for Loops - p5.js tutorial. Retrieved May 24, 2020, from https://www.youtube.com/watch?v=cnRD9o6odjk