By the end of this lesson, you'll be able to:
:information_source: Info What is a Loop? A loop is a block of code that runs over and over again until a certain condition is met. Think of it like a robot that keeps doing the same task until you tell it to stop!
Imagine you have food to deliver to customers. This is how a loop works:
note In programming, we call each time through the loop an iteration. It's like counting how many times you've done the same task!
Every loop has three important parts:
Let's use our food delivery example:
tip Why Loops Matter Computers are amazing at repeating tasks thousands or even millions of times without getting tired. This is what makes them so powerful!
The for loop is perfect when you know exactly how many times you want to repeat something.
for (initialization; condition; update) {
// code to run when condition is true
}
Here's what happens step-by-step:
Let's count from 0 to 100 using a for loop:
for (var i = 0; i <= 100; i++) {
console.log(i);
}
Here's what happens in this code:
i
and set it to 0i
less than or equal to 100? Yes! (``0 <= 100``)i
(prints 0)i
(now i
= 1)i
reaches 101i
= 101, the condition (``101 <= 100``) is false, so the loop stopsnote Quick Tip
The expression i++
is shorthand for i = i + 1
. It adds 1 to the variable!
You can make your loop count by different amounts!
Count by 10s:
for (var i = 0; i <= 100; i += 10) {
console.log(i);
}
// Output: 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
Count backwards:
for (var i = 100; i >= 0; i -= 10) {
console.log(i);
}
// Output: 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0
tip
i += 10
means "add 10 to i" and i -= 10
means "subtract 10 from i"
You can control your loop with conditional statements:
for (var car = 1; car < 10; car += 2) {
if (car === 7) {
break;
}
console.log('The number of cars: ' + car);
}
Output:
The number of cars: 1
The number of cars: 3
The number of cars: 5
:information_source: Info About Break The
break
statement is like an emergency exit for your loop. When the program seesbreak
, it immediately stops the loop and moves on to the next part of your code.
Learn more about break
: https://www.w3schools.com/js/js_break.asp
The while loop is great when you don't know exactly how many times to repeat something. It keeps going as long as a condition is true.
while (condition) {
// Run loop when condition is true
}
note Key Difference Unlike the for loop, the while loop doesn't have initialization or update built into its syntax. You handle these separately!
You can write a while loop to do the same thing as a for loop:
var i = 0;
while (i <= 100) {
console.log(i);
i++;
}
This prints numbers 0 to 100, just like our for loop example!
While loops shine when you don't know how many times you'll need to repeat. Here's a real example:
var number = 100;
while (number > 5) {
number = number / 3;
console.log(number);
}
Output:
33.333333333333336
11.111111111111112
3.703703703703704
tip When to Use While Use a while loop when you're waiting for something to happen, like:
Sometimes you need your code to run at least once before checking if it should continue. That's perfect for a do...while loop!
do {
// Run at least once
} while (condition)
:information_source: Info How It Works
do
blockThe key difference: This loop always runs at least once!
Here's a program that adds up positive numbers until you enter a negative one:
var sum = 0;
do {
var number = +prompt("Enter a number: ");
sum = sum + number;
} while (number >= 0);
console.log("The sum is " + sum);
note
The +
before prompt
converts the text input into a number!
Loop Type | When to Use | Syntax |
---|---|---|
for | You know exactly how many times to repeat | for (init; condition; update) { } |
while | You don't know how many times to repeat | while (condition) { } |
do...while | You need to run at least once before checking | do { } while (condition) |
:information_source: Info Summary
Try these exercises to master loops:
Remember: The best way to learn loops is to practice writing them yourself!