🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  • While Loop
  • Syntax
  • Example 1 - Infinite Loop
  • Example 2 - Until a Value
  • Do While Loop
  • Syntax
  • Example 3 - Not Zero
  • Example 4 - Print a Box
  1. Unit 3: Control Flow
  2. Loops (Repeating Code)

While & Do/While Loops

PreviousFor...LoopNextDebugging

Last updated 6 years ago

While Loop

Probably the simplest loop structure, this code block will run if and only if the condition is true. Once it becomes false, the loop stops. You can also break out of the loop with a break; statement. Keep in mind that the while loop may not run at all, depending on the condition.

Syntax

The syntax of a While loop is some of the simplest possible:

while (someCondition) {
    // Do anything in here
}

Example 1 - Infinite Loop

// This will loop forever (infinite loop) 
// unless you call 'break;' somewhere inside the loop block
// Infinite loops are VERY bad as you have no ability to properly end it
while (true) {
    console.log("INFINITE LOOP!");
}

Example 2 - Until a Value

Do While Loop

The Do While is a variation on the While. It is guaranteed to run at least once but it will only loop back to the top if the condition is true.

Syntax

The syntax is similar to the While loop but notice the differences - especially the semicolon ending the statement.

do {
    // Some code that is guaranteed to run once
} while (someCondition);

/*** Note that there is a semicolon at the end of the statement.  ***/

Example 3 - Not Zero

// Add numbers the user enters until they enter zero

// Assuming good input from the user
let userNumber, sum = 0;

// loop body is executed at least once
do {
    userNumber = Number(prompt("Please enter a number:"));
    sum += userNumber;
} while (userNumber !== 0);

console.log("Total sum was %i", sum);

// You might not recognize the console.log on line 12. That is a different
// way of including a variable into a string (it only works in console.log).

Example 4 - Print a Box

Clicking the blue "Powered By" button will open this example in a new tab
Clicking the blue "Powered By" button will open this example in a new tab