C Programming :: Loop Control - Discussion
60 / 61
#include <stdio.h>
int main()
{
int i = 0;
for (foo(); i == 1; i = 2)
printf("In for loop\n");
printf("After loop\n");
}
int foo()
{
return 1;
}
#include <stdio.h>
int main()
{
int i = 0;
for (foo(); i == 1; i = 2)
printf("In for loop\n");
printf("After loop\n");
}
int foo()
{
return 1;
}
AAfter loop
BIn for loop after loop
CCompile time error
DInfinite loop
Show Explanation
Asked In ::
Read Full Answer
Report Error
Please Login First Click Here
Amit Kumar
3 Sep, 2018 10:12 PM
Answer is correct,
if you see carefully the code, i is initialize as zero, and in for loop it comparing i == 1 which will be false so that second printf will work.
in for loop it calling function, which will retrun 1 however, it will not impact much as its just initialization part of far loop.
In for loop, for(initialization; condition ;increment).
in given code condition will fail as i value is zero, and it checking that whether it is equal to 1 or not.
coming back to your answer, it will be, if you make some changes in code like you initialize i =1;
so that condition will be true in for and both statement will be printed, in that scenario only option B will be true.
Report Error