5️⃣

4.5 While Loop

4.5 While Loop

The while loop loops through a block of code as long as a specified condition is true.

Structure

while (condition) { // code block to be executed }

Example

In the following example, the code in the loop will run, over and over again, as long as the variable, i, is less than 10:
while (i < 10) { text += "The number is " + i; i++; }
⚠️
If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.

Practice

You can add practice problems here if you want. Follow the format below.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.