C Quiz – 104 | Question 2

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.