Loop Control Statements (break and continue)
In loops, you sometimes need more control over how and when the loop should run. JavaScript provides two special keywords that help you manage the flow inside loops:
break ? stops the loop completely
continue ? skips the current loop step and moves to the next step
These two statements are powerful and widely used in real projects. For college students and freshers, learning loop control is important because it helps solve complex logic problems easily.
1. break Statement
The break statement stops the loop immediately, even if the loop condition is still true.
Example 1: Stop the loop at number 5
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
Output:
1
2
3
4
As soon as i becomes 5, the loop stops completely.
Example 2: Stop when a matching value is found
let numbers = [10, 20, 30, 40, 50];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === 30) {
console.log("Found 30 at index", i);
break;
}
}
Output:
Found 30 at index 2This is useful for searching items in lists.
2. continue Statement
The continue statement skips the current iteration of the loop and proceeds to the next.
Example 1: Skip number 5
for (let i = 1; i <= 7; i++) {
if (i === 5) {
continue;
}
console.log(i);
}
Output:
1
2
3
4
6
7
The number 5 is skipped, but the loop continues.
Example 2: Print only even numbers
for (let i = 1; i <= 10; i++) {
if (i % 2 !== 0) {
continue; // skip odd numbers
}
console.log(i);
}
Output:
2
4
6
8
10
continue makes filtering values easier.
break vs continue (Simple Comparison)
| Keyword | What It Does |
|---|---|
| break | Exits the loop completely |
| continue | Skips the current iteration and continues the loop |
Real-Life Example: Searching in a List
let students = ["Riya", "Karan", "Aman", "Neha"];
for (let i = 0; i < students.length; i++) {
if (students[i] === "Aman") {
console.log("Aman found!");
break;
}
}
Output:
Aman found!
The loop stops as soon as "Aman" is found.
Real-Life Example: Skip Blank Values
let items = ["Pen", "", "Book", "", "Pencil"];
for (let i = 0; i < items.length; i++) {
if (items[i] === "") {
continue; // skip empty values
}
console.log(items[i]);
}
Output:
Pen
Book
Pencil
This is useful when working with form data or user input.
Common Mistakes Beginners Make
Using
breakaccidentally instead ofcontinueCreating infinite loops by forgetting increments
Putting conditions incorrectly
Skipping important code by using continue at the wrong place
Be careful where you place these statements.
Example Program With Output
for (let i = 1; i <= 10; i++) {
if (i === 3) {
continue; // skip 3
}
if (i === 8) {
break; // stop at 8
}
console.log(i);
}
Output:
1
2
4
5
6
7
3 is skipped
Loop stops completely when 8 is reached
Practice Tasks (Do It Yourself)
Print numbers 1 to 20, but skip multiples of 4.
Print numbers up to 50, but stop at 30.
Find the first number divisible by 7 in an array.
Skip negative numbers and print only positive ones from a list.
Print student names but skip empty entries.