Machine-translated by Google. Questions are authored in English.
Top

Discussion

Point out the error, if any in the for loop.

#include <stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

 

  • A.There should be a condition in the for loop
  • B.The two semicolons should be dropped
  • C.The for loop should be replaced with while loop.
  • D.No error

Answer: D

Correct answer: No error

for(;;) is legal and idiomatic: all three clauses may be omitted, and an omitted condition is taken as true, giving an infinite loop. The two semicolons are required, since the header always has three parts. The loop here terminates properly through its break.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.