mikereeve
3/23/2020 - 12:06 AM

## Loops

For Loops

I want to do something that I count the number of times I do it // let is similar to var but makes it smaller // we initially start is at 0 then i++ will increment it by 1 `let i = 0`

How do we know when to stop the loop? Another way to make a decision much like an if statement. Here the decision is, should I continue the loop or should I stop the loop. We are going to reevaluate the experession each time we do a loop `i < students.length` //called a test statement

//In the second example it will go over a list of values `for (let student of students)`

While Loops

While loop except we set up an initial value nor do we increment. Its only a single clause and evalutes true or false. The while loop says keep going until it evaluates to true. We are not counting any more because its not a for loop. With a While loops we are checking if students.length still has items in it that is because we are saying > 0. We are actually pulling the value out of the array. It grabs or removes the value (in this case "Matt" or "Susan") in the array causing the array to shrink in the number of values in the array. The while loop continue to ask if there are any values left in the array causing it to be FALSE and then stopping the loop.

``````var students = ["Mat", "Jami", "Sarah", "Susan"];

for (let i = 0; i< students.length; i++) {
greetStudent(students[i]);
}

for (let student of students){
greetStudent(student);
}

``````
``````var students = ["Matt", "Sarah", "Susan" ];

while (students.length > 0 ) {
let student = students.shift();  //shift will cause to take the value from the front of the array.
console.log(`Hello,  \${student}!`); //pop will take the value from the back of the array.
}``````