निम्नलिखित में से कौन सा कोड सबसे अधिक कुशल है?
Which of the following code is most efficient ?
Code 1;
for(var number=10;number>=1;number--)
{
document.writeln(number);
}
Code 2 ;
var number=10;
while(number>=1)
{
document.writeln(number);
number++;
}
A)
B)
C)
D)
Explanation:
Here’s a concise explanation:
Code 1:
- It uses a
for
loop that correctly counts down from 10 to 1, and stops after 10 iterations.
Code 2:
- It uses a
while
loop but increments the number, which causes an infinite loop because the conditionnumber >= 1
never becomes false.
Result:
- Code 1 is efficient because it works as expected.
- Code 2 creates an infinite loop, making it inefficient.
Correct Answer: (A) Code 1.