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.
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.
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.
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
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);
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.
51 videos|28 docs|12 tests
|
|
Explore Courses for Software Development exam
|