basiccodingconcept.online

What Is a Loop?

  • A loop is a programming structure that repeats a set of instructions until a specified condition is met.
  • Loops are commonly used in programming to replace repeated lines of code.
  • Loops save time and reduce error by running the same instructions automatically.
  • A loop makes programs easy to read because one loop can replace many repeated statements.
  • Example: To play 4 sounds, a loop runs the play instruction four times instead of writing four separate code lines, so all sounds are played.

 

Why Do We Need Loops?

  • Imagine you want to print numbers from 1 to 100. Without loops, you need 100 lines of code.
  • A loop can do the same task with about 3 lines of code by repeating the instructions.
  • Loops do repeated code, which reduces duplicate statements and keeps programs shorter.
  • Real-World Analogy: Think about a washing machine. It washes every piece of clothing using the same wash cycle instead of giving each item a separate instruction.
  • A loop repeats everything inside its block until the required condition changes.
08 · Loops — JavaScript Playground
08

Loops

Loops repeat work without copy-paste. A for loop counts from a start to an end.

JavaScript
Output

    

Types of Loops

 

What Is a for Loop?

The for loop is the most common loop when you know exactly how many times to repeat. It has three parts: Initialization (let i = 0), Condition (i < 5) that keeps looping while true, and Update that Changes the counter and Keeps it moving up. 

Example:
for (let i = 0; i < 5; i++)
{ console.log(i); }

What Is a while Loop?

A while loop keeps running as long as the condition is true. It is used when you do not know how many iterations are needed before the condition changes. 

Example: 

while (count < 5)

{ count++; }

What Is a do…while Loop?

A do…while Loop runs once at least, then checks the condition. Example: do { count++; } while (count < 5);

How Do You Loop Through Arrays?

Looping through Arrays is one of the most common tasks because loops uses going through each item in an array. Example: for (const item of array) { console.log(item); }

 

Controlling Loops with break and continue

 

Sometimes a loop needs to stop early or skip one iteration.

for (let i = 1; i <= 5; i++) {

  if (i === 3) break;

  console.log(i);

} 

1

2 

continue skips the current iteration and moves to the next one. Example:

for (let i = 1; i <= 5; i++) {

  if (i === 3) continue;

  console.log(i);

} 

Output:

1

2

4

5 

Common Loop Patterns

Many programming problems can be solved using a few common loop patterns that you’ll use repeatedly in real-world code.

Sum All Numbers

JavaScript

let numbers = [1, 2, 3, 4, 5];

let sum = 0;

for (let num of numbers) {

  sum += num;

}

console.log(sum); // 15

Find an Item

JavaScript

let names = ["Alice", "Bob", "Charlie"];

for (let name of names) {

  if (name === "Bob") {

    console.log("Found Bob!");

    break;

  }

}

Count Occurrences

JavaScript

let text = "hello world";

let count = 0;

for (let char of text) {

  if (char === "l") {

    count++;

  }

}

console.log("Letter 'l' appears " + count + " times"); // 3

Find the Largest Number

JavaScript

let numbers = [12, 45, 7, 89, 23];

let largest = numbers[0];

for (let num of numbers) {

  if (num > largest) {

    largest = num;

  }

}

console.log(largest); // 89

Find the Smallest Number

JavaScript

let numbers = [12, 45, 7, 89, 23];

let smallest = numbers[0];

for (let num of numbers) {

  if (num < smallest) {

    smallest = num;

  }

}

console.log(smallest); // 7

Build a New Array

JavaScript

let numbers = [1, 2, 3, 4];

let squares = [];

for (let num of numbers) {

  squares.push(num * num);

}

console.log(squares); // [1, 4, 9, 16]

Filter Values

JavaScript

let numbers = [1, 2, 3, 4, 5, 6];

let evens = [];

for (let num of numbers) {

  if (num % 2 === 0) {

    evens.push(num);

  }

}

console.log(evens); // [2, 4, 6]

Build a New String

JavaScript

let word = "javascript";

let result = "";

for (let char of word) {

  result += char.toUpperCase();

}

console.log(result); // JAVASCRIPT
Loops Quiz

Loops Quiz

Learn how to repeat actions efficiently with loops.

Question 1 of 10 Score: 0
0%

What Are the Key Takeaways?

  • Always know the number of iterations before using for loops, because for loops are great when the number iterations is known.
  • While loops are great when the unknown stop condition depends on changing data instead of a fixed count.
  • For…of provides the cleanest way to loop arrays, and it reads each array element in sequence.
  • Break exits a loop, while continue skips the next iteration without ending the loop.
  • Loops can stop eventually when the ending condition becomes true, and programmers make sure to avoid infinite loops that never stop.