In programming, loops allow us to repeat actions multiple times. For example, you might need to output items from a list one by one or execute the same code for each number from 1 to 10. Here, we'll focus on basic loops: while, do..while, and for.

Posted At: Aug 03, 2024 - 119 Views

Loops: while and for JavaScript & TypeScript

Loops: while and for

In programming, loops allow us to repeat actions multiple times. For example, you might need to output items from a list one by one or execute the same code for each number from 1 to 10. Here, we'll focus on basic loops: while, do..while, and for.

The while Loop

The while loop repeatedly executes a block of code as long as a specified condition is true.

Syntax:

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

Example in JavaScript:

let i = 0;
while (i < 3) { // loops until i is less than 3
  console.log(i); // 0, 1, 2
  i++;
}

Example in TypeScript:

let i: number = 0;
while (i < 3) { // loops until i is less than 3
  console.log(i); // 0, 1, 2
  i++;
}

Key Points:

  • The condition is evaluated before each iteration.
  • If the condition is false at the start, the loop body is never executed.

The do…while Loop

The do…while loop is similar to the while loop, but the condition is evaluated after the loop body is executed. This ensures that the loop body runs at least once.

Syntax:

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

Example in JavaScript:

let i = 0;
do {
  console.log(i); // 0, 1, 2
  i++;
} while (i < 3);

Example in TypeScript:

let i: number = 0;
do {
  console.log(i); // 0, 1, 2
  i++;
} while (i < 3);

Key Points:

  • The loop body is executed once before the condition is evaluated.
  • If the condition is false after the first iteration, the loop exits.

The for Loop

The for loop is the most commonly used loop. It includes initialization, condition, and increment/decrement in a single line.

Syntax:

for (initialization; condition; increment) {
  // code block to be executed
}

Example in JavaScript:

for (let i = 0; i < 3; i++) {
  console.log(i); // 0, 1, 2
}

Example in TypeScript:

for (let i: number = 0; i < 3; i++) {
  console.log(i); // 0, 1, 2
}

Key Points:

  • Initialization: Executes once before the loop starts.
  • Condition: Evaluated before each iteration; if false, the loop ends.
  • Increment: Executes after each iteration of the loop body.

Skipping Parts of a for Loop

You can omit parts of the for loop if they are not needed.

Example:

let i = 0;
for (; i < 3; i++) {
  console.log(i); // 0, 1, 2
}

Example without increment:

let i = 0;
for (; i < 3;) {
  console.log(i); // 0, 1, 2
  i++;
}

Infinite Loops

An infinite loop runs indefinitely and must be stopped with a break statement.

Example:

for (;;) {
  // repeats forever
}

Breaking the Loop

You can exit a loop at any time using the break directive.

Example in JavaScript:

let sum = 0;
while (true) {
  let value = +prompt("Enter a number", '');
  if (!value) break;
  sum += value;
}
console.log('Sum:', sum);

Example in TypeScript:

let sum: number = 0;
while (true) {
  let value: number = +prompt("Enter a number", '');
  if (!value) break;
  sum += value;
}
console.log('Sum:', sum);

Continuing to the Next Iteration

The continue directive skips the current iteration and moves to the next one.

Example in JavaScript:

for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) continue;
  console.log(i); // 1, 3, 5, 7, 9
}

Example in TypeScript:

for (let i: number = 0; i < 10; i++) {
  if (i % 2 === 0) continue;
  console.log(i); // 1, 3, 5, 7, 9
}

Labels for break/continue

Labels can be used to break out of multiple nested loops.

Example in JavaScript:

outer: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    let input = prompt(`Value at coords (${i},${j})`, '');
    if (!input) break outer;
  }
}
console.log('Done!');

Example in TypeScript:

outer: for (let i: number = 0; i < 3; i++) {
  for (let j: number = 0; j < 3; j++) {
    let input: string = prompt(`Value at coords (${i},${j})`, '');
    if (!input) break outer;
  }
}
console.log('Done!');

Summary

  • while: Repeats a block of code while the condition is true.
  • do…while: Similar to while, but executes the block at least once.
  • for: A loop with initialization, condition, and increment/decrement in one line.
  • Use break to exit a loop and continue to skip the current iteration.
  • Labels can be used with break and continue to manage nested loops.

Call to Action  

How do you think AI will shape the future of technology? Share your thoughts in the comments below. For more insights into the latest tech trends, visit our website PlambIndia and stay updated with our blog.  

 

Follow Us  

Stay updated with our latest projects and insights by following us on social media:  

- LinkedIn: PlambIndia Software Solutions  

- PlambIndia: Plambindia Software Solution.

- WhatsApp Number: +91 87663 78125

- Email: contact@plambIndia.com , kuldeeptrivedi456@gmail.com

Contact Us

Become a Client

Explore our diverse range of services and find the perfect solution tailored to your needs. Select a category below to learn more about how we can help transform your business.

Kuldeep Trivedi

plot no 1 / 2 suraj mall compound mal compound

+918766378125

contact@plambindia.com


By clicking contact us button, you agree our terms and policy,
Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
Your Cart