JavaScript While/do Loop | JavaScript for Web Development - Software Development PDF Download

Introduction

The while/do loop is a fundamental concept in JavaScript that allows you to execute a block of code repeatedly as long as a specified condition is true. This article will guide you through the basics of the while/do loop, provide clear examples, and offer sample problems to help solidify your understanding.

Understanding the While Loop

The while loop executes a block of code repeatedly as long as a specified condition is true. It consists of a condition and a code block enclosed in curly braces. The condition is evaluated before each iteration, and if it evaluates to true, the code block is executed. If the condition is false initially, the code block is skipped entirely.

Executing Code with the Do/While Loop

The do/while loop is similar to the while loop, but with one important difference: it executes the code block at least once before evaluating the condition. This means that the code block is guaranteed to run at least once, regardless of whether the condition is initially true or false.

Key Differences Between While and Do/While Loops

  • The while loop checks the condition first and then executes the code block, while the do/while loop executes the code block first and then checks the condition.
  • The while loop may skip the code block entirely if the condition is initially false, while the do/while loop always executes the code block at least once.

Examples and Code Explanation

Let's explore two examples to understand the while loop and the do/while loop in action.

Example 1: Printing Numbers with While Loop

let number = 1;


while (number <= 5) {

  console.log(number);

  number++;

}

Code Explanation: In this example, we start with number set to 1. The while loop condition checks if number is less than or equal to 5. If true, it executes the code block, which prints the current value of number and increments it by one. This process repeats until the condition becomes false. The output will be:

1

2

3

4

5

Example 2: Calculating Factorial with Do/While Loop

let number = 5;

let factorial = 1;


do {

  factorial *= number;

  number--;

} while (number > 0);


console.log("Factorial:", factorial);

Code Explanation: In this example, we calculate the factorial of a given number using a do/while loop. We start with number set to 5 and factorial set to 1. Inside the loop, we multiply factorial with the current number and decrement number by one. The loop continues as long as number is greater than 0. Finally, we output the calculated factorial. The output will be:

Factorial: 120

Sample Problems

Now, let's tackle some sample problems to reinforce our understanding of while and do/while loops.

Problem 1: Counting Even Numbers
Write a program that counts and prints all the even numbers from 1 to 20 using a while loop.

let number = 1;


while (number <= 20) {

  if (number % 2 === 0) {

    console.log(number);

  }

  number++;

}

Problem 2: Sum of Digits
Write a program that calculates and prints the sum of the digits of a given number using a do/while loop.

let number = 12345;

let sum = 0;


do {

  sum += number % 10;

  number = Math.floor(number / 10);

} while (number > 0);


console.log("Sum of digits:", sum);

Conclusion

In this article, we explored the while loop and the do/while loop in JavaScript. We learned that the while loop executes a code block as long as a condition is true, while the do/while loop guarantees the code block is executed at least once. We examined examples and solved sample problems to solidify our understanding. With this knowledge, you can now leverage these loop structures to create powerful and efficient JavaScript programs.

The document JavaScript While/do Loop | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

pdf

,

Viva Questions

,

shortcuts and tricks

,

Summary

,

video lectures

,

MCQs

,

Extra Questions

,

Previous Year Questions with Solutions

,

Free

,

Sample Paper

,

Important questions

,

practice quizzes

,

JavaScript While/do Loop | JavaScript for Web Development - Software Development

,

Semester Notes

,

JavaScript While/do Loop | JavaScript for Web Development - Software Development

,

JavaScript While/do Loop | JavaScript for Web Development - Software Development

,

Exam

,

past year papers

,

ppt

,

study material

,

Objective type Questions

;