JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • While Statement Loops
  • Difference Between For and While Loops
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 3-Loops

While Loops

While Statement Loops

It is possible for a while loop to stand without the do. Including the do guarantees that the statement inside the while will process once. It is wise to take care with stand-alone while loops, as they have a natural tendency to go infinite.

The while loop executes its statements if the designated condition (i.e. n < 3) reads as TRUE. If the condition reads FALSE, the statement within the loop stops executing and control passes on. The condition is checked before the statement (i.e. n++; x += n;) is executed, at which point, the condition is checked again. If the condition returns FALSE, execution stops and control passes on before the statement is executed. To execute multiple statements, use a block statement ({...}) to group.

//While Loops

//Create a variable
var score = 0;
        //Set a condition in parens
while(score < 10){
    //Set an increment operation
    score++;
    //Print to the console
    console.log("Score: ", score);
}

//Another example
var age = 0;
while(age < 100){
    age+=10;
    console.log("Age:", age);
}

if (age === 100){
    console.log("I made it!");
}

//A challenge -create a while loop that prints 10-100 by 10s. AT 50
// print "Halfway there!"

var counter = 0;
while(counter < 100){
    counter+=10;
    if(counter === 50){
        console.log("There's halfway");
    } else {
        console.log(counter);
    }
}

Difference Between For and While Loops

// for loop

var cars = ['BMW', 'Volvo', 'Saab', 'Ford'];
var i = 0;
var text = '';

for (;cars[i];) {
  text += cars[i] + '<br>';
  i++;
}

// while loop

var cars = ['BMW', 'Volvo', 'Saab', 'Ford'];
var i = 0;
var text = '';

while (cars[i]) {
  text += cars[i] + '<br>';
  i++;
}
PreviousDo WhileNext4-Functions

Last updated 7 years ago