By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

Infinite for loop

P: 48
Expand|Select|Wrap|Line Numbers
  1. for(;;)
  2. {
  3. }
is an infinite loop

but whether it can be made finite by modifying it into
Expand|Select|Wrap|Line Numbers
  1. i = 5;
  2. for(;;)
  3. {
  4. printf("%d", i++)
  5. if(i>10)
  6. break;
  7. }
My opinion is the above code will not be infinite and the loop will come to an end after i takes the value 11. Is it correct?
Aug 25 '10 #1

✓ answered by whodgson

Yes and it will print 567891011

Share this Question
Share on Google+
2 Replies

P: 542
Yes and it will print 567891011
Aug 26 '10 #2

Expert Mod 5K+
P: 9,197
Your loop is still an infinite loop.

The fact you broke out of the loop does not make it finite.

In this case, the loop should not have been infinite to start with:

Expand|Select|Wrap|Line Numbers
  1. i = 5; 
  2. for(;i<=10;++i) 
  3. printf("%d", i) 
A break is usually a sign of a poorly desinged loop. That is, the code that regulates the loop should not appear inside the loop. This is why the for statement was put into C in the first place.
Aug 27 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.