Open In App
Related Articles

C Quiz – 104 | Question 2

Improve Article
Improve
Save Article
Save
Like Article
Like

With respect to following “for” loops in C, pick the best statement. Assume that there is a prior declaration of ‘i’ in all cases




for (i = 0; i < 10 ; i++) // (i)<br>
for ( ; i < 10 ; i++) // (ii)<br>
for (i = 0;  ; i++) // (iii)<br>
for (i = 0; i < 10 ; ) // (iv)<br>
for ( ; ; ) // (v)<br>

(A) Only (i) and (v) would compile successfully. Also (v) can be used as infinite loop.
(B) Only (i) would compile successfully.
(C) All would compile successfully but behavior of (ii), (iii) and (iv) would depend on compiler.
(D) All would compile successfully.


Answer: (D)

Explanation: In C, any of the 3 expressions of “for” loop can be empty. The exact behavior of the loop depends on the body of the loop as well. Basically, all of the 3 expressions of loop can be put inside the loop body. So as per C language standard, all of the above are valid for loops.

Quiz of this Question

Last Updated : 08 Nov, 2021
Like Article
Save Article
Similar Reads