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

Download the notes
JavaScript While/do Loop
Download as PDF
Download as PDF

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);

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

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
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
51 videos|30 docs|12 tests

Up next

51 videos|30 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

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

,

Semester Notes

,

Summary

,

MCQs

,

study material

,

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

,

pdf

,

mock tests for examination

,

Sample Paper

,

video lectures

,

past year papers

,

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

,

Important questions

,

ppt

,

shortcuts and tricks

,

Objective type Questions

,

Free

,

Exam

,

practice quizzes

,

Extra Questions

,

Viva Questions

,

Previous Year Questions with Solutions

;