the output for the following loop
for(putchar('c');putchar('a');putchar('r'))
{
putchar('t')
}
please give output with explaination
Answers
Answer:
Explanation:
Control flow of for loop is like:
1. Initialize
2. Condition check
3. Execute loop body
4. Update
5. Repeate from 2 to 4 untill condition holds.
So this is how control flow of for loop is to be implemented.
Now, in given question:
for(putchar('c');putchar('a');putchar('r'))
putchar('t');
1. Initializarion: putchar('c') results: c
2. Condition check : putchar('a') results : a
3. Executions of loop body : putchar('t') results : t
4. Updation : putchar('r') results : r
5. Repition : atratratratratratr......
final result: catratratratratrat......
As we can see it is an infinte loop so the correct answer is: D
if it would have been implemented using Recursion, then this will be continued until the runtime stack get exhausted. But just because it has been implemented using for loop it will be continuing itself like this forever.