✅ JavaScript While Loop – Summary
Purpose:
The while loop is used to execute a block of code repeatedly as long as
a specified condition is true.
🔹 Syntax:
javascript
CopyEdit
while (condition) {
// code block to be executed
}
Example:
javascript
CopyEdit
let i = 0;
while (i < 10) {
console.log("The number is " + i);
i++;
}
Output:
python
CopyEdit
The number is 0
The number is 1
...
The number is 9
🔹 The do...while Loop
A do...while loop is similar to a while loop, but it always executes at
least once, even if the condition is false.
javascript
CopyEdit
let i = 0;
do {
console.log("The number is " + i);
i++;
} while (i < 10);
🔹 Key Difference between for and while:
A for loop includes:
Initialization
Condition
Increment
A while loop focuses only on the condition, and initialization/increment
are done outside the loop body.
✍️Subjective Questions with Answers
Q1. What is the main difference between a while loop and a
do...while loop?
Answer:
A while loop checks the condition before executing the code
block.
A do...while loop executes the code block at least once before
checking the condition.
Q2. When would you use a while loop instead of a for loop?
Answer:
Use a while loop when the number of iterations is not known in
advance and you want to keep looping based on a condition.
Q3. Write a program to print numbers from 1 to 5 using a while
loop.
Answer:
javascript
CopyEdit
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Output:
CopyEdit
1
2
3
4
5
Q4. Write a do...while loop that prints numbers from 10 to 1.
Answer:
javascript
CopyEdit
let i = 10;
do {
console.log(i);
i--;
} while (i >= 1);
Q5. Can a while loop run infinitely? Explain with an example.
Answer:
Yes. If the condition never becomes false, the loop continues forever.
javascript
CopyEdit
let i = 1;
while (i > 0) {
console.log(i); // infinite loop
}
📝 10 MCQs on JavaScript While and Do/While
Loops
1. Which loop is guaranteed to run at least once?
A. while
B. do...while ✅
C. for
D. None of the above
Explanation:
do...while runs once before checking the condition.
2. What is the output of the following code?
javascript
CopyEdit
let i = 0;
while (i < 3) {
console.log(i);
i++;
}
A. 0 1 2 ✅
B. 1 2 3
C. 0 1 2 3
D. Infinite loop
Explanation:
Runs while i < 3 → prints 0, 1, 2.
3. What is the correct syntax for a do...while loop?
A. do while (condition) {}
B. do { } while (condition); ✅
C. while { } do (condition);
D. loop while (condition) {}
Explanation:
Option B is correct syntax in JavaScript.
4. How many times will this loop run?
javascript
CopyEdit
let i = 5;
while (i < 5) {
console.log(i);
i++;
}
A. 5 times
B. 0 times ✅
C. 1 time
D. Infinite loop
Explanation:
Since i < 5 is false from the start, loop doesn’t run.
5. What is the key difference between while and do...while loops?
A. do...while checks condition first
B. while always runs
C. do...while runs at least once ✅
D. They are the same
Explanation:
do...while executes before checking the condition.
6. Which loop is best when the number of iterations is unknown?
A. for
B. while ✅
C. do...while
D. switch
Explanation:
Use while when condition depends on runtime data.
7. What happens if we forget to increment the variable in a while
loop?
A. Loop runs once
B. Loop ends
C. Infinite loop ✅
D. Syntax error
Explanation:
Condition remains true → infinite loop.
8. Find the output:
javascript
CopyEdit
let i = 1;
do {
console.log(i);
i++;
} while (i < 1);
A. No output
B. 1 ✅
C. 1 2
D. Infinite loop
Explanation:
Runs once before checking i < 1 (which is false).
9. Which statement is true about while loops?
A. They always run once
B. They check the condition after the code block
C. They may not run at all ✅
D. They need a break statement
Explanation:
If the condition is false at the start, the loop never runs.
10. Which of the following is a valid use of while?
javascript
CopyEdit
let i = 0;
while (true) {
console.log("Hello");
break;
}
A. Syntax Error
B. Prints "Hello" infinitely
C. Prints "Hello" once ✅
D. Never runs
Explanation:
Condition is always true, but break exits the loop after one print.