C | Loops & Control Structure | Question 11
Output of following C program?
#include<stdio.h> int main() { int i = 0; for ( printf ( "1st\n" ); i < 2 && printf ( "2nd\n" ); ++i && printf ( "3rd\n" )) { printf ( "*\n" ); } return 0; } |
(A) 1st
2nd
*
3rd
2nd
*
(B) 1st
2nd
*
3rd
2nd
*
3rd
(C) 1st
2nd
3rd
*
2nd
3rd
(D) 1st
2nd
3rd
*
1st
2nd
3rd
Answer: (B)
Explanation: It is just one by one execution of statements in for loop.
a) The initial statement is executed only once.
b) The second condition is printed before ‘*’ is printed. The second statement also has short circuiting logical && operator which prints the second part only if ‘i’ is smaller than 2
b) The third statement is printed after ‘*’ is printed. This also has short circuiting logical && operator which prints the second part only if ‘++i’ is not zero.
Please Login to comment...