Consider the following code segment in C++. How many times the for loop will be repeated?
Consider the following code segment in C++. How many times the for loop will be repeated?
int main ( )
{
int f = 1;
for (; f ;)
cout <<"f = " <<f++<<"\n";
return 0;
}
Show answerHide answer
Correct answer: (C) Repeated forever
Explanation
for(; f ;) has no update expression of its own, and f starts at 1 and is incremented each time by f++. f never becomes 0 in normal counting, so the condition stays true and the loop runs forever.