C Quiz – 104 | Question 1

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 < 10; i = 0 ; i++) // (i)
for (i < 10; i++ ; i = 0) // (ii)
for (i = 0; i < 10 ; i++) // (iii)
for (i = 0; i++ ; i < 10) // (iv)
for (i++; i = 0 ; i < 10) // (v)
for (i++; i < 0 ; i = 10) // (vi)

chevron_right


(A) All the above “for” loops would compile successfully.
(B) All the above “for” loops would compile successfully. Except (iii), the behaviour of all the other “for” loops depend on compiler implementation.
(C) Only (iii) would compile successfully.
(D) Only (iii) and (iv) would compile successfully.
(E) Only (iii) and (iv) would compile successfully but behaviour of (iv) would depend on compiler implementation.


Answer: (A)

Explanation: Basically, all of the “for” loops are valid i.e. . In the above examples, it doesn’t matter what expression has been put in which part of a “for” loop. The execution order of these expressions remain same irrespective of where they have been put i.e. “1st expression” followed by “2nd expression” followed by “body of the loop” followed by “3rd expression”. But the exact behavior of each of the above “for” loop depends on the body of loop as well. In fact the following is also valid and work without any issue in C. for(printf(“1st”) ; printf(“2nd”) ; printf(“3rd”)) { break; }

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.