2. Which of the following is an infinite loop?
a) int i = 5; for (int a=0; i < 20; a--)
b) int a = 5; while (a<20) a++;
c) for (a=0; a<20; a++) { }
d) for (int a = 2; a<20; a= a+2)
Answers
Answered by
3
Answer:
- Option a is the correct answer for the problem.
Explanation:
a)
int i = 5;
for (int a = 0; i < 20; a--) { }
In this code, we can see that the value of 'i' is never incremented. So, the condition i < 20 remains true always. Thus the loop runs forever.
b)
int a = 5;
while (a < 20)
a++;
Here, the value of a is increasing which means the condition given inside brackets ultimately becomess false after some time. So the loop terminates.
c)
for(int a = 0; a < 20; a++) { }
This is quite similar to previous option. The value of a keeps increasing and the loop terminated when a becomes equal to 20.
d)
for (int a = 2; a < 20; a = a + 2) { }
This code is similar to option c. Value of a increases by 2 and finally the loop terminates.
anindyaadhikari13:
Thanks for the brainliest :)
Similar questions
Physics,
9 days ago
Economy,
9 days ago
English,
19 days ago
Math,
9 months ago
Computer Science,
9 months ago